Wrong cin Input - Program Getting Out of Control

Sep 6, 2011 at 1:01pm
I'm new on C++ programming and I tried to write a program to randomly generate some numbers as "Dice Roller". I requested the user to insert the limit of the roll by "cin", and the program works fine, but when the input is anything else that "short int", the program gets out of control and repeats itself. Here is the code:

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main () {

    short limit, roll;
    srand ( time (NULL) );

          while (int const repeat = 100) {

          cout << "Enter the maximum limit: ";
          cin >> limit;
          
//Error handling
              if (cin.fail() == true ){
                                cout << "Please enter an integer." << endl << endl;
                                cin.clear();
                                continue;}
              else if (limit <= 0) {
                                cout << "Please enter a number greater than \"0\"" << endl;
                                continue;}
          roll = rand()%limit+1;
          cout << "You have rolled: " << roll << ". Limit: " << limit << "." << endl << endl << endl;}
          
return 0;
}


Any ideas how I can fix this?
Last edited on Sep 6, 2011 at 1:19pm
Sep 6, 2011 at 1:59pm
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>

using namespace std;

int main () {

    short limit, roll;
    srand ( time (NULL) );

          while (int const repeat = 100) {

          cout << "Enter the maximum limit: ";
          cin >> limit;
          
//Error handling
              if (cin.fail() || !isspace(cin.get())){ // If the reading operation stopped on unusual character (not space)
                                // To prevent such input as '123abc'
                                cout << "Please enter an integer." << endl << endl;
                                cin.clear();
                                while(!isspace(cin.get())); // Throw all useless characters from this input
                                continue;}
              else if (limit <= 0) {
                                cout << "Please enter a number greater than \"0\"" << endl;
                                continue;}
          roll = rand()%limit+1;
          cout << "You have rolled: " << roll << ". Limit: " << limit << "." << endl << endl << endl;}
          
return 0;
}
Last edited on Sep 6, 2011 at 2:01pm
Sep 6, 2011 at 8:08pm
Thank you for the solution. Now the program displays the "Please enter an integer." message and continues the loop instead of repeating itself and getting out of control. As a novice, I couldn't fully comprehend the logic of how the fix codes detect the wrong type of input and throw it, and I'm going to do research on them.

Thank you again!
Last edited on Sep 7, 2011 at 4:43am
Topic archived. No new replies allowed.