Help with Loops and I/O Files - Where am I going wrong?

I need to know where I am going wrong in this code.

These were my instructions:

Do not use arrays or tables, functions are not required. I am providing you a file called Stock.txt that contains information regarding the 9 different stock items carried by THRIFTY SALES, INC. The file is a repeating sequence of three records such that the first record represents an item code number, the second record represents the description of each item and the third record contains the current selling price. Then the sequence repeats. Your program is to simulate the checkout register allowing the check out clerk to input item codes to obtain the current selling prices and descriptions. If an incorrect code is entered, the clerk should be notified by an appropriate message on the screen. If the item is found, the clerk will enter the number of items with that description and price and continue to the next item. When all items have been entered the clerk terminates the transaction by entering an item code of 0 (zero). The program will build a sales slip called Sales.txt enumerating the item code, description, unit price, quantity purchased, and total extended price. The bill will be subtotaled and an 8% sales tax added for a final total. Amount tendered by the customer and change given will be finally added to the invoice. This invoice should be written out to an output file called Sales.txt. The final total and change should appear on the screen as well.
Your output file should line up all columns and output formatted for dollars and cents on money amounts, using setprecision, showpoint, and fixed. Make sure you format your output to the screen as well.
This assignment uses file processing found in chapter 5.
Do not use arrays or tables.

This is what I have so 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

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

using namespace std;

int main ()
{
	
	int ans;
	int itemNum;
	int amt;
	char response;
	float subtotal;
	float total;
	int taxRate = 0.08;
	float tax;
	float moreMoney;
	float money;
	float change;
	bool found;

	
	ofstream outputFile;
	int itemNumber;
	string description;
    double price;
	
	
	outputFile.open ("sales.txt");		
											
		if (!outputFile)
		{
		 cout << "The File (sales.txt) failed to open";
		}

	total = 0;
	
	cout << "What Item number would you like me to look up for you?"<<endl;
	cin>> itemNum;
	
		
			while (itemNum != 0);
			{
			
			ifstream inputFile;
			inputFile.open ("stock.txt");
			
			inputFile >> itemNumber;
			inputFile >> description;
			inputFile >> price;
			
				if (!outputFile)
				{
				 cout << "The File (sales.txt) failed to open";
				}		
				
				
				int count;
				for ( count = 1; count <= 9; count++);
				{
				
						if ( itemNum = itemNumber);
						{ 
						cout << "Item number " << itemNumber << ", is pulling up " << description << " and costs $" << price << " per item." << endl;
						cout << "How many " << description << " would you like to purchase?" << endl;
						cin >> amt;
						subtotal = price * amt;
						total += subtotal;
				
						outputFile << itemNumber;
						outputFile << description;
						outputFile << amt;
						outputFile << subtotal;
			
						}
				}
				
				
						cout << "OK, I've Added " << amt << " " << description << " to your order." << endl;
				
			}
				 
		
	
if (itemNum = 0);
{
cout << fixed << showpoint << setprecision (2);
tax = subtotal * .08;
cout << "Tax : $" << tax <<endl;
total = subtotal + tax;
cout << "Your Total is : $" << total << endl;
cout << "How much would you like to pay?"<< endl;
cin >> money;
	while (money < total);
	{ 
	cout << "I'm sorry, but that is not enough money to cover the Total. How much more would you like to give?"<< endl;
	cin >> moreMoney;
	moreMoney = money + moreMoney;
	}
}
cout << fixed << showpoint << setprecision (2);
change = money - total;
cout << "Your change is : $" << change << endl;
outputFile << tax;	
outputFile << total;
outputFile << money;
outputFile << change;

outputFile.close ();

return 0;
}
Hi,
The first thing to do is see if the code compiles, with warnings turned on. I used cpp.sh with c++14 and all 3 warnings:

In function 'int main()':
65:32: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
65:33: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
88:16: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
88:17: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
12:6: warning: unused variable 'ans' [-Wunused-variable]
15:7: warning: unused variable 'response' [-Wunused-variable]
18:6: warning: unused variable 'taxRate' [-Wunused-variable]
23:7: warning: unused variable 'found' [-Wunused-variable]


== is for comparison, = is for assignment
empty body means you have an ; which is a null statement - get rid of it :+)

If you format your editor to use spaces instead of tabs it will display better when posted here, This site converts tabs to 8 spaces, so there is excessive indenting.

Good Luck !!
ok, I did a fresh run at it, but still haven't quite got it yet.
any ideas???

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

using namespace std;

