continue loop

This is a discussion on continue loop within the C++ Programming forums, part of the General Programming Boards category; I am trying to continue this loop until the person enters 0 then it will end up printing out the ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    continue loop

    I am trying to continue this loop until the person enters 0 then it will end up printing out the receipt. But it only lets me enter up to two two items then goes to press any key to continue. Why is it only letting me do two items before it quits? There is more cpp files and an h file but I am not including those here, I'm just stuck on this loop. What am I doing wrong? thanks for the help

    Bryan

    Code:
    #include <iostream>
    #include <fstream>
    #include <cctype>
    #include <string>
    //#include "grocery.h"
    
    using namespace std;
    
    int ProdNum;
    string ProdName;
    float ProdPrice; 
    char Taxable;
    int Quantity;
    
    const int tax_rate=.075;
    
    int main()
    
    {
    	ifstream OpenFile("inventory.txt");
    	
    	char ch;
    
          while(!OpenFile.eof())
      {
    OpenFile.get(ch);
    	cout << ch;
    	}
    	cout << endl << endl;
    	 
    	cout << "Thanks for shopping at OUR Market." << endl << endl;
    
    {
    	int customer = 1;
    
    	cout << "Enter purchases for customer " << customer++ << endl;	
    	cout << "Enter product code 0 to end purchases." << endl << endl;
    }
    cin >> ProdNum;
     	   
    
      if (ProdNum!=0)  {
        cin >> ProdNum;
      }
      else  {
        cout<<"Thanks for shopping, your receipt is printed below" << endl << endl;
      }
    
      //Calculations
    
    
    /*cout << "Is there another customer? (Y/N) ";
    		cin >> customer;
    		 while (customer == "Y");
    		if (customer == "N")
    		cout << "Thanks for shopping at OUR Market." << endl;
    		
    */
    return 0;
    }
    Last edited by romeoz; 09-25-2003 at 03:38 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you need to use another loop.

    the opening brace above if(ProdNum >= 1) and the closing brace above //calculations are meaningless as is. They would be in reasonable placement if you want to enclose all the intervening code in your second loop, controlled by the following condition: while(ProdNum != 0).

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I changed the above file but it still gets the same error. It stops and says "Press any key to continue after two entries." I need it to go to "Thank you ect". only when I press 0.

    Bryan

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    while(ProdNum!=0)
    cin >> ProdNum;

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I appreciate your help in this program but I try that and it still says illegal else without matching if....Could you give a little more detail, I'm sorry.

    Bryan

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    There is nothing in your code that would make it loop to ask for more products. You have this code:

    Code:
    cin >> ProdNum;
     	   
    
      if (ProdNum!=0)  {
        cin >> ProdNum;
      }
      else  {
        cout<<"Thanks for shopping, your receipt is printed below" << endl << endl;
      }
    As you can see, if you enter in anything but 0 the first time, it asks you again, then just continues down to the end. What you want is something like this:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cctype>
    #include <string>
    //#include "grocery.h"
    
    using namespace std;
    
    int ProdNum;
    string ProdName;
    float ProdPrice; 
    char Taxable;
    int Quantity;
    
    const int tax_rate=.075;
    
    int main()
    
    {
    	ifstream OpenFile("inventory.txt");
    	
    	char ch;
    
          while(!OpenFile.eof())
      {
    OpenFile.get(ch);
    	cout << ch;
    	}
    	cout << endl << endl;
    	 
    	cout << "Thanks for shopping at OUR Market." << endl << endl;
    
    int customer = 1;
    
    cout << "Enter purchases for customer " << customer++ << endl;	
    cout << "Enter product code 0 to end purchases." << endl << endl;
    
    do
    {
         cin << ProdNum;
    } while (ProdNum!=0); //loops while the user enters non-zero
    
    cout<<"Thanks for shopping, your receipt is printed below" << endl << endl;
    
      //Calculations
    
    
    /*cout << "Is there another customer? (Y/N) ";
    		cin >> customer;
    		 while (customer == "Y");
    		if (customer == "N")
    		cout << "Thanks for shopping at OUR Market." << endl;
    		
    */
    return 0;
    }
    You're also going to need to implement an array for the product entering part. I mean, the way it is now, you're only storing one Product Number. Each time the user enters a number, it overwrites the last one entered. I'll leave that to you however.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Originally posted by bennyandthejets
    You're also going to need to implement an array for the product entering part. I mean, the way it is now, you're only storing one Product Number. Each time the user enters a number, it overwrites the last one entered. I'll leave that to you however.
    I suggest a linked list implementation

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    thanks for the help....that works perfectly...I am going to create an array to read my inventroy file to get all the informationn next...I just wanted to get my loop and stuff working..thanks again

    Bryan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Continue and switch
    By camzio in forum C Programming
    Replies: 10
    Last Post: 10-04-2008, 08:31 AM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  4. cant get the loop to work right
    By djxtremor in forum C Programming
    Replies: 3
    Last Post: 10-30-2002, 05:34 AM
  5. Switch in a While Loop
    By WindShield in forum C Programming
    Replies: 6
    Last Post: 10-19-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21