Reading a file

Hello, my professor gave us a file that has peoples names, current salary, and salary % increase. He is wanting us to code a program that will read the data from a file then display it onto the screen in organized columns. However, he also wants us to calculate what the new salary will be using the % increase and current salary. I have the program to where it will display everything properly, however I have know idea how to make the computer grab the correct values as variables to calculate the new salary. Please help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

int main() {


    double salaryIncrease;
    double newSalary;
    double salary;
    string line;
    ifstream myfile ("FiletoRead.txt");


    if (myfile.is_open()){
        while ( getline (myfile,line)){

            cout << line << '\n';
    }
    myfile.close();
  } else {
            cout << "Unable to open file";
  }
  return 0;
}
















You know the order of the data in the file. Read in each piece of data one at a time. You know which read is the salary. Read it in to a number type, such as int. You know which read is the percentage. Read it in to a number type. Use cin to do this. Calculate the new salary from those two data.
It would help if you show how your file is structured.

If your file is formatted like the following

Smith John 55000 0.05
Brown Lucy 56000 0.06
Soprano Tony 1200000 0.10


Then it might be better to use space-delimited cin >> as opposed to getline.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

int main() {
    using namespace std;

    string lastName;
    string firstName;
    double salary;
    double salaryIncrease;

    ifstream myfile ("FiletoRead.txt");

    if (myfile.is_open()) {
        while (cin >> lastName >> firstName >> salary >> salaryIncrease) {
            double newSalary = salary * salaryIncrease;
            cout << newSalary << '\n';
        }
    } else {
        cout << "Unable to open file";
    }
    return 0;
}


Last edited on
Okay, i tried that and instead i just get a blank prompt, with no output. My input file is set up in the example you showed. Like this:

Last First Salary PercentInc
Cavendish Brandon 80543 5.6%
Hill John 9353 2.4%

Last edited on
Show the contents of your file.

Copy it between [code] [/code] tags.
Oh I'm an idiot. I meant to use

while (myfile >> lastName >> firstName >> salary >> salaryIncrease)

cin use stdin, not file input.
I tried change that, I still get no output.
Hmm, well it should be entering the while loop at least once before it fails. Do you have a print statement inside your while loop to check of it's entering there? (You can also use a proper debugger, but print statement's are a poor man's debugger).

If your file has the percent sign % after at the end of the 4th number, you'll need to loop through a fifth token to capture that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

int main() {
    using namespace std;

    string lastName;
    string firstName;
    double salary;
    double salaryIncrease;
    string percentToken;

    ifstream myfile ("file.txt");

    if (myfile.is_open()) {
        while (myfile >> lastName >> firstName >> salary >> salaryIncrease >> percentToken) {
            cout << lastName << " " << firstName << " " << salary << " " << salaryIncrease << percentToken << "\n";
            double newSalary = salary * salaryIncrease;
            cout << newSalary << '\n';
        }
    } else {
        cout << "Unable to open file";
    }
    return 0;
}


Input:
1
2
3
John Smith 20000 1.1%
Sally Ride 30000 3.3%
Foo Bar 3 100%


Output:
1
2
3
4
5
6
John Smith 20000 1.1%
22000
Sally Ride 30000 3.3%
99000
Foo Bar 3 100%
300


_______________________

Basically, if the input is 42%, this counts as 2 tokens when you're parsing with input >> some_double >> some_string
Last edited on
Oh I'm an idiot. I meant to use
while (myfile >> lastName >> firstName >> salary >> salaryIncrease)


Don't be so hard on yourself. To err is human.
Okay, i've got it to where it will display the data I need, however I need it to be in perfect columns, how can i get it organized? As you can see i tried using spaces but it doesn't work


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main() {


    double salaryIncrease;
    double newSalary;
    double salary;
    string firstName;
    string lastName;
    ifstream myfile ("FiletoRead.txt");

    if (myfile.is_open()){
        while((myfile >> lastName >> firstName >> salary >> salaryIncrease)){


            cout << lastName << "     " << firstName << "     " <<salary << salaryIncrease << "     ";

            newSalary = salary + (salary * (salaryIncrease / 100));
            cout << newSalary << endl;
    }
    myfile.close();
  } else {
            cout << "Unable to open file";
  }
  return 0;
}













Last edited on
Try to mess around with stream formatting functionality inside the <iomanip> header, specifically setw
http://www.cplusplus.com/reference/iomanip/setw/

If needed, there is other formatting functionality in the <ios> header, namely std::left and std::right http://en.cppreference.com/w/cpp/io/manip/left (if you need the correct justification).

setw will automatically add more whitespace (by default) to match the width you need.

Ex, this will give yourself 10 characters of room for the first name, and 20 characters of space for the last name.
1
2
3
4
5
6
7
8
9
10
11
12
// Example program
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
  std::string firstName = "Tom";
  std::string lastName = "Jones";
  
  std::cout << std::setw(10) << std::left << firstName << std::setw(20) << lastName << "!" << std::endl;
}

Last edited on
Topic archived. No new replies allowed.