Look for structural/logic errors

I have to variants of the same program, below. They run, but return invalid data when I run them. The code is as follows with the results posted below that. If I could have some experienced eyes look this over for mistakes I would appreciate it.

Here is the text file input the programs read.

JOHNNY BEGOOD*
265
SALLY GREAT*
650
SAM KLUTZ*
177
PETE PRECISE*
400
FANNIE FANTASTIC*
399
MORRIE MELLOW*
200


First program. is a sentinel-controlled loop

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
#include <iostream>
#include <fstream>
#include <iomanip>  //output formatting
#include <string>   //string variables

using namespace std;
int main()

{
double pieces;  //number of pieces made
double pay;  //amount earned 
double piecestot = 0.0; //pieces accumulator
double paytotal = 0.0; //pay total accumulator
string name; //name of worker

ifstream inFile;
ofstream outFile;

//***********input statements****************************

inFile.open("Piecework1.txt"); //opens the input text file
outFile.open("piecework1.out");  //opens the output text file 
outFile << setprecision(2) << showpoint;
outFile << name << setw(6) << "Pieces" << setw(12) << "Pay" << endl;
outFile << "_____" << setw(6) << "_____" << setw(12) << "_____" << endl;

getline (inFile, name, '*'); //priming read 
inFile >> pieces;

while (name != "End of File") //while condition test
{    //begining of loop
if(pieces>=1 && pieces<=199)
paytotal = pieces * .50;

if (pieces>=200 && pieces<=399)
pay = pieces * .55;

if (pieces>=400 && pieces<=599)
pay = pieces * .60;

if (pieces>=600)
pay = pieces * .65;

outFile << name << setw(6) << piecestot << setw(12) << paytotal << endl;

piecestot = piecestot + pieces;
paytotal = paytotal + pay;

} //end of loop

inFile.close();
outFile.close();

return 0;

}


Here are the output results.

Pieces Pay
_____ _____ _____
JOHNNY BEGOOD 0.00 0.00
JOHNNY BEGOOD2.7e+002 1.5e+002
JOHNNY BEGOOD5.3e+002 2.9e+002
JOHNNY BEGOOD8.0e+002 4.4e+002
JOHNNY BEGOOD1.1e+003 5.8e+002
JOHNNY BEGOOD1.3e+003 7.3e+002
JOHNNY BEGOOD1.6e+003 8.7e+002
JOHNNY BEGOOD1.9e+003 1.0e+003
JOHNNY BEGOOD2.1e+003 1.2e+003
JOHNNY BEGOOD2.4e+003 1.3e+003
goes on appears to be an infinite loop....


Here is the second version of the code, is a count controlled loop.

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
#include <iostream>
#include <fstream>
#include <iomanip>  //output formatting
#include <string>   //string variables

using namespace std;
int main()

{
double pieces;  //number of pieces made
double pay;  //amount earned 
double piecestot = 0.0; //pieces accumulator
double paytotal = 0.0; //pay total accumulator
string name; //name of worker

ifstream inFile;
ofstream outFile;

//***********input statements****************************

inFile.open("Piecework2.txt"); //opens the input text file
outFile.open("piecework2.out");  //opens the output text file 
outFile << setprecision(2) << showpoint;
outFile << name << setw(6) << "Pieces" << setw(12) << "Pay" << endl;
outFile << "_____" << setw(6) << "_____" << setw(12) << "_____" << endl;


getline(inFile, name, '*'); //priming read 
inFile >> pieces;

for (int i=0;i<=6;i++) //while condition test
{    //begining of loop
if(pieces>=1 && pieces<=199)
paytotal = pieces * .50;

if (pieces>=200 && pieces<=399)
pay = pieces * .55;

if (pieces>=400 && pieces<=599)
pay = pieces * .60;

if (pieces>=600)
pay = pieces * .65;

outFile << name << setw(6) << piecestot << setw(12) << paytotal << endl;

piecestot = piecestot + pieces;
paytotal = paytotal + pay;

} //end of loop

inFile.close();
outFile.close();

return 0;

}


Here are the results.

Pieces Pay
_____ _____ _____
JOHNNY BEGOOD 0.00 0.00
JOHNNY BEGOOD2.7e+002 1.5e+002
JOHNNY BEGOOD5.3e+002 2.9e+002
JOHNNY BEGOOD8.0e+002 4.4e+002
JOHNNY BEGOOD1.1e+003 5.8e+002
JOHNNY BEGOOD1.3e+003 7.3e+002
JOHNNY BEGOOD1.6e+003 8.7e+002
Hi, you need to do another getline at the end (but inside of) the while/for loop, otherwise, the loop is always working with JOHNNY BEGOOD (the first line, it only got to read from the file one time).
What line would you suggest I put it on, and how should it be structured?
in your first code example, on line 48.
1
2
3
4
5
piecestot = piecestot + pieces;
paytotal = paytotal + pay;
getline(inFile, name, '*'); 
inFile >> pieces
} //end of loop 


I donĀ“t fully understand your code, I just see that the program enters the while loop with name == JOHNNY BEGOOD and pieces == 265 (you accomplish that thanks to lines 27 and 28). Then it repeats the loop infinitely (in the while version, why should it go out?, it never gets to End of File!) or six times (in the for version) but always whith out re-reading from the file...
Topic archived. No new replies allowed.