objective c - ObjC: scanf skipped in do/while loop -
i'm starting teach myself objective c , have been fooling around simple console based calculator. start i'm asking user operation perform, 2 numbers perform operation on, , print answer after it's calculated.
that part seems work fine - next piece i'm trying implement allow user keep making calculations until decide no longer want to. thinking approach put running of calculator inside do/while loop @ end of checking see if user wants continue via 'y' or 'no' via scanf();, , check see user choose respond with. i'm encountering though @ end of first pass, request input via scanf(); gets ignored , program terminates.
i hoping :) i've done due diligence best via troubleshooting, online research(found on white space issue scanf(); didn't help), , guessing date i've been unsuccessful. below main section of code:
int main (int argc, const char * argv[]) { nsautoreleasepool * pool = [[nsautoreleasepool alloc] init]; float answer; char op,repeat; nslog(@"welcome sexy calculater, (_(_)!!"); //don't mind me being weird calculater *deskcalc; deskcalc=[[calculater alloc] init]; { [deskcalc clear];//clear calc op =[deskcalc getoperator]; //get +,-,*,or/ [deskcalc getnumbers]; //get 2 numbers [deskcalc calculate: op]; answer=[deskcalc accumulater]; nslog(@"your answer %f!", answer); nslog(@"would calculate again(y/n)?"); scanf("%c",&repeat); //nslog(@"you typed: %c", repeat); } while (repeat=='y'); [deskcalc release]; [pool drain]; return 0; }
thanks in advance help. if haven't provided enough information apologize! please let me know , i'll more willing provide more. again all!
you want have @ scanf skips every other while loop in c
you need work ensure scanf() read newline that's on stdin after user enter 'y', 'n', or whatever other streams of keys hit before pressing enter.
this other posting seems relevant: why scanf() causing infinite loop in code?
Comments
Post a Comment