what's the best way to achieve this

This is a discussion on what's the best way to achieve this within the C++ Programming forums, part of the General Programming Boards category; Oke, I will change that. But how can i take care of that if a user has a false input ...

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I will change that.
    But how can i take care of that if a user has a false input , he/she gets a error message and then sees the menu again.

    Could i do :"
    Code:
    case 1:
      cout<<"U hebt gekozen voor invoeren uitslagen";
    break;
    case 2:
       cout<<"U hebt gekozen voor het maken van standen";
    break;
    case 3:
       cout<<"U hebt gekozen voor stoppen";
    break;
    default:
       cout<<"U hebt een verkeerde keuze gemaakt";
       cin.get {};
       menu () ;
    }
    Roelof

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Another problem.
    Im changed it to this.
    Code:
    #include <iostream>
    
    using namespace std;
    string input ;
    
    int Menu(){
    cout<<"1) Input game.: "<<endl;
    cout<<"2) Making the rank.: "<<endl;
    cout<<"3) Quit.: "<<endl;
    getline(cin, input);
    return input;
    }
    
    int main(){ // or int main() {
    input=Menu();
    switch (input){
    case 1:
      cout<<"U hebt gekozen voor invoeren uitslagen";
    break;
    case 2:
       cout<<"U hebt gekozen voor het maken van standen";
    break;
    case 3:
       cout<<"U hebt gekozen voor stoppen";
    break;
    default:
       cout<<"U hebt een verkeerde keuze gemaakt";
    }
    // return 0; if you have int main()
    }
    But now every input is default.
    What is the problem now ?

    Roelof

  3. #18
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    do you think it's odd that your switch statement takes an int, yet user input is read into a string?
    goto( comeFrom() );

  4. #19
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Are you saying you are actually running that code? Wow, what compiler are you using. That shouldn't even compile.

    You also need to include the <string> header.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    230
    It was compeling with the gcc compiler.

    I changed everything to this to make it work.
    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    string input ;
    int choice ;
    
    int Menu(){
    cout<<"1) Input game.: "<<endl;
    cout<<"2) Making the rank.: "<<endl;
    cout<<"3) Quit.: "<<endl;
    getline(cin, input);
    choice = atoi(input.c_str());
    return choice;
    }
    
    int main(){ // or int main() {
    input=Menu();
    switch (choice){
    case 1:
      cout<<"U hebt gekozen voor invoeren uitslagen";
    break;
    case 2:
       cout<<"U hebt gekozen voor het maken van standen";
    break;
    case 3:
       cout<<"U hebt gekozen voor stoppen";
    break;
    default:
       cout<<"U hebt een verkeerde keuze gemaakt";
    }
    // return 0; if you have int main()
    }
    Is this better.
    If so, how can I take care that on a wrong choice or when the choice 1 or 2 is ready the menu is displayed again ?


    Roelof

  6. #21
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    You said it in one of your earlier posts...

    a *while* loop might help. Keep asking the user for input, until it is acceptable.
    goto( comeFrom() );

  7. #22
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    934
    Don't use <stdlib.h>, use <cstdlib>.
    Don't make the variables input and choice global, put them inside main and pass them to menu() as parameters.

    If you want menu() to be called more than once, you should use a loop. In this context i would suggest a while loop and a boolean variable to keep track of how long the loop runs.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  8. #23
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Code:
    string input ;
    int choice ;
    
    int Menu(){
    cout<<"1) Input game.: "<<endl;
    cout<<"2) Making the rank.: "<<endl;
    cout<<"3) Quit.: "<<endl;
    getline(cin, input);
    choice = atoi(input.c_str());
    return choice;
    }
    
    int main(){ // or int main() {
    input=Menu(); <--There is something wrong here
    switch (choice){
    Quote Originally Posted by roelof
    how can I take care that on a wrong choice or when the choice 1 or 2 is ready the menu is displayed again ?
    The menu/switch statement needs to be in a loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #24
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    string input ;
    int choice ;
    
    int Menu(){
    cout<<"1) Input game.: "<<endl;
    cout<<"2) Making the rank.: "<<endl;
    cout<<"3) Quit.: "<<endl;
    getline(cin, input);
    choice = atoi(input.c_str());
    return choice;
    }
    
    int main(){ // or int main() {
    input=Menu(); <--There is something wrong here
    switch (choice){


    The menu/switch statement needs to be in a loop.

    Code:
    int foo(){ return 5; }
    
    int main(){
    
        std::string s;
        s = foo();
        return 0;
    }
    compiles fine. Doesn't mean it's good code, but at least it's compilable code.
    goto( comeFrom() );

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    So a while loop and inside a case.
    Then I have to do a case 3 also otherwise 3 will be default.
    Or schould I use several if then

    Roelof

  11. #26
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Is there a reason you have to convert from the string to int? Is using a switch statement a requirement of the assignment? If not, stick to using a string for the input, and use multiple if/elses.

    So what happens after the user enters 1, 2, or 3? Is the whole point of this exercise just to display messages to the user? If so, you could set up the ternary operator as a "switch" statement, and you might get extra-credit!
    goto( comeFrom() );

  12. #27
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello StainedBlue,

    If the user chooses 1 he/she gets a screen where a game can be added to the database.
    2 then the programm makes of the data inputted on 1 a ranking.
    3 then the programm quits.

    Roelof

  13. #28
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    got it.

    then i might suggest:

    Code:
    
    using namespace std;
    
    string get_user_input()
    {   
        string input;
        
        do
        {
            // a. Prompt for input
            
            // b. record input
            
            // c. if input is not 1, 2, or 3, let the user know
            //    their input is not acceptable
            
        } while( // while not 1, 2, or 3
        
        return input;
    }
    
    int main(){
        
        string input = get_user_input();
        
        if( input == "1" ){ /* go to game input */ }
        
        else if( input == "2" ){ /* do whatever */ }
        
        else // quit
        
        return 0;
    }
    goto( comeFrom() );

  14. #29
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    Thank you for the help so far.
    Tommorow I try this and post it so you or someone else can give me input so I can learn C++.
    I think the only way to learn is make your feet wet.

    Roelof

  15. #30
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Next problem.
    Im now trying this piece
    Code:
    int input_games()
    {
    
         string home_team , away_team, score_home_team, score_away_team ;
    
        do
        {cout<< "Home team :";
         getline(cin,home_team);
         cout<< "Away team :";
         getline (cin,away_team);
         cout<< "Score home team :" ;
         getline (cin, score_home_team);
         cout<< "Score away team :";
         getline (cin, score_away_team);
         if (strlen(home_team))!=0 && strlen(away_team)!=0 && strlen(score_home_team)!=0 && strlen(score_away_team!=0) {
         cout<<"You have forgotten to fill in all the fields";
         cin.get{}; }
        while ( strlen(home_team)!=0 && strlen(away_team)!=0 && strlen(score_home_team)!=0 && strlen(score_away_team)!=0);
    
        }
    }
    But the debugger says that the if is wrong.
    What's a better way to look if a user has made a input.
    I get now this messages
    Code:
    C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp||In function 'int input_games()':|
    C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp|20|error: cannot convert 'std::string' to 'const char*' for argument '1' to 'size_t strlen(const char*)'|
    C:\Users\wobben\Desktop\toernooi\toernooi\main.cpp|20|error: expected primary-expression before '!=' token|
    Roelof

Page 2 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-31-2008, 01:26 PM
  2. Best way to achieve this
    By cloudy in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-06-2006, 07:23 PM
  3. Replies: 3
    Last Post: 09-11-2005, 10:13 AM
  4. The more intelligent risks you take in life, the more you'll achieve
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-20-2003, 05:23 PM
  5. Regarding Careers - need advice
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-05-2002, 03:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21