Thread: ????if Else????

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Question ????if Else????

    Ok so i have to create a program that has a basic menu (i.e. apple - a, orange -o, juice - j ) I need to create a loop that allows the customer to enter (a, o, j ) in as an option, yet at the same time rejects any other syllable thats not in the menu by letting the user know they made an invalid entry. Should I use an If else statement for this, if not can what can I use, would some one be able to show me an example and link to show more of this... Thanks for you help in advance.
    cj

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    if you are sure the user is going to enter a single character input, you can use a "switch" statement with the default case being invalid entry.

  3. #3
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    int MENUANSWER;
    MAINMENU:
    cout<<"[1] For Apple"<<endl;
    cout<<"[2] For Orange"<<endl;
    cout<<"[3] For Juice"<<endl;
    cin>>MENUANSWER;
    
    if (MENUANSWER==1)
    {
              //Apple chosen
    }
    
    else if (MENUANSWER==2)
    {
            //Orange chosen
    }
    
    else if (MENUANSWER==3)
    {
           //Juice chosen
    }
    
    else
    {
           //Invalid choice
          goto MAINMENU;
    }
    
    return 0;
    }
    Last edited by bradszy; 02-23-2008 at 07:22 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    switch(x)
    {
    	case 'a': /* Do something; */ break;
    	default: // User did not enter something acceptable
    }
    And don't use goto; use a loop instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    May I ask why not?

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Simply put it is unsafe. There ARE many explanations of this on the board, and on google. 5 minutes of searching will get you hours of proof.

    EDIT:
    Also is this really that hard?
    Code:
    int main()
    {
    	bool quit = false;
    	char input;
    	while (!quit)
    	{
    		std::cin >> input;
    		std::ignore();
    		switch(input)
    		{
    		case 'q':
    			quit = true;
    			break;
    		default:
    			break;
    		}
    	}
    }
    I swear people are afraid of a few extra lines of code, for better readability and code security.
    Last edited by Raigne; 02-23-2008 at 11:02 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    better write your loops in the following manner:
    Code:
    while (std::cin >> input)
    {
    	std::ignore();
    	switch(input)
    	{
    	case 'q':
    		break;
    	default:
    		break;
    	}
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by bradszy View Post
    May I ask why not?
    Mostly because it causes spaghetti code. In other words, it creates hard to read and maintainable code because goto can jump just about anywhere in the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    There are cases where gotos can actually make the code more readable and maintainable (for example, breaking out of nested loops). However, in most other cases, it is bad.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed, but that is pretty much all that it's good for in C++. Almost.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cyberfish View Post
    There are cases where gotos can actually make the code more readable and maintainable (for example, breaking out of nested loops).
    Such a comment is not helpful here. No beginner should ever use a goto; It's counter-productive to their necessary learning of structured programming.

    Don'y use all CAPITALS for variable names. It's ugly and harder to read, and clashes with most people's coding conventions whereby only constants and macros are all uppercase.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by iMalc View Post
    Such a comment is not helpful here. No beginner should ever use a goto; It's counter-productive to their necessary learning of structured programming.
    But the statement was true, and it's not inconceivable that a beginner might write code that requires breaking out of a nested loop. Specific unnecessary uses of goto should be pointed out, and after finding out that 99% of them are in fact unnecessary, they'll get the idea. But I don't think they should be told that it should never be used.

Popular pages Recent additions subscribe to a feed