Jump to content

I don't know what to do!?

PeshkovIntel

So i am a freshmen in college and i have to get this program written within the next 12hrs for extra credit. I can't wrap my head around how to get started mainly how to get a program to read a txt file and output a new one. 

And this is to be written in C++. 

 

This right here is the question

 

 

There are three employees in a company and they are slated to get a pay increase this year. You are given a file, named employeeData.txt, that has the following data inside.

Haptal Felicity 75124.87 5.3
Beckinsale Ford 49546.56 6.7
Longoria Dempsey 85700.50 4.9

Each line has the employee’s last name, first name, current salary, and the percent for their pay increase.

For example, the first line of the data is the first employee and their last name is Haptal, their first name is Felicity, their salary is 75124.87, and their pay increase is 5.3 percent.

Write a program that reads data from the specified file, employeeData.txt, and stores the output in the file employeeRaise.dat. For each employee, the data stored in the output file must be of this form:
firstName lastName updatedSalary. Be sure to format the output of decimal numbers to two decimal places.
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, PeshkovIntel said:

So i am a freshmen in college and i have to get this program written within the next 12hrs for extra credit. I can't wrap my head around how to get started mainly how to get a program to read a txt file and output a new one. 

To start with, what have you tried so far, and what language is it? We won't do your homework for you, but if you give us where your heads at we can help point you in the right direction.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Hint: Filereader.

 

I won't do your homework for you. At least post something you've written and we'll  give pointers or corrections.

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
Share on other sites

Link to post
Share on other sites

It is C++. So i have the structure of the program already set up. But how do i get it to read the txt file. It keeps saying " Cannot locate file". Is there a certain location that the txt file needs to be in? 

Link to comment
Share on other sites

Link to post
Share on other sites

This is currently the state of my program 

 

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("employeeData.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  return 0;
}
 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, PeshkovIntel said:

It is C++. So i have the structure of the program already set up. But how do i get it to read the txt file. It keeps saying " Cannot locate file". Is there a certain location that the txt file needs to be in? 

Well that just means the file location you entered is wrong..

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, PeshkovIntel said:

 

When posting code, please format it like this.

public static void main(String args[]){
	//just an example I know this is Java
}

 

2 minutes ago, PeshkovIntel said:


  ifstream myfile ("employeeData.txt");

You need to put in the entire path to the file.

Like so ("D:/stuff/fileishere/file.txt");

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
Share on other sites

Link to post
Share on other sites

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("C:\Users\Mark\Desktop\Computer Programs\Assignment 02 EC\employeeData.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  return 0;
}

I'm still getting the same error. Everything is in the right location 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, PeshkovIntel said:

ifstream myfile ("C:\Users\Mark\Desktop\Computer Programs\Assignment 02 EC\employeeData.txt");

You're going to need to escape the \ by doing 

ifstream myfile("C:\\Users\\Mark\\Desktop\\Computer Programs\\Assignment 02 EC\\employeeData.txt");

 

Otherwise it thinks you are trying to use string literals (think of things like \n (newline), \t (tab), and \r (carriage return)). Things like this seem silly at first, but you catch on. Other than that I'm pretty sure this code should read in the data just fine. Nothing else sticks out as incorrect here.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, bleedblue said:

You need to put in the entire path to the file.

Not necessarily.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

In this situation it can be argued that the file location is easier to find through a relative path, not through an explicit one.

ifstream myfile("../employeeData.txt");

Should work as expected.

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/8/2018 at 11:05 PM, reniat said:

To start with, what have you tried so far, and what language is it? We won't do your homework for you, but if you give us where your heads at we can help point you in the right direction.

 

On 10/8/2018 at 11:06 PM, bleedblue said:

Hint: Filereader.

 

I won't do your homework for you. At least post something you've written and we'll  give pointers or corrections.

 

You guys sounds like hardcore Stack users. ?

Link to comment
Share on other sites

Link to post
Share on other sites

what i would do is something like
------------
open input file
create/open output file
read line by line
    split each line by empty space
    process the values
    write on output
______________

keep in mind that you will need to look for many things if you decide to take this approach,


 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Franck said:

 

 

You guys sounds like hardcore Stack users. ?

...probably cuz i am

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×