Wednesday, 15 July 2015

c++ - Why does this not ask user for another input? -


This part of a large project is now asking the user for a string, how many words are counted in it, print the # words Ask the user if they want to do it again, if they want, ask for the second string, and so on. But it works just fine for the first time, after this, it answers yes / no as a test / string. For example: I like coding I 3. Again? Yes no Yes. 1. Again? Yes / No ... Can anyone tell me how to fix this disturbance?

  #include & lt; Iostream & gt; #include & lt; String & gt; using namespace std; String origin [10] = {"hello", "sir", "madam", "officer", "stranger", "where", "is", "the", "mine", "yours"}; The translation of the string [10] = {"Ahoy", "mate", "proud beauty", "folg bloggart", "scurvy dog", "white", "b", "and" "," m "," yier "}; String input; string ans; boole pledge () {cout  yes / no:"; cin> "ans; if (ans.compare (" yes ") = = 0) {return true;} if (ans.compare ("no") == 0} {return false;}} int getNumOfWords (string input) {int numOfSpaces = 0; string current; for (int i = 0 ; I & lt; input.length (); i ++) {current = input.at (i); if (current.compare ("") == 0} {numOfSpaces ++;}} NumOfSpaces + 1;} Zero play (string input) {int numOfWord S = getNumOfWords (input); Cout & lt; NumOfWords & lt; endl;} zero start () {getline (cin, input); play input;} int main () {playing bulg ; Play {start (); play = playgain ();} while playing); return 0;}  

When the input reads from cin.getline () , a new letter has been omitted in the input stream, so it does not read your C string. cin.ignore () Use boring calling getline ()

  zero start () {cin.ignore (); Gateline (pin, input); Play (input); }  

No comments:

Post a Comment