I have an equation with 4 variables, I am prompting the user to enter each one of these variables, and then the program decides based on what variables are entered, which variables it needs to solve for. For instance, given variable a and b, I need to solve for b and c. I am trying to come up with a way for the program to decide which variables have been entered, and which ones have not been. This is what I have been thinking so far:
int a,b,c,d;
char unknown;
cout<<"****This program decides which variables to solve for****\n;
cout<<"Please enter the known variables below, if a variable is unknown,
please enter a'?'\n"
cout<< "please enter variable a\n";
cin>>a;
cout<< "please enter variable b\n";
cin>>b;
etc....
if (a =='?'){
check b,c,d}
if (b =='?'){
check c,d}
And then running those variables through if statements to determine which variables are present and which aren’t. There has to be an easier way though, these if else if statements have the potential to be ridiculous. If any of you have any advice its much appreciated. Thanks!
The pseudo-code that you’ve posted won’t work, because
>>xwherexis anintwill try to interpret the user’s input as a specification of an integer. So, with the user typing?what happens is that the operation fails,cinis put in a failure state, and further input operations are ignored until or if that failure state is cleared.One way around this is to input a line at a time, into a
std::string, by usingstd::getlinefrom the<string>header. When you have the line of input you can then check whether it’s a question mark (or simpler, just empty). And if not you can then attempt to convert the user’s number specification to anintby using e.g. astd::istringstream(as I recall from the<sstream>header).It can be instructive to make this part work even if you’ll probably discover, as “Potatoswatter” commented, that the problem you’re doing this for can be quite complex.
Cheers & hth.,
– Alf