String problem with c++ game

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:
1
2
3
4
5
6
7
8
9
10
11
12
try
    {
        if (number < 1 || number > 9)
        {
            throw 1;
        }
    }
    catch(int x)
    {
        cout << "\n You typed a wrong number! ERROR: " << x << endl;
        game();
    }


Pls help!
Let us assume you read in the number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
      int nNumber;
      std::cout << "Please enter a number: ";
      std::cin >> nNumber;

      // to check if something worked or not.
      if(std::cin.good())  // this would check to see if anything else than the integer was entered.
      {

             // do your exception here....
      }
      else
      {
            std::cout << "you didn't enter a number!!" << std::endl;
      }


You can find answers like this all over the board, it isn't the first time it has been asked.
You can add:

1
2
3
4
   else if(cin.fail())
  {
        throw 
 }

Wait, if the user enters:

"3sadksdk"

Would this be valid? Do you want to just check the first value?
I get the feeling you're not testing the code we have given you.

"3sadksdk" would be a string, not an integer. This would make cin.good() would return a false, and cin.fail() would return a true, since I couldn't read this into a integer type.

If I wanted to catch the number in the string, I would use a different approach.

You asked about a number, which would be an integer or float. What you gave was a number within a string. They are two different things and would be handled differently.
I don't know the else if(cin.fail())
Would it be like this:
1
2
3
4
else if(cin.fail(number))
{
throw...
}

???

I tried it, the problem persists
The game restarts infinitely!
Last edited on
can you show me read in structure? and how you process it? I would assume it is on a loop of some form, if it show me that too. I figure it wouldn't be too long and won't reveal that much about your game.

cin.good() and cin.fail() take no paramerters.
Last edited on
The game is 600 lines of code man

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
#include <ctype.h>

using namespace std;

int game();
int number;

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();
    }
Last edited on
I am starting to suspect the reason for the endless loop is the lack of knowledge of setting up exceptions.

 
catch (int x)


This might be reacting to any integer. Usually you throw an exception class, not a value, to save some confusion in this situation.

and why you didn't do:

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

int game()
{ 
         bool good = false;
         while(!good)
         {
              cin >> number;
              if(cin.good())
             {
                 if( number > 0 && number < 8)
                 {
                        good = true;
                 }
                 else
                 {         
                       cout << "\n You typed a wrong number! ERROR: " << x << endl;
                 }
             }
             else
             {
                  cout << "\n You didn't enter a number!" << endl;
             }
       } // while
       return number;
}


There was no reason to make this mess recursive, which you did. All you wanted was a valid number.
Last edited on
after reading a bit more on exceptions myself your exception block it might look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int number = 0;

try
{
     cin >> number;
     
     if(number < 1 || number >7)
     {
           throw 200;
     }
}
catch(int e)
{
       cout << "\n You typed a wrong number! ERROR: " << e << endl;
}
catch(ios_base::failure&)
{
       cout << "\n You didn't type in a number!!" << endl;
}
catch(...)
{
       cout << "\n I don't know what happened" << endl;
}


This might get you to what you were trying to get to a little better
Topic archived. No new replies allowed.