Infinity loop when variables are entered as non-numeric.

I am a beginner of C++.
I am writing an area-calculating programme;
I find that if the input of the float is non-numeric,
the program will go on forever loop... even after I have checked for its trait.

What should I do to prevent it to loop endlessly?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cstdlib>
#include <iostream>
#include <ctype.h>
using namespace std;

int main()
{   char restart = 'y';
    do { 
    system("cls");
    string select;
    cout << "Please select which area do you want to calculate:" << endl;
    cout << "1. Square" << endl; 
    cout << "2. Exit" << endl << endl; 
    cout << "I would like to select "; 
    cin >> select;  
     
   if(select=="1")
   { system("cls");
     cout << "Your choice is ";
     cout << select; 
     float length;
     cout << endl << "You have selected to calculate the area of Square." << endl << endl; 
     cout << "The length of the side: "; 
     cin >> length; 
     if (static_cast<int>(length) == length && length>0)
        {cout << endl << "The area of square = " << length*length << endl << endl; 
         cout << "Restart? (If you want to restart, please input a 'y'.)" << endl;
         cout << "Otherwise you can input anything to exit.  " << endl << endl;
         cin >> restart;
        }
     else 
        {cout << endl << "Sorry, the side of any geometric figure should be positive number." << endl ;
         cout << "You are referring to the main page." << endl << endl ;
         system("Pause");
         restart='y';
        }
   }
   
   else if(select=="2")
   { system("cls"); 
      cout << "Thanks for using this system!" << endl;
      system("pause");
      return 0; 
    } 
    
     else
    { system("cls"); 
      cout << "You have inputted a wrong instruction!" << endl;
      cout << "Restart? (If you want to restart, please input a 'y'.)" << endl;
      cout << "Otherwise you can input anything to exit.  " << endl << endl;
      cin >> restart;
    } 
   
   }while (restart =='y' || restart=='Y'); 
}
When I enter an alphabet for the "length",
it correctly goes to a warning.

However, it loops forever after pressing enter again,
how to solve it?
I would use getline, and check if the strings are of size 1. For integer values, look at this for converting strings into other numeric types:

look at the "numeric conversions" section here:
http://en.cppreference.com/w/cpp/string/basic_string

I should note that if the string doesn't represent a numeric type, it will throw an exception, or segfault. You can just iterate over the string, make sure there's one dot, and that there are no letters or other special characters. For ints, you can just make sure there's only numbers in the string.

Good luck!
Last edited on
Once you enter an alpha character for length, the badbit will be set on the stream and it will stop giving characters. Try adding cin.clear() before line 15.
Topic archived. No new replies allowed.