I have been working on this program for about two weeks. I missed the day that the professor covered the material necessary to code this program due to surgery. I am now behind about 5 programs that are due at the end of the week and I'm trying to teach myself what I've missed. I have asked classmates for help and what they have told me has not been working. If anyone has the free time, I would absolutely love it if you could debug this program for me. I know this community is NOT for giving homework answers. I wrote all this code on my own and cannot figure out why if won't display the correct values in their respective places in the formatting. It is a program that we were assigned in a lab one day. The program is supposed to input a name and a pay rate from a data file, and then calculate the net pay and print it to an output file in a check format. The formatting is correct but the numbers are wrong. Please help.

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

using namespace std; 

// Named constant definitions (and function declarations): 

ifstream inData;
ofstream outData;

// Main program: 

int main() 
{ 
// Variable declarations:
 
// Insert declarations from various parts below, as needed

float net;
float hrs;
float rate;
int dollars;
float cents; 
string payto;
string date;



// Function body: 

// Insert code from Part I algorithm - inputting data from file

inData.open("wages.dat");
outData.open("check.out");

getline(inData,payto);
inData >> payto;
inData >> rate;

// Insert code from Part II algorithm - calculating net pay

	cout << "Please enter the total hours worked." << endl;
	cin >> hrs;

	cout << "Please enter today's date (ex.  MM/DD/YYYY)." << endl;
	cin >> date;
	cout << endl;

net = hrs * rate;


// Insert code from Part III algorithm - determining dollars and cents

dollars = net;
cents = (net - dollars) * 100;



// Insert code from Part IV & V of algorithm - printing check to datafile

	outData << endl << "12432 Somewhere St." << endl << setw(48) << left << setfill (' ') << "Russellville, AR 72802";
	outData << setw(13) << setfill(' ') << right << date << setw(12) << right << setfill('_') << "Date" << endl;
	outData << endl;
	outData << setw(7) << left << setfill('_') << "Pay" << setw(47) << left;
	outData << setfill('_') << payto << " $" << setw(15) <<setprecision(2) << showpoint << fixed << net;
	outData << endl << endl << setw(18) << right << setfill(' ') << dollars << "_Dollars_&_";
	outData << cents << '_' << setw(18) << left << setfill(' ') << "Cents" << endl << endl;
	outData << endl << setw(43) << left << setfill(' ') << "Bank of Foundations I" << setw(30);
	outData << right << setfill('_') << '_' << endl;


// Remove block commet notation before beginning to make code visible

inData.close();
outData.close();


return 0; 
} // end function main

I would think that your problem is here:

getline(inData,payto);

inData >> payto;
inData >> rate;

You might have better luck with something like:

inData >> payto >> rate; // Read in first values, priming read
  	
while(inData){ //as long as there are numbers to be read (this loop should get all values from the file, in order)
  	
   // whatever you're supposed to do with your two variables will go here
        				
    inData >> payto >> rate; //read next two values
}
inData.close();

Myrtle Turtle,

Thank you for the reply. My class has not yet covered the "while" statement that you used. If you could explain what exactly it does and how it works, it would be appreciated. Thank you.

ALSO.....
I don't know if I explained the problem well. The program should read a name from the input file. Also, it reads an hourly pay rate that is used to calculate the total net pay. I have to display the dollars and cents separately as if it was written on a check. Sorry if there was any confusion. And thank you again for the response.

If your class hasn't covered while loops then my solution probably isn't what you want.

But here is some information on while loops anyway. Sorry I could not be more help to you.

Ok. Loops are actually what we are about to look at. Thank you for your direction to new material and assistance with this program.

I think MyrtleTurtle had the correct answer, even if while loops aren't necessary. He recommended code that would read multiple names and payment rates, but you are only trying to read one.

For the code you were using:

getline(inData,payto);
inData >> payto;
inData >> rate;

getline reads the first line of your input file (inData) into the payto string.

line 2 then takes the next word from your input file (all characters up to a whitespace) and places them in that same payto string.

line 3 then takes another word from your input file, converts it to a floating point number, and then places it in your rate variable.

Since I'm guessing your input file looks like: Joe 15.00 , you don't need that first getline(...) statement. The remaining 2 lines will read the name and rate. If your file is only the 1 line shown above, then the output will look funny, because that first getline() statement will read all the data your file has to offer. The remaining lines won't be able to read (and therefore change) anything, so your float variable, for example, will be completely random.

Galf,
Thank you for your input. Going through each line and explaining exactly what it did helped me to fix the code. The data file had the name and pay rate on two different lines. So, what I did, with the help from your response, was just omit the inData >> payto; line since the getline statement grabbed the first line and moved on. Thank you for both the responses. I'm sure I will have many more simplistic questions. I really do appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.