Please help...once again!

Hello everyone. I have once again hit a brick wall in my C++ Course. I have been trying to get this program with arrays to work. But, for some reason, I can't even get the main part of the program to help. Here is what I have thus far:
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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

int main(){
    int fn, ln;
    char stat [100];
    float ssn[100], hw[100], hr[100], oth[100], otp[100], regp[100], gp[100], tx[100], np[100];
int counter=0;
int i=0;
ifstream fin("5A.txt");
fin>>stat[i]>>ssn[i]>>hw[i]>>hr[i]>>oth[i]>>otp[i]>>regp[i]>>gp[i]>>tx[i]>>np[i];
cout<<"PLEASE ENTER THE EMPLOYEE'S FIRST NAME:";
cin>>fn;
cout<<"PLEASE ENTER THE EMPLOYEE'S LAST NAME:";
cin>>ln;
cout<<endl;
cout<<"__________________________________________________________________________"<<endl;
cout<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl;
cout<<"=========================================================================="<<endl;
cout<<"FIRSTNAME "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"LASTNAME "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"STAT "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"SSN "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"HW "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"HR "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"OTH "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"OTP "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"REGP "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"GROSS "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"TAX "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"NET "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<endl;
cout<<"=========================================================================="<<endl;
cout<<fn<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<ln<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<stat<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<ssn<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<hw<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<hr<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<oth<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<otp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<regp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<gp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<tx<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<np<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<endl;
    cin.get();
    return 0;
} //MAIN 

Basically, what I would like ^^this mess to do is have the User enter the Employee's First Name. Then, it should ask the User for the Employee's Last Name. Then, the program should call to the .txt file for the input information. Here is what I have in said file:
1
2
JOHN SMITH M 113 50 20 10 300 800 1100 385 715
JANE DOE M 223 45 15 5 112.5 675 787.5 275 512.5

Any help would be greatly appreciated!
Oh my god...

I would suggest starting over and trying to add some more structure to your program. First off, just use getline() into two std::strings (one for their first name, one for their second). Then open the file and go through each line until you find the desired line with the employee on it. Then just print out that whole line. You should only need a few variables.
Last edited on
Flags don't automatically clear. You don't have to keep setting the precision to the same thing.
Not to mention you're storing the employees first and last name in an int.
Yeah, I am confused about the first and last name part. If I don't store them in the int, which one will the program look for in the .txt file? And ascii, thank you for your post, but I am not sure I understand it. Do you mean something like taking out the "cout" lines and use "getline()" with the "PLEASE ENTER..." part? I am not familiar with getline(). After that, call for the .txt file in the code?

Thanks in advance :)
If I don't store them in the int, which one will the program look for in the .txt file?

Don't quite follow what you're saying here, what does this sentence mean? Because the first and last names of employees are strings of characters, they should be treated as such. I would recommend using an std::string to store the names: http://www.cplusplus.com/reference/string/string/
Do you mean something like taking out the "cout" lines and use "getline()" with the "PLEASE ENTER..." part? I am not familiar with getline(). After that, call for the .txt file in the code?

getline() is used to get user input into a std::string. I'm saying you should use getline() to read into the strings. At the moment you're using integers to store the name, which makes no sense. Then after that use a while loop to iterate through each line in the file, and compare each name to the name entered by the user. If the name is found, print out the line.
Thanks a lot for your continued attention, ascii :)

After reading the info in the link you provided, plus some of the links on that page, I edited the code. But, it is outputting all of the information in the .txt file after I input the first and last names. I am thinking I need a while statement in there, but I am not sure where to start. Here is what I have thus far:
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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

int main(){
    string fn;
    string ln;
    string stat [100];
    string ssn[100], hw[100], hr[100], oth[100], otp[100], regp[100], gp[100], tx[100], np[100];
int counter=0;
int i=0;
//counter<5;
ifstream fin("5A.txt");
cout<<"PLEASE ENTER THE EMPLOYEE'S FIRST NAME:"<<fn;
getline (fin,fn);
cout<<"PLEASE ENTER THE EMPLOYEE'S LAST NAME:"<<ln;
getline (fin,ln);

    fin>>fn[i]>>ln[i]>>stat[i]>>ssn[i]>>hw[i]>>hr[i]>>oth[i]>>otp[i]>>regp[i]>>gp[i]>>tx[i]>>np[i];
cout<<"__________________________________________________________________________"<<endl;
cout<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl;
cout<<"=========================================================================="<<endl;
cout<<"FIRSTNAME "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"LASTNAME "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"STAT "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"SSN "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"HW "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"HR "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"OTH "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"OTP "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"REGP "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"GROSS "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"TAX "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"NET "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<endl;
cout<<"=========================================================================="<<endl;
cout<<fn<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<ln<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<stat<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<ssn<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<hw<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<hr<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<oth<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<otp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<regp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<gp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<tx<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<np<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<endl;
    cin.get();
    return 0;
} //MAIN 

Thanks in advance!
Yes, that's what I was trying to say, that right know your programming is outputting everything and it needs branching to only output the desired line. Read through this: http://cplusplus.com/doc/tutorial/files/ and pay specific attention to this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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

  else cout << "Unable to open file"; 

  return 0;
}


