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.
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
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