Thread: need a little help-where is the error?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    need a little help-where is the error?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	double kilometers, double miles, double centigrade, double fahrenheit;
    	double square_miles, double acres, double inches, double centimeters;
    	int menu_number;
    
    	cout << "Press enter after entering a value!\n";
    	cout << endl;
    	cout << "MENU\n";
    	cout << endl;
    	cout << "1) Kilometers to Miles\n";
    	cout << "2) Centigrade to Fahrenheit\n";
    	cout << "3) Square Miles to Acres\n";
    	cout << "4) Inches to Centimeters\n";
    	cout << endl;
    	cout << "Please enter a number from the menu: ";
    	cin >> menu_number;
    
    	if (menu_number == '1');
    	{
    		cout << "Please enter the number of kilometers: ";
    		cin >> kilometers;
    		cout << endl;
    		miles = kilometers * 0.62;
    		cout << kilometers << " kilometers equals " << miles << " miles.\n";
    	}
    		else if (menu_number == '2');
    		{
    			cout << "Please enter the degrees centigrade: ";
    			cin >> centigrade;
    			cout << endl;
    			fahrenheit = ((9/5)*centigrade) + 32;
    			cout << centigrade << " degrees centigrade equals " << fahrenheit << " degrees fahrenheit.\n";
    		}
    			else if (menu_number == '3');
    			{
    				cout << "Please enter the square mileage: ";
    				cin >> square_miles;
    				cout << endl;
    				acres = square_miles * 640;
    				cout << square_miles << " square miles equals " << acres << " acres.\n";
    			}
    				else if (menu_number == '4');
    				{
    					cout << "Please enter the number of inches: ";
    					cin >> inches;
    					cout << endl;
    					centimeters = inches * 0.39;
    					cout << inches << " inches equals " << centimeters << " centimeters.\n";
    				}
    					else
    					{
    						cout << "You have entered an invalid number. Please try again.\n";
    					}
    					
    					return 0;
    }
    [edit]Code tags added by Hammer. Use them next time please.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    first of all, declare your variables like this:

    Code:
    double kilometers, miles, centigrade, fahrenheit;
    double square_miles, acres, inches, centimeters;
    int menu_number;
    You don't need to keep saying "double".

    Next, you don't need semicolon's after if and elseif, ie

    if (menu_number == 1)

    instead of

    if(menu_number == 1);

    ALSO, since the menu selection is an integer, you don't need the single quotes around the numbers in the if statements.

  3. #3

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    another problem

    how do i loop the menu, and also--how do I keep the program running after i have entered an invalid number?

  5. #5
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131

    Re: Another Problem

    Originally posted by papisuave
    how do i loop the menu, and also--how do I keep the program running after i have entered an invalid number?
    You Can Make another function for the Menu... and Input.. or you
    you can use a goto statement in your last else part of the condition.
    Check Out the Useage of goto Statement.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	double kilometers,miles, centigrade, fahrenheit;
    	square_miles, acres, inches, centimeters;
    	int menu_number;
    
    	cout << "Press enter after entering a value!\n";
    	cout << endl;
    	cout << "MENU\n";
    	cout << endl;
    	cout << "1) Kilometers to Miles\n";
    	cout << "2) Centigrade to Fahrenheit\n";
    	cout << "3) Square Miles to Acres\n";
    	cout << "4) Inches to Centimeters\n";
    	cout << endl;
                    menu:  // See This Line ...for goto...
    	cout << "Please enter a number from the menu: ";
    	cin >> menu_number;
    
    	if (menu_number == 1)
    	{
    		cout << "Please enter the number of kilometers: ";
    		cin >> kilometers;
    		cout << endl;
    		miles = kilometers * 0.62;
    		cout << kilometers << " kilometers equals " << miles << " miles.\n";
    	}
    		else if (menu_number == 2)
    		{
    			cout << "Please enter the degrees centigrade: ";
    			cin >> centigrade;
    			cout << endl;
    			fahrenheit = ((9/5)*centigrade) + 32;
    			cout << centigrade << " degrees centigrade equals " << fahrenheit << " degrees fahrenheit.\n";
    		}
    			else if (menu_number == 3)
    			{
    				cout << "Please enter the square mileage: ";
    				cin >> square_miles;
    				cout << endl;
    				acres = square_miles * 640;
    				cout << square_miles << " square miles equals " << acres << " acres.\n";
    			}
    				else if (menu_number == 4);
    				{
    					cout << "Please enter the number of inches: ";
    					cin >> inches;
    					cout << endl;
    					centimeters = inches * 0.39;
    					cout << inches << " inches equals " << centimeters << " centimeters.\n";
    				}
    					else
    					{
    						cout << "You have entered an invalid number. Please try again.\n";
     goto menu; 					
    }
    					
    					return 0;
    }
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    menu

    i don't undestand what you mean by the menu part, it keeps coming up with an error

  7. #7
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Keep in mind that many people consider goto statements to be bad pogramming practice.

    An alternative would be to place everything in a do-while loop.


    PHP Code:
    bool loopAgain;

    do
    {
    loopAgain false;
    //menu
    //excute based on selection
    //if invalid set
      
    else
      {
      
    cout << "You have entered an invalid number. Please try again.\n";
      
    loopAgain true;
      }
    } while(
    loopAgain); 


    everytime the loop started loopAgain is set to false, this means if they make a valid selection then the loop will break at the end, but if they make an invalid selection loopAgain is set to true so the while statement checks it and because it is true loops again.

    We have to set loopAgain to false at the beginning each time so that if on that looping they make a valid selection then the loop will break.

    Another approach would be to set loopAgain=false at the begginning, and only change it to false if they choose the menu option for Quit. This allows the user to repeatedly use the program again and again until they decide the want to exit.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM