Thread: beginner - how do i allow more then one user input before my consol closes

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    6

    beginner - how do i allow more then one user input before my consol closes

    hi, my names nathan and im new to C++ but have touched on some basic coding when i was younger.

    my program is very very basic as i only started learning a few days ago but i am currently stuck on how to get my program to do what i want it to do.


    i want to be able to select a option from the menu, (like a game menu) then have that open up a new command window etc etc.

    i'm also not sure on how to get my code to stay open and not close after one user input.

    im not looking for some one to code it for me, i'm just looking for some one with experience to set me on the right path as i am teaching my self.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame()
    {
        cout<<" Play New Game ";
    }
    void loadgame()
    {
        cout<<" Load Saved Game ";
    }
    void settings()
    {
        cout<<" Settings ";
    }
    void exit()
    {
        cout<<" Exit ";
    }
    
    int main()
    {
        int userinput;
        cout<<"#######################"<<"\n";
        cout<<"# 1. Play Game        #"<<"\n";
        cout<<"# 2. Load Saved Game  #"<<"\n";
        cout<<"# 3. Change Settings  #"<<"\n";
        cout<<"# 4. Exit             #"<<"\n";
        cout<<"# - Selection:        #"<<"\n";
        cout<<"#######################"<<"\n";
        cin>> userinput;
        switch ( userinput ) {
    case 1:
        playgame();
        break;
    case 2:
        loadgame();
        break;
    case 3:
        settings();
        break;
    case 4:
        exit();
        break;
    default:
        for ( userinput;  != 1 || 2 && 3 && 4; userinput++ ) {
            cout<<" Sorry try again. "<<"\n";
            cin>> userinput;
        }
        break;
        }
    cin.get();
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by blackwolf
    i'm also not sure on how to get my code to stay open and not close after one user input.
    This is covered in an FAQ: How do I Stop my (Windows) Console from disappearing everytime I run my program? You have pretty much implemented the second suggestion, except that the blocking read only reads one character, which would not help if there are characters left in the input buffer. Consider cin.ignore() with appropriate arguments instead, though personally I recommend the first suggestion.
    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
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    i've already read that.. hence why you can see i've implemented the second suggestion, and i am running it through command line.

    can you explain what you mean by cin.ignore() with appropriate arguments?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by blackwolf
    i've already read that.. hence why you can see i've implemented the second suggestion, and i am running it through command line.
    Oh, my mistake. I did not notice that you did not implement a loop to repeat. Basically, that is what you need to do: implement a loop that wraps almost your entire main function body such that it keeps looping until the user enters input to request for the program to end.

    Quote Originally Posted by blackwolf
    can you explain what you mean by cin.ignore() with appropriate arguments?
    Change this:
    Code:
    cin.get();
    to:
    Code:
    cin.ignore(10000);
    But it will not help you in this specific case since you simply don't have a main loop.
    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

  5. #5
    Guest
    Guest
    By the way: multiline string literals will concatenate in your code, which might not be self evident, so you can do:
    Code:
    cout << "#######################\n"
            "# 1. Play Game        #\n"
            "# 2. Load Saved Game  #\n"
            "# 3. Change Settings  #\n"
            "# 4. Exit             #\n"
            "# - Selection:        #\n"
            "#######################\n";

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    thanks for the help guys / girls, much appreciated I've been reading up lots and lots and at the moment there seems to be so many different ways to code the exact same thing, some more efficient then others.

    -adrian, little things like your comment is really helpful as i didn't know you could make it do that. saves a lot of time / space / hassle
    Last edited by blackwolf; 01-09-2016 at 01:00 AM.

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    6
    which type of loop would i use to achieve that?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by blackwolf View Post
    which type of loop would i use to achieve that?
    It depends on your goal, but I would suggest a while loop.
    at the moment there seems to be so many different ways to code the exact same thing, some more efficient then others.
    There is a reason for this, and it is because each loop has something different to offer.

    To be clear, the basic loop structure is while. Once you understand how it works, then you can make literally every loop into a while if you wanted.

    do-while's claim to fame is that it will run exactly once. This is because of the body, which appears first, is also executed first, then the condition is checked. So, say your goal was to generate a number between 1 and 100. Well, you know that you will need to call rand() at least once, so this could lead to code like:
    Code:
    // Call rand() repeatedly until we get a number we want.
    do {
        result = rand();
    } while (result < 1 || result > 100);
    (Advantage of this is that it works with rand()'s actual period to get a number, at the cost of time, instead of faster smooshing of the result, which is often implemented poorly. See here. )

    When you write enough while loops, you begin to notice a pattern:
    Code:
    size_t i = 0; // initialize a starting point 
    while ( i < arrayN ) { // work until this is satisfied
        printf("array[%2d] = %d\n", i, array[i]);
        ++i; // update your initial variable 
    }
    This is a pattern that happens frequently in code. You are either counting something, or maybe something needs to be done at every step along the way (for some vague notion of a step.) The for loop was invented to highlight this in the first line and leave the actual work in the body.
    Code:
    for (size_t i = 0; i < arrayN; ++i) {
       printf("array[%2d] = %d\n", i, array[i]);
    }
    Eventually when you get used to programming a little bit, you will focus on writing the easiest loops to read. You will notice what you are doing and be able to choose between while and for and do-while. But if you write a while because it's easy, no one is going to be too bothered by it. They might just show you the format they would have used instead.

    I'm sure this is covered in other places like the tutorials, but maybe this helped a little more.
    Last edited by whiteflags; 01-09-2016 at 02:25 AM.

  9. #9
    Registered User
    Join Date
    Jan 2016
    Posts
    6

    Eventually when you get used to programming a little bit, you will focus on writing the easiest loops to read. You will notice what you are doing and be able to choose between while and for and do-while. But if you write a while because it's easy, no one is going to be too bothered by it. They might just show you the format they would have used instead.

    I'm sure this is covered in other places like the tutorials, but maybe this helped a little more.
    this helped very much i've found that the tutorials on this website do not explain things in enough detail or in simple terms which makes you need to read it 5-6 times over just to understand it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-20-2015, 01:03 PM
  2. Replies: 3
    Last Post: 12-20-2015, 01:03 PM
  3. Beginner User Input Question
    By anchorblue804 in forum C++ Programming
    Replies: 10
    Last Post: 09-10-2012, 04:37 PM
  4. Replies: 13
    Last Post: 08-30-2012, 08:51 PM
  5. Beginner question- user input termination
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 02:48 PM

Tags for this Thread