C ++ getline or cin does not accept string with spaces, I searched google and I'm still at a dead end!

First of all, thanks to everyone who helps me, it is very appreciated!

I am trying to save a string with spaces and special characters in MessageToAdd .

I am using getline (cin,MessageToAdd); , and I also tried cin >> MessageToAdd; .

I'm so dumb! When I enter the sample input

Test

Everything works as intended. However, if I used

Test test

The entire console will flash quickly until I press Ctrl C.

My variable input style at the top, which I mentioned, is deprecated. Please forgive me, as I am still teaching myself and this is just a habit. I will change my style soon after I allow it :)

 void AddMessage() { ifstream myReadFile; string str; string MessageToAdd; string myMessages[10]; int i; // of course my famous i static string rowHtmlCloseTags; static string rowHtmlOpenTags; string replacement; myReadFile.open("C:\\Users\\Andrews\\Documents\\Visual Studio 2010\\Projects\\computerclass\\Debug\\outages.htm",ios::in); i = 0; //the start of my array rowHtmlCloseTags = "</b></td>"; // value that I want to replace with nothing rowHtmlOpenTags = "<td><b>"; if(!myReadFile) // is there any error? { cout << "Error opening the file! Abortingโ€ฆ\n"; exit(1); } if (myReadFile.is_open()) { cout << endl; while (!myReadFile.eof()) { getline(myReadFile, str); if (str == "<tr>") { getline(myReadFile, str); //get the next line cause thats where the <td><b>Outage Message</b></td> is. size_t foundIndex = str.find(rowHtmlCloseTags); //does the sought string exist in this this line? if (foundIndex != str.npos) //if not no position str.replace(foundIndex, rowHtmlCloseTags.size(), replacement); //replace the string else std::cout << "Oops.. didn't find " << rowHtmlCloseTags << std::endl; //else throw a bitch foundIndex = str.find(rowHtmlOpenTags); //does the sought string exist in this this line? if (foundIndex != str.npos) //if not no position str.replace(foundIndex, rowHtmlOpenTags.size(), replacement); //replace the string else std::cout << "Oops.. didn't find " << rowHtmlOpenTags << std::endl; //else throw a bitch myMessages[i]=str; i++; } } } system("cls"); i=0; while (i < 10) { cout << i << ") " << myMessages[i] << endl; i++; if (myMessages[i]=="") { break; } } myReadFile.close(); cout << endl; cout << endl; cout << "Enter the message you would like to see on the reader board.\n"; cout << "Or enter 911 to go back to the main menu: "; cin.ignore(1080); getline (cin,MessageToAdd); if (str == "911") //go back to the main menu { system("cls"); mainMenu(); } else //insert the message into a blank spot in the array { i=0; while (i < 10) { if (myMessages[i].empty()) { myMessages[i]=MessageToAdd; break; } else { i++; } } } //now rebuild the htm file with the new array CreateHtmlFile(myMessages); } 
+4
source share
1 answer

I will tell you one thing that does not immediately match your code, not your specific problem, but nonetheless hairy.

I assume your mainMenu() function calls this. In this case, you are misunderstood that:

 if (str == "911") //go back to the main menu { system("cls"); mainMenu(); } 

will return to your menu. It will not be done. What he will do is call your main menu code again and, ultimately, you will run out of stack space.

I suspect you should loop in mainMenu() , and this code should just use return; , and not recursively call mainMenu() .

This and the fact that I think you should compare MessageToAdd with "911" , not str .


Another thing I would like to do is put the debug temporary code in:

 cout << "DEBUG A\n"; i=0; while (i < 10) { cout << "DEBUG B " << i << "\n"; if (myMessages[i].empty()) { cout << "DEBUG C\n"; myMessages[i]=MessageToAdd; break; } else { i++; cout << "DEBUG D " << i << "\n"; } cout << "DEBUG E\n"; } cout << "DEBUG F\n"; 

and see what is printed. Of course, you can track the execution in the debugger, but it will require you to do the work. If you just post the output (the first 100 lines, if it's huge), then we can probably tell you what's wrong.


Actually, I think your problem is cin.ignore . When I run my code, nothing works, neither Test nor Test Test Test . This is because it ignores the first 1080 characters I'm trying to enter. The proof can be seen when you modify these statements:

 cin.ignore(1); getline (cin,MessageToAdd); cout << MessageToAdd << "\n"; 

and you get est output when you type Test .

Take out the ignore line and try again. I am not sure about this, since you seem to indicate that Test working, but I do not see this being correct.


So here is what you need to do (minimum):

  • completely get rid of cin.ignore .
  • use return , not mainMenu() .
  • use if (MessageToAdd == "911") instead of if (str == "911") .
  • let us know how this happens.
+8
source

Source: https://habr.com/ru/post/1314284/


All Articles