I had this piece of code with the problem that it used the first character after the first loop (the first loop was fine)
do{ cout << endl << "command:> "; string cmdStr1=""; cin.ignore(); getline(cin, cmdStr1); cout << "cin= " << cmdStr1 << endl;
The output was:
command:> pos
cin = pos
command:> pos ... from the 2nd cycle, he began to delete the 1st character
cin = os
...
If "cin.ignore ();" was commented, then this led to a βsegmentation errorβ:
command:> cin =
Segmentation error
The solution works for me:
To move "cin.ignore ();" lines immediately before the do-while loop.
cin.ignore(); do{ std::cout << endl << "command:> "; std::string cmdStr1=""; std::getline(std::cin, cmdStr1); std::cout << "cin= " << cmdStr1 << endl;
The output was:
command:> pos
cin = pos
command:> pos
cin = pos
...
...
PS It was very difficult to put the code here ... I am disappointed that I continue to cooperate.
source share