Thread: Switch statement = infinite loop

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    9

    Switch statement = infinite loop

    alright... I'm writing a program and my switch statement seems to be causing an infinite loop in certain circumstances.

    When putting in the case numbers, the program works just fine... but when putting in any non-number character (letters, for instance), it enters an infinite loop. My default seems to do nothing.
    Also, if I input certain numbers ( 9 and 10, for instance), it doesn't trigger the default... but other numbers do (6 through 8)...

    I've tried everything I could think of. I've searched google and this forum. I'm using Microsoft Visual Studio .NET 2003

    Here's the entire program.
    Code:
    #include "stdafx.h"
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    
    #include <iomanip>
    using std::setprecision;
    
    int main()
    {
    	int product;
    	int quantity;
    	double product1 = 2.98;
    	double product2 = 4.5;
    	double product3 = 9.98;
    	double product4 = 4.49;
    	double product5 = 6.87;
    	double total = 0;
    	
    	cout << "We offer the following products: \n\nProduct 1 = $"<< product1
    		<< "\nProduct 2 = $" << fixed << setprecision( 2) << product2 << "\nProduct 3 = $"
    		<< product3 << "\nProduct 4 = $" << product4 << "\nProduct 5 = $" <<product5 
    		<< endl << endl;
    
    	cout << "Enter the product number you would like to purchase (-1 when finished): ";
    	cin >> product; 
    	 
    	while ( product != -1 )
    	{
    		switch ( product )
    		{
    		case 1:
    			cout << "Please enter the quantity you would like to purchase: ";
    			cin >> quantity;
    			total = total + ( product1 * quantity );
    			break;
    		
    		case 2:
    			cout << "Please enter the quantity you would like to purchase: ";
    			cin >> quantity;
    			total = total + ( product2 * quantity );
    			break;
    			
    		case 3:
    			cout << "Please enter the quantity you would like to purchase: ";
    			cin >> quantity;
    			total = total + ( product3 * quantity );
    			break;
    
    		case 4:
    			cout << "Please enter the quantity you would like to purchase: ";
    			cin >> quantity;
    			total = total + ( product4 * quantity );
    			break;
    
    		case 5:
    			cout << "Please enter the quantity you would like to purchase: ";
    			cin >> quantity;
    			total = total + ( product5 * quantity );
    			break;
    		
    		case '\n':
    		case '\t':
    		case ' ':
    			break;
    					
    		default: //catch all other characters
    			cout << "Incorrect character entered." << endl;
    			break;
    		}
    
    	cout << "Enter the product number you would like to purchase (-1 when finished): ";
        cin >> product;
    
    	}
    
    	cout << "\nThe total of all purchases is $" << fixed << setprecision( 2 )
    		<< total << endl;
    
    	return 0;
    }
    Last edited by Lucid003; 10-09-2005 at 09:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  2. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  3. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  4. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM
  5. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 06:29 PM