program skipping cin/getline requests

I can't figure out what could cause to program to skip over cin and getline requests, but it is, so can anyone help?

Ow and i know that alot of the error handling really isn't needed, and could be structured and/or named better, but i'm doing this as more of a refresher on string handling, error handling/finding, and just a quick 2 hr program.

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <string>

using namespace std;

void error(string message);
bool changeTemp();
void changeValue();
void checkValues();

double startTemp, endTemp, step;

int main()
{
	cout << "Enter start temperatur in celsius\n";
	cin >> startTemp;
	cout << "\nEnter end temperatur in celsius\n";
	cin >> endTemp;
	checkValues();
	cout << "\nEnter step size\n";
	cin >> step;
	checkValues();
	cout << "\nNow printing table...";
	for(double temp = startTemp; (temp + 0.50f) < endTemp;)
	{
		cout << "\nCelsius: " << temp << " | Fahrenheit: " << ((temp * 1.80f) + 32.00f);
		temp = temp + step;
	}
	cout << endl;
	system("PAUSE");
}

void error(string message)
{
	
	if(message.compare("endTooLow"))
	{
		cout << "\nEnd value cannot be lower than start value.";
		changeTemp();
		checkValues();
	}

	if(message.compare("startTooHigh"))
	{
		cout << "\nStart value cannot be higher than end value.";
		changeTemp();
		checkValues();
	}

	if(message.compare("stepTooHigh"))
	{
		cout << "\nStep value cannot be larger than end temp minus start temp.";
		cout << "\nThat value is " << endTemp-startTemp;
		changeValue();
	}
	if(message.compare("invalidInput"))
	{
		changeValue();
	}
}

bool changeTemp()
{
	string start, end;
	start = "start";
	end = "end";
	string value;

	cout << "\nWould you like a new end or start temperatur?\n";
	getline(std::cin, value);
	if(start.compare(value))
	{
		cout << "\nEnter new start temperatur\n";
		cin >> startTemp;
		checkValues();
	}
	else if(end.compare(value))
	{
		cout << "\nEnter new end temperatur\n";
		cin >> endTemp;
		checkValues();
	}
	else return false;
}

void checkValues()
{
	if(startTemp && endTemp)
	{
		if(startTemp > endTemp)
		{
			error("startTooHigh");
		}
	
		else if(endTemp < startTemp)
		{
			error("endTooLow");
		}
		
		else if(step)
		{
			if(step > endTemp - startTemp)
			{
				error("stepTooHigh");
			}
		}
	}
}

void changeValue()
{
	string sStart, sEnd, sStep;
	sStart = "start";
	sEnd = "end";
	sStep = "step";

	string value;

	cout << "\nWould you like to change start, end, or step value?";
	getline(cin, value);
	if(sStart.compare(value))
	{
		cout << "\nWhat should be the new start temperatur value?\n";
		cin >> step;
		checkValues();
	}
	else if(sEnd.compare(value))
	{
		cout << "\nWhat should be the new end temperature value?\n";
		cin >> step;
		checkValues();
	}
	else if(sStep.compare(value))
	{
		cout << "\nWhat should be the new step value?\n";
		cin >> step;
		checkValues();
	}
	else
	{
		cout << "\nInvalid input";
		error("invalidInput");
	}
}
Last edited on
closed account (Dy7SLyTq)
have you tried flushing it?
Get rid of getline(cin,value); and just use cin>>value; instead.

The rest of your problems are caused by not understanding how the std::string.compare() function works.

It returns 0 when the strings match. That is, it is false when the strings match and true when they don't match.

Just use value==start for example to compare strings.
In this case, I think using std::cin >> value instead of getline(std::cin, value); is a reasonable solution.

When the string to be entered consists of multiple words, getline is appropriate, but here the input is a single word and cin >> value is just fine.

There is a difference in the way these functions behave.
cin >> value will read from the input stream only up to the first whitespace character. Any unused characters will remain in the input buffer and may be accepted by the next cin command. There will usually be a newline character '\n' left in the buffer afterwards.

On the other hand, getline reads all characters up to the first newline character, and then reads and discards the newline itself.

When this sequence is executed:
1
2
    cin >> value1;
    getline(cin, value2);

the first line leaves the '\n' in the buffer, the second line reads an empty string and then discards the newline.

A solution is to use cin.ignore() or cin.ignore(1000,'\n') to discard unwanted characters from the input buffer. The first ignores a single character, the second ignores up to 1000 characters, or until the newline is found.
1
2
3
    cin >> value1;
    cin.ignore();
    getline(cin, value2);

Topic archived. No new replies allowed.