I've had this issue before, and someone suggested using a bool variable. The problem is we haven't "officially" instructed on the use of bool variables, and my instructor is extremely picky about making sure we understand our code. What I'm hoping for is some alternative solution, like writing the code a bit differently instead of adding an additional variable (or at least a variable other than a bool). If not, I would love it if someone could explain in simple terms what a bool variable is an how it works.
The basic code I have is:
1 2 3 4 5 6 7 8 9 10 11
string input;
while (input != "quit")
{
cout << "Enter a string: ";
getline(cin, input);
string pal = string(input.rbegin(), input.rend()); //reverses the beginning and end of input then assigns reversal to pal
if (input.compare(pal) == 0) cout << "It is a palindrome." << endl;
else cout << "It is not a palindrome." << endl;
}
And the issue is that quit is added to the cin buffer and included in the output. The loop exits, but only after quit is tested as a palindrome. I have another code for another program with a similar issue, in which two substrings are swapped and concatenated with a comma and space between, and after entering quit, the comma is added to the buffer and prints at the end as part of the vector.
See if anything in http://www.lb-stuff.com/user-input is helpful. In specific, you should loop on the input operation:while(std::getline(std::cin, input) && input != quit)
By the way, why use .compare() when you can just use ==?
LB, thank you for the info. Unfortunately I'm still having this problem. For that particular assignment I just got rid of the loop since it wasn't necessary. Also, the point of the compare was just to show that we learned the material. Like I said, instructor is super picky.
Anyway, my big problem is that whenever I create a loop like this for example:
1 2 3 4 5 6 7
while (true)
{
getline (cin, input);
if (input != "quit");
cout << input << endl;
elsebreak;
}
The loop always works so that when quit is entered, the loop terminates but quit is still in the cin buffer and I want to prevent that from happening somehow, or at least remove it. Is that possible?
The loop always works so that when quit is entered, the loop terminates but quit is still in the cin buffer and I want to prevent that from happening somehow, or at least remove it. Is that possible?
That shouldn't be happening. If was still in the buffer then input shouldn't be equal to quit and the loop shouldn't terminate. Can you show the whole program that is producing the problem.
This is the one that I am working on right now, but I've had this issue consistently. In this circumstance, the input names are saved to file, and quit isn't, but when displayNames is called, quit is displayed first, then the file. This on specifically is also creating a "," character in the saved file when quit is entered. It's like the part of the if statement is executing when quit is entered. Which obviously it isn't, but I can't figure out why quit is being output but not saved to file, and why "," is being created for quit when the if statement shouldn't be executing at all.
You are passing name as a parameter by reference (or address, whatever you want to call it) instead of by value. This means anything done to name in your function is done to the actual name variable in your main (since they're the same variable).
In the function, you read input into name until name == "quit". So when you return from this function, name == "quit".
void displayLast(string name)
In this function, you are passing name as a parameter by value. Since name == "quit" in main, this means that in the function name starts with the value "quit". You output name before reading anything new into it. Therefore the first line is obviously going to be "quit". Fix it by using while (getline(namesFile, name)) instead.
Thank you fg109, that actually helps me understand how function calls work a bit better, and quit is no longer displaying.
I am still having the issue with the getNames function in which the comma is being added. I remember for some of my previous programs I was shown how to create a variable, assign it the desired value in the loop, then output it/modify it/remove it/etc. For this, I need to write it to file. I tried simply having the statement namesFile << name << endl; at the end of the loop, but that didn't work for pretty obvious reasons. Would you be able to suggest anything for that?