Problem with if... else.

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.
Last edited on
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;
    else break;
}


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?
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
using namespace std;

void getNames(string &name, ofstream &namesFile)
{
	namesFile.open("names.txt", ios::app);
	string first;
	string last;
	cout << "Enter first and last name (quit to stop)" << endl;
	while (getline (cin, name) && name != "quit")
	{
		if (name != "quit")
		{
			int mid = name.find(" ");
			int end = name.find("\n");
			int len = end-mid;
			first = name.substr(0, mid);
			last = name.substr(mid+1, len-1);
			name = last + ", " + first;
			namesFile << name << endl;
		}
		else break;
	}
}

void displayNames(string name)
{
	ifstream namesFile;
	namesFile.open("names.txt");
	while (!namesFile.eof()) 
	{
    	cout << name << endl;
    	getline(namesFile, name);
	}
}

void displayLast(string name)
{
	ifstream namesFile;
	namesFile.open("names.txt");
	while (!namesFile.eof()) 
	{
    	cout << name << endl;
    	getline(namesFile, name, ',');
	}
}

int main()
{
	ofstream namesFile;
	string names;
	int sel;
	cout << "*** Welcome to the Name Index. Please follow instructions.***\n" << endl;
	while (true)
	{
		cout << "--------------------------" << endl;
		cout << "What would you like to do?" << endl;
		cout << "--------------------------" << endl;
		cout << "Enter Names(1)\nDisplay Names(2)\nDisplay Last Names(3)" << endl; 
		cout << "--------------------------" << endl;
		cout << "Enter selection: ";
		cin >> sel;
		cout << "--------------------------" << endl;
		switch (sel)
		{
			case 1: getNames (names, namesFile); break;
			case 2: displayNames(names); break;
			case 3: displayLast(names); break;
			default: "Invalid input.";
		}
		cout << endl;
		if (sel == 0) 
		{
			cout << "Goodbye.";
			break;
		}
	}
	namesFile.close();
}
Last edited on
void getNames(string &name, ofstream &namesFile)

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.

The same reasoning applies for displayLast.
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?
Sorry, I don't see anything in the getNames function that would do that, and I can't replicate the problem.
This is wrong: while (!namesFile.eof()) never do this, ever.
Loop on the input operation instead: while(getline(namesFile, name))
Last edited on
Yeah, fg pointed that out too. I fixed it promptly, and I'll let my instructor know haha.

Thanks all for the help. I'll try to figure out the concatenation problem on my own.
Topic archived. No new replies allowed.