Telling apart numerical values from letters

Hello everyone. I'm doing the c++ tutorial found on this website and I'm making this simple program, adding/changing it as I learn new stuff from the tutorial. And here it is:

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 main ()
{ 
  float a, b, c, perimeter, area; 
  string input;
start:
      
      cout << "\nEnter the value of the first side of the right triangle: "; 
           getline (cin,input);
           stringstream(input) >> a;
                           
      cout << "\nEnter the value of the second side of the right triangle: ";
           getline (cin,input);
           stringstream(input) >> b;              
                  
      area = (a * b) / 2; 
      cout << "\nThe area of this right triangle is: " << area << " square units.\n";
          
      c= sqrt(a*a+b*b);
      perimeter = a+b+c;          
      cout << "\nThe perimeter of this right triangle is: " << perimeter << " units.\n";
      cout << "===================================================================\n"; 
      
          goto start;     
}



As you can see its a simple thing, mainly playing around with the Pythagarean theorem... It works, but I need some help with it. How can I get the program to tell apart wether the user has entered a number, like hes supposed to, or something nonsensical, like a statement (I like pies!)? I want to make it so that if the user enters letters/words he gets a message asking to re-enter the value, or something along those lines.
I've tried doing this:

1
2
3
4
5
 cout << "\nEnter the value of the first side of the right triangle: "; 
      
           cin >> a;
           if ((!(a<0) && !(a>0)) && !(a=0))
                     cout << "The entered value is incorrect, please enter a digit./n";



I reasoned that if the entered value is neither less than zero, more than zero or equal than zero, then its not a number, thus its a letter or statement... But when executed the program would go crazy... Thanks for any help in advance.
Try:
if( isdigit(a) )

Note: This is a function of <cctype>

I believe that stringstream automatically checks to make sure that the value in it can be stored into the target value.
Last edited on
I've had some interesting effects... Though it still doesint work as it should.

If I try

1
2
3
4
5
cout << "\nEnter the value of the first side of the right triangle: "; 
      
           cin >> a;
           if( isdigit(a) )
                     cout << "The entered value is incorrect, please enter a digit.\n";


Then numerical values are ok, however words and letters still make the program go balistic. It starts looping, continously asking me to enter the values, not waiting for them, giving the previous answer, and asking the value again etc.

If I try

1
2
3
4
5
cout << "\nEnter the value of the first side of the right triangle: "; 
           getline (cin,input);
           stringstream(input) >> a;
           if( isdigit(a) )
                     cout << "The entered value is incorrect, please enter a digit.\n";


Both numbers and letters/words are ok, except if I enter letters and words, it just gives me the previous answer (or if there was no previous answer, just says 0). For the hell of it, I've tried:
1
2
3
4
5
cout << "\nEnter the value of the first side of the right triangle: "; 
           getline (cin,input);
           stringstream(input) >> a;
           if(! isdigit(a) ){
                     cout << "The entered value is incorrect, please enter a digit.\n";


Though, this way both letters and numbers say "The entered value is incorrect...."
Look here for type safe input:
http://www.cplusplus.com/forum/articles/6046/

Edit: To get a number:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// How to get a number.
 int myNumber = 0;

 while (true) {
   cout << "Please enter a valid number: ";
   getline(cin, input);

   // This code converts from string to number safely.
   stringstream myStream(input);
   if (myStream >> myNumber)
     break;
   cout << "Invalid number, please try again" << endl;
 }
 cout << "You entered: " << myNumber << endl << endl;


Notice the one line if statement. If it is true, the task completed properly, it will break from the loop. If it returns false, the string could not be stored into the variable it will ask for it again.

Since you are using floats, you will need to make the variables floats. (You probably knew that but, just in case)
Last edited on
Thanks for your help, everything is working just as intended now. I've adapted that code to suit my purposes, though there is still one minor problem.
1
2
3
4
5
6
7
8
9
if (myStream >> a)
           
              int k=0;
              
           else {
                     
                     cout << "\nThe entered value is incorrect, please enter a digit.\n";
                     goto start;
                     }


As you can see I entered a K variable right after the if function. That was because, if I understood my compiler correctly, the function needs something to do when the "if (myStream >> a)" is true. Though this K variable is otherwise pointless, so my question would be, is there a "do nothing" option? Or something like it?
I'd imaging you could use an empty block like
1
2
3
4
5
if (myStream >> a) {
} else {
    cout << "\nThe entered value is incorrect, please enter a digit.\n";
    goto start;
}

or better yet, just have it check if the value is not true:
1
2
3
4
if (!(myStream >> a)) {
    cout << "\nThe entered value is incorrect, please enter a digit.\n";
    goto start;
}
Last edited on
Don't ever use goto. That makes code reading and debugging a pure nightmare.

In answer to your question Emergency, Nothing has to be executed inside a conditional.

For future reference: When you are unsure, test it!

It would have taken less than 10 seconds to get the answer to your question if you would have commented out the line and hit compile.

Last edited on
But I did try to leave it empty, and I got some errors. Though I just copied Gumbercules's code, and it seems to work, so probably it was my own clumsyness. Sorry, I'm still very fresh.

I thank you both sincerely. You guys really helped me with theese first steps in c++ ;)

EDIT: And I'll try to loose the goto's at some point later. At the moment, they're easier to comprehend for me than the while function, or whatever else I'm supposed to replace them with.
Last edited on
Topic archived. No new replies allowed.