1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

String problem with c++ game

Discussion in 'Programming & Webmastering' started by Aleksander, Sep 24, 2011.

  1. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    Hi guys!
    I am having a little problem with my latest game in c++
    i know how to give an error if the user types in a wrong number,
    but i dont know how to make it if the user types in a string!
    Anyone can help?
    This is the code of the error so far:
    Code:
    try
        {
            if (number < 1 || number > 9)
            {
                throw 1;
            }
        }
        catch(int x)
        {
            cout << "\n You typed a wrong number! ERROR: " << x << endl;
            game();
        }
    
     
  2. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    Tried this one:

    Code:
    try
        {
            if (number < z || number > a)
            {
                throw 1;
            }
        }
        catch(string x)
        {
            cout << "\n You typed a wrong number! ERROR: " << x << endl;
            game();
        }
     
  3. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    I think you can use something like this ...
    Code:
    if (static_cast<int>(number) == number)
    {
        // it's an integer
    }
    else
    {
        // it's not an integer
    }
    
     
    Aleksander says thanks.
  4. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    thanks :)
    Where did you get that command btw?
    It sounds very logic to me
     
  5. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    I'm not sure what you are asking.
    static_cast<> is a C++ operator.
     
  6. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    Never mind. Seems i can't find the real solution of the problem.
    Yours does the same thing as the one i wrote :p
    Anyway, nice programming trick :)
    (i was asking where is the site where you learnt it)

    The real problem is that if i type a char or string instead of a number, the game never ends restarting
    I have this problem with all my games and this is the only thing i have not learned yet!
    (i mean, fixing this problem, my games, will not have a single "bug")
     
  7. ctrain New Member

    Joined:
    Jan 12, 2010
    Messages:
    393 (0.22/day)
    Thanks Received:
    72
    cin is in the failure state and will just return immediately, you need to clear the fail state and wipe the input stream.

    something like this:
    Code:
    int num;
    
    std::cin >> num;
    if(cin.fail())
    {
    	std::cout << "oh shit";
    	
    	std::cin.clear();							//clear the failure state
    	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 	//clear the input stream
    }
    
    else
    	...
     
    Last edited: Sep 25, 2011
  8. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    What should i put in this row when i have numbers from 1 to 10 for example?
    Code:
    cin.ignore(numeric_limits<streamsize>max(), '\n');
     
  9. de.das.dude

    de.das.dude Pro Indian Modder

    Joined:
    Jun 13, 2010
    Messages:
    7,814 (4.90/day)
    Thanks Received:
    2,077
    have you tried using the isdigit() function?
    header is ctype.h
     
  10. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    Same problem with the isdigit() lol
    All things i have tried, all worked the same :p
     
  11. de.das.dude

    de.das.dude Pro Indian Modder

    Joined:
    Jun 13, 2010
    Messages:
    7,814 (4.90/day)
    Thanks Received:
    2,077
    LOL something is borked i guess. things like this happen.
     
  12. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    Are you calling the game() method on a bad input?
    That's what restarting the game, no?

    If not, I'm confused.

    Are you trying to do something like this?

    Code:
    While input does not equal the exit command
    {
      Get input
      if (input is valid)
      {
          Do something with valid input and ask for next input
      }
      else
      {
          If (input does not equal the exit command)
          {
              Throw an error an error and ask for a valid input
          }
       }
    }
    
     
  13. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    The problem is:
    If the user types in a wrong integer like 3980218408....
    that throws an error and the game goes back
    If the user types in a string or char, that makes the game
    restart infinitely
    See the throw i have programmed in the first post

    Also, your prototype is wrong as i don't mean to exit the game
    The player could be anywhere, but if he doesn't press an integer
    he looses the game and has to restart!
     
  14. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    The pseudocode that I wrote would ask a player for input.
    If the input was correct (valid) it would do something and then ask for the next input.
    If the input was invalid, it would inform the player and then ask for input again.
    If the player entered the "exit" string (whatever that may be) the game would quit.

    Can you post your whole input code routine?
     
  15. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <fstream>
    #include <string>
    #include <ctype.h>
    
    using namespace std;
    
    int main()
    {
        game();
    }
    
    int game()
    {
        cin >> number;
    
        try
        {
            if (number < 1 || number > 7)
            {
                throw 1;
            }
        }
        catch(int x)
        {
            cout << "\n You typed a wrong number! ERROR: " << x << endl;
            game();
        }
    
    This is all needed, cuz the whole programming is 600 lines
    I don't really think you will find anything with the code anyway.
     
    Last edited: Sep 25, 2011
  16. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    Have you tried recursion*?

    *Calling method x within method x.
     
  17. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    You mean like this?
    Code:
    catch(int x)
        {
            cout << "\n You typed a wrong number! ERROR: " << x << endl;
            game();
        }
    
    It is "recursioned", i just deleted like 70 lines of code in the post
    If i put a string or char, the recursion continues infinitely (or a double || float)
     
    Last edited: Sep 25, 2011
  18. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    You don't want recursion in this case as you are just filling the stack and heap for no good reason. You should not call the game() method from within itself just for bad input.
     
  19. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    If i don't put the recursion, the game ends!
     
  20. temp02 New Member

    Joined:
    Mar 18, 2009
    Messages:
    493 (0.24/day)
    Thanks Received:
    166
    You really shouldn't call the game function again when some error occurs, you would be increasing the call stack for no valid/good reason, use Kreij's approach and loop the game function call on main.
    Also add "cin.clear();" to the catch, like so:
    Code:
    catch(int x)
    {
      cout << "\n You typed a wrong number! ERROR: " << x << endl;
      cin.clear(); // flush the input error buffer
    }
     
  21. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    How about having x be a string (instead of an int) then try to parse it into an int? If it doesn't parse it should go to the catch.

    EDIT: by x I mean the number you're inputting.
     
    Aleksander says thanks.
  22. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    If you "cin" to an integer, it will automatically go into a fail state (can't convert) as ctrain pointed out. You can use this to check to see if the inputted value is a string as opposed to an integer.

    If the inputted integer is out of range, a simple range check will do.
     
  23. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    LOL
    tried razaron's suggestion it works but ends in an unusual way!
    I created 2 try functions, one with int x and one with char x
     
  24. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    Why and what 2 try functions?
     
  25. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.82/day)
    Thanks Received:
    304
    Code:
    try
        {
            if (number < 1 || number > 7)
            {
                throw 1;
            }
        }
        catch(int x)
        {
            cout << "\n You typed a wrong number! ERROR: " << x << endl;
            game();
        }
    try
        {
            if (number < 1 || number > 7)
            {
                throw 2;
            }
        }
        catch(char x)
        {
            cout << "\n You typed a wrong number! ERROR: " << x << endl;
            game();
        }
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page