sentinel loop/file input problem

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;

taxtotal = 0;
pricetotal = 0;

cin >> itemid;
while (itemid != 0)
{
cin >> price >> percentoff >> purchased >> tax;
saleprice = price-((price*percentoff)/100);
pricecurrent = saleprice*purchased;

if (tax == 'T' )
taxcurrent = pricecurrent*SALESTAX;
else taxcurrent = 0;

cout << left << setw(8) << itemid << setw(6)<< left <<setfill(' ')
<< "$" <<right << price << right << setw(5) << percentoff << "% $"
<< setfill(' ') << setw(10) << right << saleprice << right <<
setw(6)<< purchased << setfill(' ') << " $" << setw(10) <<right
<< pricecurrent << setfill(' ') << " $" << right << setw(10)<<
taxcurrent << endl;

taxtotal = taxtotal+taxcurrent;
pricetotal = pricetotal+pricecurrent;
}
cout << left <<"SUBTOTAL" << setw(36) << right << "$" << setw(10) << pricetotal << endl;

cout << left <<"SALES TAX" << setw(36) << right << "$" << setw(10) << taxtotal << endl;

cout << left <<"TOTAL DUE" << setw(36) << right << "$" << setw(10) << pricetotal+taxtotal << endl;


}




Last edited on
1
2
3
4
5
6
7
8
9
10

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.
Last edited on
Topic archived. No new replies allowed.