You're program should be similar to this one, except in the while loop you should compare each line to the desired line.
I've been reading that page over and over, and I just came seem to understand it. It is running and asking me for the first name, but then it just skips the last name and outputs a bunch of numbers and letters in place of the info from the .txt file. I am at a loss here as to what to do next. Here is what I have now, after the edit:
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
59
60
61
62
63
64
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

int main(){
   // string fn;
   // string ln;
   int fn;
   int ln;
   string line;
//int counter=0;
int i=0;
//counter<5;
cout<<"PLEASE ENTER THE EMPLOYEE'S FIRST NAME:";
cin>>fn;
cout<<"PLEASE ENTER THE EMPLOYEE'S LAST NAME:";
cin>>ln;
ifstream fin("5A.txt");
if (fin.is_open()){
    while ( fin.good() )
{
    getline (fin,line);
}
    string stat [100], ssn[100], hw[100], hr[100], oth[100];
    string otp[100], regp[100], gp[100], tx[100], np[100];
    fin>>stat[i]>>ssn[i]>>hw[i]>>hr[i]>>oth[i]>>otp[i]>>regp[i]>>gp[i]>>tx[i]>>np[i];
cout<<"__________________________________________________________________________"<<endl;
cout<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl;
cout<<"=========================================================================="<<endl;
cout<<"FIRSTNAME "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"LASTNAME "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"STAT "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"SSN "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"HW "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"HR "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"OTH "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"OTP "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"REGP "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"GROSS "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"TAX "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<"NET "<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<endl;
cout<<"=========================================================================="<<endl;
cout<<fn<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<ln<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<stat<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<ssn<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<hw<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<hr<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<oth<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<otp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<regp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<gp<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<tx<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)
<<np<<setw(5)<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint|ios::left)<<endl;
}

    cin.get();
    return 0;

} //MAIN 


Here is kinda what I want it to do:
1
2
3
4
5
6
7
8
PLEASE ENTER THE EMPLOYEE'S FIRST NAME: (User enters a name from the .txt file, like JOHN)
PLEASE ENTER THE EMPLOYEE"S LAST NAME: (User enters a last name from the .txt file, like SMITH)
__________________________________________________________________________
DR. EBRAHIMI'S PAYROLL INSTITUTE
==========================================================================
FIRSTNAME LASTNAME STAT SSN  HW   HR   OTH   OTP   REGP GROSS TAX   NET
==========================================================================
(the program opens the .txt file, searches for that name, and outputs the info for that name, and then loops back to the beginning)

Am I way off base, here? I cannot figure out what I am doing wrong :(
As I have suggested twice now, I'd recommend that you start over. Right now your code is messy. And this time around try to practice proper indentation, most editors and IDE's will do it automatically for you. You're still inputting the first and last names into integers as well. Here's a skeleton program for you to work off of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// I haven't compiled this, so you might have to fix some errors
#include <iostream>
#include <ifstream>
#include <string>

int main(int argc, char *argv[]) 
{
	std::string first, last;	
	ifstream data ("data.txt");
	while (1) {
		// get the first and last name of the input
		std::cout << "First name (or empty line to quit): ";
		getline(cin, first);
		if (firstname.empty())        // if an empty line is entered
			break;
		std::cout << "Last name: ";
		getline(cin, last);
		// now search the file
	}
        return 0;
}

I've left the rest up to you because I don't like to give out full answers. I would recommend using a while loop to grab each line from the file and check to see if the names on that line match the desired name. If it does, output the line.
Last edited on
I am starting over now. It is pretty much too late to get it in on time anyway, so I am going to start from scratch. Thanks for the advice, ascii, as well as the skeleton to work with. I really do appreciate your expertise and knowledge :)
It's no problem, and I hardly consider myself an expert, just an enthusiast (with a lot to learn, their are far more knowledgeable members of this site than me). It's probably better to get it in right but late than to turn it in wrong. You only need to add a few more lines of code too make it work from the skeleton program. A while loop to go through each line of the file, and an if statement to check if the line is the desired one.
Well, you definitely seem to know what you're talking about, and you have the patience to help a noob out :)

Speaking of the while loop, can you provide any tips on that? For instance, let's say I were to do something like this:
1
2
3
4
5
6
7
8
while (fin>>fn[i]>>ln[i]>>stat[i]>>ssn[i]>>hw[i]>>hr[i]){
if (ln[100] == "SMITH"){ //displays info for JOHN SMITH
else {
         if (ln[100] == "DOE")
         if (ln[100] == "REY")
         if (ln[100] == "MI")
         }
}

Would that just show the info for the last name("ln[100]") the User provides? Also, any hints on where to place it would be great.
The while loop should be placed where I said to in my skeleton program (the comment that says "now search the file"). You should really read that article I posted earlier on file input and output. As I've said before, I would suggest using getline() to read each line of the file, and then compare the name on that line to the name entered by the user. If there is a match, print the output of that getline().
Topic archived. No new replies allowed.