Hi -- After lots of searching I cant seem to find an answer (well one that I understand at least..)

I want to make the variables wobstacle, waisle, and wturn ONLY numbers where a user cannot input a letter and break the program. I was thinking of something along the lines of if wobstacle ! isnan then return them back to the beginning...But the syntax is incorrect and doesn't seem as though I can use it this way...any help?

Thanks

#include <iostream.h>
#include <math.h>
int main()
{
    double wobstacle,   //Width of obstacle
           waisle,      //Width of aisle
           wturn;       //Width of turn
    bool flag;         
         
//Calculate if the turn meets US
flag=true;
   cout << endl << "Please enter the width of the OBSTACLE, AISLE, and "
<< "TURN in inches. (With spaces inbetween): ";
cin >> wobstacle >> waisle >> wturn;

while ( flag == true )
{
//Check for not a number:
if ( wobstacle ! nan && waisle ! nan && wturn ! nan )
              {
                     flag=false;
              }
   else
        cout<< endl << "Please enter a number";
        cin >> wobstacle >> waisle >> wturn;
        flag=true;
      
}     

    if(wobstacle >= 48 && waisle >= 36 && wturn >= 36
                 || wobstacle < 48 && waisle >= 42 && wturn >= 48)
    {
                 cout << "The design specifications meet the UFAS requirements";
    }
    
    else 
         cout << "The design specifications do not meet the UFAS requirements";

system("pause");
return 0;    
}

Hi -- After lots of searching I cant seem to find an answer (well one that I understand at least..)

I want to make the variables wobstacle, waisle, and wturn ONLY numbers where a user cannot input a letter and break the program. I was thinking of something along the lines of if wobstacle ! isnan then return them back to the beginning...But the syntax is incorrect and doesn't seem as though I can use it this way...any help?

Thanks

#include <iostream.h>
#include <math.h>
int main()
{
    double wobstacle,   //Width of obstacle
           waisle,      //Width of aisle
           wturn;       //Width of turn
    bool flag;         
         
//Calculate if the turn meets US
flag=true;
   cout << endl << "Please enter the width of the OBSTACLE, AISLE, and "
<< "TURN in inches. (With spaces inbetween): ";
cin >> wobstacle >> waisle >> wturn;

while ( flag == true )
{
//Check for not a number:
if ( wobstacle ! nan && waisle ! nan && wturn ! nan )
              {
                     flag=false;
              }
   else
        cout<< endl << "Please enter a number";
        cin >> wobstacle >> waisle >> wturn;
        flag=true;
      
}     

    if(wobstacle >= 48 && waisle >= 36 && wturn >= 36
                 || wobstacle < 48 && waisle >= 42 && wturn >= 48)
    {
                 cout << "The design specifications meet the UFAS requirements";
    }
    
    else 
         cout << "The design specifications do not meet the UFAS requirements";

system("pause");
return 0;    
}

C++ doesn't support such syntaxes

if ( wobstacle ! nan && waisle ! nan && wturn ! nan )

and anyway when u r taking input using cin it will only read a double value since the data type of the variables u r reading are of type double. U dont need to validate them like that.

>U dont need to validate them like that.
Actually he needs to. If the user enters any non-numeric character, his program can hang, halt, break or whatever. The worst part is, that the programmer won't be able to display a error message.


>Link
failbit, badbit, goodbit and stuff only makes my code difficult to read and understand.

If at all I am curious about what my user is inputting, I always input it in form of string and just parse it my own way.
I thus get a lot more control over the input I received.

>Link
failbit, badbit, goodbit and stuff only makes my code difficult to read and understand.

I don't believe, so. A c++ programmer should at least know the basics of input stream, and what happens when it fails.

Edited 7 Years Ago by firstPerson: n/a

>U dont need to validate them like that.
Actually he needs to. If the user enters any non-numeric character, his program can hang, halt, break or whatever. The worst part is, that the programmer won't be able to display a error message.


>Link
failbit, badbit, goodbit and stuff only makes my code difficult to read and understand.

If at all I am curious about what my user is inputting, I always input it in form of string and just parse it my own way.
I thus get a lot more control over the input I received.

ya agreed.......
its the best practice to take an input as string so that u have more control over what the user inputs, but also i guess "cin" might be intelligent enough to know whether the input is valid or not and if not then i think we can trap it and ask the user to re-enter his input.

>but also i guess "cin" might be intelligent enough to know whether the input is valid or
>not
cin is intelligent enough that it won't at least calmly accept a invalid input. But when the input is not valid, say you entered a character when a integer was expected, cin gets 'mad' and does quite arbitrary things. For sure you could check the fail bit flags to validate your input but then..... its up to you.
The bottom line is: cin, by default do not handle invalid input so cutely. You definitely need to put an effort to make it behave properly.

The bottom line is: cin, by default do not handle invalid input so cutely.

Its c++, is there anything cute about it?

>Its c++, is there anything cute about it?
Yes. I find std::strings , std::vectors very cute. I have a less of headache when I use these (provided I am not concerned so much about the program speed).

>Its c++, is there anything cute about it?
Yes. I find std::strings , std::vectors very cute. I have a less of headache when I use these (provided I am not concerned so much about the program speed).

Oh, so you like the followings :

std::vector<vector<float>> vec2d; //might be compiler error to some
vec2d.resize(100,vector<float>(100,0)); // resizes it to 100 x 100

And string in its beautified form is alright, but its true form is ugly,
and definitely not cute?

Or you can typedef everything, and add more complexity to your code

And no matter how cute it might look, one can always make
it much more complex with c++ syntax, particularly n00bs.

come on, c++ is not cute, its simply a Beast

This article has been dead for over six months. Start a new discussion instead.