Having problems reading lines of text files

Good day, everyone!

I have tried and tried to find the information that I need for this, but I cannot seem to find it anywhere (which probably goes to show that I'm not only a pretty poor programmer, but a bad searcher, too). Anyway, I'm taking a intro to C++ course, and I am stuck on an assignment. I'm hoping someone here can help me out.

The assignment is to create a program that displays the salary of a paycode provided by the user. (Ex: The program asks for a pay code, the user enters "1" and the program produces a single "Pay code 1 makes $15000 per year." statement and asks again.). The program is also supposed to notify the user if an incorrect pay code is given (for instance, 11 in an invalid entry). The entries in the text file are set up like payCode#salary\n or 1#15000 \n 2#18000 \n, etc.

Currently, my program only displays the first line of the text file, regardless of what is input, and I have no clue as to how to test if the pay code is valid.

Any help would be greatly appreciated. Thanks, in advance!

Code below:

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
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;

int main()
{
	string code = "";
	double salary = 0.0;
	ifstream inFile;

	cout << endl;
	cout << "Enter valid payroll code" << endl;
	cout << "or enter -1 to exit: ";
	getline(cin, code);

	do
	{
		inFile.open("Intermediate24.txt");
		if(inFile.is_open())
		{
			getline(inFile, code, '#');
			inFile >> salary;

			cout << fixed << setprecision(2);
			cout << "Payroll code " << code << " makes $" << salary << " per year." << endl << endl;
		}
		else
			cout << "File cannot be opened." << endl;
		// end if
		inFile.close();

		cout << "Enter valid payroll code" << endl;
		cout << "or enter -1 to exit: ";
		getline(cin, code);
	} while(code != "-1"); // end while

	system ("pause");
	return 0;
} // end main 
I realize it's probably bad etiquette to push your own topics here, and I'm sorry about that. I'm just trying to find an answer to my problem. I've read through the tutorial and done many searches across the Internet. All I'm able to find is the way to display all of a text file and not a single selected line.

Thanks again in advance for any help.
Sorry it took so long for any response. . . . It works with this program. There are better ways to do some of the things here but I didn't have much time to attempt involving anything I haven't formally gotten to yet.

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
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>

int main()
{
    using namespace std;

    string input("");
    int code(0), lines(0);
    double salary(0);
    char symbol;
    ifstream inFile;
    cout.setf(ios::fixed); cout.precision(2);

    inFile.open("data/t.txt");
    if(!inFile.fail())
    {
        do
        {   cout << "Enter valid payroll code" << endl
                 << "or enter 0 to exit: ";
            getline(cin, input, '\n');
            code = atoi(input.c_str());

            lines = 0;
            while(!inFile.eof())
            {
                getline(inFile, input, '\n');
                lines++;
            }
            inFile.close();
            inFile.open("data/t.txt");

            if(code > 0 && code <= lines)
            {
                for(int i = 1; i < code; i++)
                    getline(inFile, input, '\n');

                for(int i = 1; i <= 2; i++)
                    inFile.get(symbol);

                inFile >> salary;
                cout << "Payroll code " << code << " makes $" << salary << " per year." << endl << endl;
            }
            else if(code < 0 || code > lines)
                cout << "Invalid payroll code." << endl << endl;

            inFile.close();
            inFile.open("data/t.txt");
        }
        while(code != 0);
    }
    else cout << "File not found.";

    return(0);
}
Thanks so much. I ended up fiddling around with something that at least "works" for the assignment. I doubt it's what it's supposed to look like, but at least it works. This was the last assignment for this class that I'm taking, and it was just driving me crazy, so again; thanks for the help. I plan on continuing with studying programming in C++ after finishing with the class, so I'll be back with more questions; I'm sure. :)

No problem. I'm glad you found something that at least works. I'm just about two and a half months studying C++ myself, so I look forward to seeing you around. :)
Topic archived. No new replies allowed.