C++ people, question

Discussion in 'OT Technology' started by Homan, Jan 31, 2003.

  1. Homan

    Homan Unconquerable OT Supporter

    Joined:
    Mar 12, 2002
    Messages:
    9,764
    Likes Received:
    0
    Location:
    Seattle, WA
    Down at inventory_1_coke function, how do I verify the input by the user? I can make it so it gives them an message if they enter a negative or a non whole number, but what if they enter a character? say character Q. The loop keeps looping and I dont know how to stop it :(

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int inventory_1_coke();
    void instructions();
    void main()
    {
    	int id1;
    	char menu_data;		//variable that is passed into the switch
    	string temp_data;	//temp_data is created incase the user enter mores then 1 character
    	do {	//This do loop loops as long as the user doesn't enter 'q' for quit
    
    		do {	//This do loop makes the user enter the data again if the user enters more then 1 character
    			instructions();
    			cin >> temp_data;
    			cout << endl; 
    
    			//This if block stops the user from entering more then 1 character
    			if (temp_data.length() > 1)
    				{
    					cout << "Error!" << endl; 
    					cout << "You enter 2 or more characters, please enter 1 valid character." << endl << endl;
    				}
    			else;
    				
    			} while (temp_data.length() > 1); //while the character the user enter is more then 1, the loop repeats
    		menu_data = temp_data.at(0); //sets the value of whatever one character the user inputs
    	switch (menu_data)
    		{
    			case 'E': case 'e':
    				cout << "---=Enter Inventory=---" << endl;
    				id1 = inventory_1_coke(); 
    
    				break;
    			case 'P': case 'p':
    				cout << "Purchase soda" << endl;
    				break;
    			case 'S': case 's':
    				cout << "Sell soda" << endl;
    				break;
    			case 'D': case 'd':
    				cout << "Display inventory" << endl;
    				break;
    			case 'Q': case 'q':
    				cout << "Quit program" << endl;
    				break;
    			default: 
    				cout << "Error!" << endl; //Any other character besides e,p,s,d,q gives an error
    				cout << "Your selection was invalid, please enter again..." << endl << endl;
    				break;
    		}
    	} while ((menu_data != 'q') && (menu_data != 'Q')); // If user enters Q or q, the program terminates
    
    }
    
    
    int inventory_1_coke()
    {
    	float inventory, temp2;
    	int temp, counter;
    
    	// temp and temp2 are used to check if the user entered a decimal number or not
    
    	do {
    		cout << "Please enter the inventory of Coke as a non negative and whole number: ";
    		cin >> inventory;
    		cout << endl;
    		temp = inventory;
    		temp2 = inventory - temp;
    		if (temp2 != 0)
    		{
    			counter = 0;
    			cout << "Error!" << endl;
    			cout << "You have entered a decimal number, please enter a whole number" << endl << endl;
    			counter = -1;
    		}
    		else if (inventory < 0)
    		{
    			counter = 0;
    			cout << "Error!" << endl;
    			cout << "You have entered a negative number" << endl << endl;
    			counter = -1;
    		}
    		else;
    	} while (counter = -1);
    	return 0;
    }
    
    void instructions()
    {
    	cout << "This program allows you to enter/buy/sell and view your current soda supplies..." << endl;
    	cout << "[E]nter Inventory------E or e" << endl;
    	cout << "[P]urchase soda--------P or p" << endl;
    	cout << "[S]ell soda------------S or s" << endl;
    	cout << "[D]isplay inventory----D or d" << endl;
    	cout << "[Q]uit program---------Q or q" << endl << endl;
    	cout << "Please enter your selection: ";
    }
     
  2. Kent

    Kent Guest

    Isn't the value of inventory == 0 if they enter a character(s) rather than a number?
     
  3. Homan

    Homan Unconquerable OT Supporter

    Joined:
    Mar 12, 2002
    Messages:
    9,764
    Likes Received:
    0
    Location:
    Seattle, WA
    yeah that's what i am asking, how do i make it so when they enter something that's not a whole number, have it give them an error and prompt them to input it again
     
  4. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I'm assuming that you want only an integer to be entered? In that case:
    Code:
    if (int(inventory) == inventory) {
      it's an integer
    } else {
      it's not
    }
    
    Edit: Fucked up on the code. It's right now.
     
  5. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    homan
    less wc3, more c++
    ;)
     
  6. Homan

    Homan Unconquerable OT Supporter

    Joined:
    Mar 12, 2002
    Messages:
    9,764
    Likes Received:
    0
    Location:
    Seattle, WA
    yeah i am C++ newbie :(
     

Share This Page