Hi this is my first programming class and I'm stuck on this assignment trying to get my program to read data from an input file set up something like
123 45.00 10 3 T
921 5.25 0 1 N
83 14.99 25 2 T
0
The last zero is supposed to be a sentinel to end the while loop
the problem is when I run the program with the data file it doesn't give me any output other than the opening formatting. I know it's not terribly pretty and my formatting skills need work but perhaps one of you can tell me where I broke it?
**edit**
Well I have it reading the file now... sort of...
I have to input with ./a.out < data (if that helps anyone)
when i run the program it is endlessly looping and not seeing the 0 sentinel to stop the while loop
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const double SALESTAX = .05;
int main ()
{
int itemid;
double price;
int percentoff;
int purchased;
char tax;
double saleprice;
double pricetotal;
double pricecurrent;
double taxtotal;
double taxcurrent;
cout << "CUSTOMER RECEIPT" << endl;
cout << left << setw(9) <<"ITEM" << right << setw(10)<< "ORIGINAL" << right <<
setw(6)<<"PCT" << setfill(' ')<< setw(3)<< right << setw(13) << "SALES" <<
right << setw(19) << "TOTAL" << setfill(' ')<< setw(3)<< right << setw(13) << "SALES" << endl;
cout << left << setw(9) <<"ID#" << right << setw(10)<< "PRICE" << right <<
setw(6)<<"OFF" << setfill(' ')<< setw(3)<< right << setw(13) << "PRICE" <<
right << setw(6)<< "QTY" << right << setw(13) << "COST" << setfill(' ')<<
setw(3)<< right << setw(13) << "TAX" << endl;
int itemid;
double price;
int percentoff;
int purchased;
char tax;
// ...
cin >> price >> percentoff >> purchased >> tax;
tax is a char. when you cin >> tax, it extracts the first char in the stream which will be a space.
On the next iteration of the loop the you try to read into a variable of type int the 'T' that is still in the input stream. This results in the stream entering a fail state and all subsequent attempts at input extraction fail.
The easiest fix for you would be to make tax a std::string type and change if (tax == 'T' ) to if ( tax == "T").
In addition, I suggest you start testing to see whether input has failed.