Thread: can this program be made simple?? if yes pls help out!

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    15

    can this program be made simple?? if yes pls help out!

    Write a menu program that lets the user select from a list of options, and if the input is not one
    of the options,reprint the list.
    Is this program correct?..i can execute this program but can this code be made simple?

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int option;
        for(int option; ;)
        {
    
    
    cout<<"1.apple"<<'\n'<<"2.mango"<<'\n'<<"3.grape"<<'\n'<<"4.orange"<<'\n'<<"5.banana"<<'\n';
    cout<<"select any one of the option listed above(enter the number of your option):\n";
    cin>>option;
        for(option;option<6;)
        {
            if(option==1)
            {
                cout<<"apple\n";
                break;
            }
            else if(option==2)
            {
                cout<<"mango\n";
                break;
            }
            else if(option==3)
            {
                cout<<"grape\n";
                break;
            }
            else if(option==4)
            {
                cout<<"orange\n";
                break;
            }
            else if(option==5)
            {
                cout<<"banana\n";
                break;
            }
            else
            {
                cout<<"enter the number again:\n";
            }
        }
    }
    }
    Last edited by recklezz; 04-15-2015 at 10:04 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is good that you made some effort to indent your code, but you need to be consistent, e.g.,
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int option;
        for (int option; ;)
        {
            cout << "1.apple" << '\n' << "2.mango" << '\n' << "3.grape" << '\n' << "4.orange" << '\n' << "5.banana" << '\n';
            cout << "select any one of the option listed above(enter the number of your option):\n";
            cin >> option;
            for (option; option < 6;)
            {
                if (option == 1)
                {
                    cout << "apple\n";
                    break;
                }
                else if (option == 2)
                {
                    cout << "mango\n";
                    break;
                }
                else if (option == 3)
                {
                    cout << "grape\n";
                    break;
                }
                else if (option == 4)
                {
                    cout << "orange\n";
                    break;
                }
                else if (option == 5)
                {
                    cout << "banana\n";
                    break;
                }
                else
                {
                    cout << "enter the number again:\n";
                }
            }
        }
    }
    A few things that I observed:
    • You declared option twice: once in the immediate scope of the main function and another time in the scope of the outer for loop. The rule of thumb is to declare variables near first use, so I would declare it in the scope of the for loop.
    • Are you sure that you are supposed to loop forever instead of requesting for user input that will indicates the end of the program?
    • The printing of the menu and prompt can be simplified to:
      Code:
      cout << "1.apple\n2.mango\n3.grape\n4.orange\n5.banana\n"
              "select any one of the option listed above(enter the number of your option):\n";
    • Your inner for loop could be replaced by an if statement, in which case you could get rid of the break statements. Actually, I would just write the if-else chain that you wrote inside the inner for loop.
    • Instead of the if-else chain, you could use a switch statement. (Though then the breaks go back in, but it is no big deal.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would put strings into array and then access string by index - so I do not need to modify code when adding the new option - only the array
    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

  4. #4
    Guest
    Guest
    Another way to make it more compact might be to declare an array of strings for your options. You can then simply use the entered number (minus 1) to query the right element.
    Edit: vart beat me to it

    Quote Originally Posted by laserlight View Post
    (...) The rule of thumb is to declare variables near first use, so I would declare it in the scope of the for loop.
    Is this recommendation mostly down to readability, or because of performance implications?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Guest
    Is this recommendation mostly down to readability, or because of performance implications?
    Both, though performance is not a concern here since the variable is of a built-in type, and in some cases where the variable is expensive to construct and cheap to modify minimally perhaps one would avoid declaring it within a loop instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    It's probably better to use a do-while loop instead, which in addition to the array of strings would look something like this:

    Code:
        // Initialize an array of strings for the options:
        std::string options[] = {/* options */};
    
        // Record the total number of strings in options array:
        int numOptions = sizeof(options) / sizeof(options[0]);
    
        // Declare a variable to hold user input:
        int userSelection = 0;
        do
        {
            // Print options with for loop, use the loop variable to number the options.
            // Ask user to choose a number associated with an option.
            // Record user selection into the variable 'userSelection'
    
        } // Keep loop going so long as user input is invalid:
        while (userSelection < 0 || userSelection >= numOptions);
    
        // Print the string in options that the user selected.
    The do-while loop will allow you to get the users input at least once before needing to check the variable that is to hold the input. That makes the loop body a good place to put the messages you want displayed on invalid input.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    1.Thanks for the tip .
    2.I'm not sure in the question it says "if the input is not one of the options,reprint the list." i couldn't get a single reprint of the menu so the infinite loop.
    3.If i replace the inner for loop with an if statement the program would just end if i enter a number larger than 5.
    4.i'm still in loops haven't got that far :/.

  8. #8
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    i haven't studied array yet

  9. #9
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    this is a little complicated for me now :/..i'll understand this better if i go further in c++

  10. #10
    Guest
    Guest
    There are few ways to make the program simpler at your current level, so you've done a good job. Babysteps, it'll come.

  11. #11
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    thanks

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not seeing a point to the nested loop at all.

    One infinite loop should do the trick, i.e.
    Code:
        int option;
        for (; ;)
        {
            cout << "1.apple" << '\n' << "2.mango" << '\n' << "3.grape" << '\n' << "4.orange" << '\n' << "5.banana" << '\n';
            cout << "select any one of the option listed above(enter the number of your option):\n";
            cin >> option;
    
            if (option == 1)
            {
                cout << "apple\n";
                break;
            }
            else if (option == 2)
            {
                cout << "mango\n";
                break;
            }
            else if (option == 3)
            {
                cout << "grape\n";
                break;
            }
            else if (option == 4)
            {
                cout << "orange\n";
                break;
            }
            else if (option == 5)
            {
                cout << "banana\n";
                break;
            }
            else
            {
                cout << "enter the number again:\n";
            }
        }
    }
    Without some change to option the other loop for (option; option < 6; ) this loop really has nothing to do. I would remove it and the program should work the same.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by recklez
    2.I'm not sure in the question it says "if the input is not one of the options,reprint the list." i couldn't get a single reprint of the menu so the infinite loop.
    Well, is "exit" supposed to be one of the options? If not, then it looks like an infinite loop is expected.

    Quote Originally Posted by recklez
    3.If i replace the inner for loop with an if statement the program would just end if i enter a number larger than 5.
    No, since your outer loop is an infinite loop, it will just loop again to print the menu and read input, without printing "enter the number again:\n". As you probably want to print that "error message", I noted that I would "just write the if-else chain that you wrote inside the inner for loop", i.e., get rid of the inner for loop entirely as in whiteflags' example in post #12.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2015
    Posts
    15
    Quote Originally Posted by laserlight View Post
    Well, is "exit" supposed to be one of the options? If not, then it looks like an infinite loop is expected.


    No, since your outer loop is an infinite loop, it will just loop again to print the menu and read input, without printing "enter the number again:\n". As you probably want to print that "error message", I noted that I would "just write the if-else chain that you wrote inside the inner for loop", i.e., get rid of the inner for loop entirely as in whiteflags' example in post #12.
    may be i should have included exit in one of those options since infinite loop is not good :/..this would have been a better program if exit was one of thos options.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program I made - Question about code
    By PersianStyle in forum C++ Programming
    Replies: 22
    Last Post: 07-13-2009, 04:09 AM
  2. little problem with a simple shell program i made.
    By keira in forum Linux Programming
    Replies: 2
    Last Post: 02-26-2008, 11:42 PM
  3. Program I made!
    By epidemic in forum C++ Programming
    Replies: 13
    Last Post: 04-06-2007, 09:34 PM
  4. who has made simple searching engine?
    By oshindeler in forum C++ Programming
    Replies: 2
    Last Post: 08-03-2006, 10:41 AM
  5. What's the best program you have ever made?
    By Kevin.j in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 10-02-2002, 11:29 AM

Tags for this Thread