c++ - repeat code automatically -
i wrote code convert string morse code. code works perfect until try repeating automatically.
whether use "while" or "do while" code runs once , terminates. find out problem is?
int main () { cout<<"enter string: "; char mystr[81]; char ch='y'; while (ch=='y'||ch=='y') { getstring(mystr); toupper(mystr,strlen(mystr)); removespace(mystr); getmorse(mystr,strlen(mystr)); cout<<"to repeat press y/y"; cin>>ch; } return 0; } i added getstring() function
void getstring(char mystr[]) { cin.getline(mystr,81,'\n'); }
after user enters input, press enter. newline character '\n' still in cin stream. need ignore it:
cin >> ch; cin.ignore(numeric_limits<streamsize>::max(), '\n'); //this ignores subsequent characters until newline character
Comments
Post a Comment