can't enter phrases in loop

getline is not readable after first time through loop. It skips that line and goes to Do you want stop entry? line.

I would appreciate any help that I can get

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
#include<iostream>
#include<string>
#include<cstring>
#include<cctype>

using namespace std;

void upr(const string& str);

int main()
{
	string strinpt;
	char quit = 'Q';
	char ch = ' ';
	while (quit != 'q')
	{
		cout << "Enter phrase in lower case to be turned into upper case: " << "\n";
		getline(cin,strinpt);
		upr(strinpt);
		cout << "Do you want stop entry? " << "\n";
		cin >> quit;
	}
	return 0;
}
void upr(const string& str)
{
	for (const char* ptr = str.c_str(); *ptr; ++ptr)
		cout << (char) toupper(*ptr);

	cout << endl;
}
closed account (Dy7SLyTq)
try writing cin.flush() at the beginning of the loop
It says:Error 1 error C2039: 'flush' : is not a member of 'std::basic_istream<char,std::char_traits<char>>' 19 1
Use

cin.ignore(1024, '\n');

just after the cin >> quit; to eat up the rest of the line, including the '\n'.

When you mix cin with getline, you've got to make sure you don't leave '\n's sitting in the stream. Otherwise getline will just get the end of the line cin nibbled at rather than waiting for a fresh, new one.

Andy
Last edited on
1
2
3
4
5
// ...               
cout << "Enter phrase in lower case to be turned into upper case: " << "\n";
// getline(cin,strinpt);
getline( std::cin >> std::ws, strinpt ) ; // http://en.cppreference.com/w/cpp/io/manip/ws
// ... 
Andy,
Sounds logical, but still doesn't work. It causes a wait in the program for more input.

Bob
Well, it works for me with GCC and VC++

???

Andy

Enter phrase in lower case to be turned into upper case:
hello
HELLO
Do you want stop entry?
n
Enter phrase in lower case to be turned into upper case:
goodbye
GOODBYE
Do you want stop entry?
n
Enter phrase in lower case to be turned into upper case:
again
AGAIN
Do you want stop entry?
q


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
#include<iostream>
#include<string>
#include<cstring>
#include<cctype>

using namespace std;

void upr(const string& str);

int main()
{
	string strinpt;
	char quit = 'Q';
	char ch = ' ';
	while (quit != 'q')
	{
		cout << "Enter phrase in lower case to be turned into upper case: " << "\n";
		getline(cin,strinpt);
		upr(strinpt);
		cout << "Do you want stop entry? " << "\n";
		cin >> quit;
		cin.ignore(1024, '\n');
	}
	return 0;
}

void upr(const string& str)
{
	for (const char* ptr = str.c_str(); *ptr; ++ptr)
		cout << (char) toupper(*ptr);

	cout << endl;
}
Weird, according to http://www.cplusplus.com/reference/string/string/getline/ you will just append the characters to the end of a string with getline, although it looks like it also clears the string first from the above output.

Maybe use numeric_limits<streamsize>::max() rather than 1024 in the above code?
Hi Jl,
That did it. Thanks for the help.
Bob
Hi Andy,
I modified the program again and it now works. The placement of the line of code is different than were I had it. It was after the cin >> read. I placed it at to top of the code before the getline. That may have made a difference. Thanks for the help.

Bob
Topic archived. No new replies allowed.