c++ - How do I test for user input for equations? -
i have equation 4 variables, prompting user enter each 1 of these variables, , program decides based on variables entered, variables needs solve for. instance, given variable , b, need solve b , c. trying come way program decide variables have been entered, , ones have not been. have been thinking far:
int a,b,c,d; char unknown; cout<<"****this program decides variables solve for****\n; cout<<"please enter known variables below, if variable 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 running variables through if statements determine variables present , aren't. there has easier way though, these if else if statements have potential ridiculous. if of have advice appreciated. thanks!
the pseudo-code you've posted won't work, because >>x
x
is int
try interpret user's input specification of integer. so, user typing ?
happens operation fails, cin
put in failure state, , further input operations ignored until or if failure state cleared.
one way around input line @ time, std::string
, using std::getline
<string>
header. when have line of input can check whether it's question mark (or simpler, empty). , if not can attempt convert user's number specification int
using e.g. std::istringstream
(as recall <sstream>
header).
it can instructive make part work if you'll discover, "potatoswatter" commented, problem you're doing can quite complex.
cheers & hth.,
– alf
Comments
Post a Comment