testing user input before storing

so lets say i ask a user for input, "type in a number". how do i test it so if it is an integer, i assign it to one variable, then if it is a different type, i assign it to a different variable.

so basically i want to store the input depending on the type.
for example:
int x;
char y;
double z;
boost::rational<int> a;
cout << "enter your number";
so i want to test and if it is an integer, it goes to x
if it is a character, it goes to y
if it is a decimal, it goes to z
and if it is a fraction, it goes to a

basically i want to do this for fraction and integer, so the user input can be stored as a fraction or integer
Well, if you there is no number in the stream and you call cin >> my_int, cin error flag will be set. You could then clear that flag and read a string. You have more fancy needs here so I suggest you read a string and then, for example, if you find a '/' in it it's a fraction, so split it into two strings and convert each into an integer, and if you don't it's an integer.
how would i implement that statement,
if(the inputted string contain '/')
assign it to fraction
else
assign it to an integer
Yes, like that. check algorithm::find, string::find, or do it yourself
Another way:
1
2
3
4
5
6
7
8
9
10
11
12
switch( what_type_am_I( input ) ){
case INTEGER:
  x = str2integer( input );
  break;
case FRACTION:
  a = str2fraction( input );
  break;
case FLOAT:
  z = str2float( input );
  break;
//...
}
Last edited on
if you use std::string then
1
2
3
4
5
6
7
8
9
string str;
//get the input with << or getile
int pos = str.find('/');//search for /
if(pos != std::string::npos){//if found..
   string numerator = std::string(str, 0, pos), denominator = std::string(str, pos+1);//split the string
   //and convert them
}else{
   //convert str
}
i figured it out
for(int i = 0; i < x.length(); i++)
if(! (x[i] >= '0' && x[i] <= '9' || x[i] == ' ') )

because with my program you only have a choice between integer or fraction.

now i got a new problem.
lets say the program is running then it sidetrack to void to do something, how do you go from void to where the program sidetracked.
i dont want it to start all over
1
2
for(int i = 0; i < x.length(); i++)
if(! (x[i] >= '0' && x[i] <= '9' || x[i] == ' ') )
your if has to have a statement after it (break; perhaps?). though this way, for example 1a2 would be considered a fraction..

what does 'sidetrack' mean? if you call a function, when it does what it has to, the program continues from where the function was called.
rational<int> c1;
int c1;
cin >> x;
for(int i = 0; i < x.length(); i++)
if(! (x[i] >= '0' && x[i] <= '9' || x[i] == ' ') )
{ c1 = boost::lexical_cast<rational<int>>(x);

}

for(int i = 0; i < x.length(); i++)
if((x[i] >= '0' && x[i] <= '9' || x[i] == ' ') )
{ stringstream ss(x);
ss >> c7;

}

cout<< c1;
cout << c7;

basically what i want is for the program to run and if it gets an integer which is c7, it outputs it and ignore c1, and vise versa
if it gets a fraction which is c1 it outputs c1 and ignore c7.
but with this, it tries to output both the integer and the fraction


oh and how do you delete or clear a variable.
so like
int x;
x = 100;
delete x and start over x from scratch
Last edited on
Topic archived. No new replies allowed.