int main ()
{

int itemNumber;
string description;
float price;

int itemNum;

bool cont = true;
bool found = false;


int amt;
float total = 0;

ifstream inputFile;
inputFile.open ("Stock.txt");

if (!inputFile)
{
cout << "The file (Stock.txt) Failed to open."<< endl;
}


ofstream outputFile;
outputFile.open ("sales.txt");

if (!outputFile)
{
cout << "The file (sales.txt) Failed to open."<< endl;
}



if (inputFile)
{
while (cont);
{
cout << "What Item number would you like me to look up for you?"<<endl;
cin>> itemNum;

if (itemNum == 0)
cont = true;

while (!inputFile.eof() && cont)
{
inputFile >> itemNumber;
inputFile >> description;
inputFile >> price;

if (itemNum == itemNumber)
{
found = true;
break;
}
}

if (found && cont)
{
cout << "How many " << description << " would you like to purchase?" << endl;
cin >> amt;

outputFile << (amt * price);
total = total + (amt * price);
found = false;
cout << "OK, I've Added " << amt << " " << description << " to your order." << endl;
}

else if (cont)
{
cout << "I'm sorry, but we do not appear to carry that Item in Stock."<< endl;
}

}

outputFile << "Subtotal : $"<< total;
outputFile << "Tax : $"<< (total * .08);
total = total + (total * .08);
outputFile << "Total : $"<< amt;
cout << "Your total comes to : $" << total << endl;
cout << "How much would you like to pay? " << endl;





float money;
float change;
cin >> money;

while (money <= total)
{
cout << "I'm sorry, but that is not enough money to cover the total." << endl;
cin >> money;
}
change = money - total;


cout << "Your change is : $" << change << endl;

inputFile.close ();
outputFile.close ();
}



return 0;
}
















Last edited on
Its compiling without errors now, but not working at all
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
   int itemNumber;           // Make life easy for yourself ...
   int itemNum;              // ... choose more different names
   string description;
   float price;
   int amt;
   float total = 0;

   ofstream outputFile( "sales.txt" );

   while( true )                                                     // Forget cont
   {
      cout << "What item number would you like me to look up for you? (0 to finish): ";
      cin>> itemNum;

      if ( itemNum == 0 ) break;                                     // Break out of the while loop here

      ifstream inputFile( "stock.txt" );                             // Either reopen, or go back to the beginning
      if ( !inputFile )
      {
         cout << "The file (stock.txt) Failed to open." << endl;
         return 1;
      }

      bool found = false;                                            // Only used here: define at this point
      while ( inputFile >> itemNumber >> description >> price )      // Don't use eof
      {
         if ( itemNum == itemNumber )
         {
            found = true;
            break;
         }
      }

      if ( found )
      {
         cout << "How many " << description << " would you like to purchase? ";
         cin >> amt;

         outputFile << ( amt * price );
         total = total + ( amt * price );
         found = false;
         cout << "OK, I've added " << amt << " " << description << " to your order.\n\n";
      }

      else
      {
         cout << "I'm sorry, but we do not appear to carry that item in stock.\n\n";
      }

      inputFile.close();
   }

   outputFile << fixed << setprecision( 2 );                           // This is money ...
   cout << fixed << setprecision( 2 );

   outputFile << "Subtotal : $" << total;
   outputFile << "Tax : $" << total * 0.08;                            // Magic number here ...
   total = total * ( 1 + 0.08);                                        // ... and here
   outputFile << "Total : $"<< amt;
   cout << "Your total comes to : $" << total << endl;
   cout << "How much would you like to pay? " << endl;

   float money, change;
   cin >> money;

   while ( money <= total )
   {
      cout << "I'm sorry, but that is not enough money to cover the total. Enter again: ";
      cin >> money;
   }
   change = money - total;
   cout << "Your change is : $" << change << endl;

   outputFile.close();
}


stock.txt:
1   socks     5.99
2   knickers  6.99
3   trousers  25.00
4   shirt     19.99
5   sweater   29.99


What item number would you like me to look up for you? (0 to finish): 12
I'm sorry, but we do not appear to carry that item in stock.

What item number would you like me to look up for you? (0 to finish): 1
How many socks would you like to purchase? 3
OK, I've added 3 socks to your order.

What item number would you like me to look up for you? (0 to finish): 4
How many shirt would you like to purchase? 2
OK, I've added 2 shirt to your order.

What item number would you like me to look up for you? (0 to finish): 0
Your total comes to : $62.59
How much would you like to pay? 60
I'm sorry, but that is not enough money to cover the total. Enter again: 70
Your change is : $7.41

Topic archived. No new replies allowed.