Why wont it let me enter a second phrase?

It lets me enter the first phrase in fine but then it skips the second one and goes straight to the cout statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
	char msg1[80] = "";
	char msg2[80] = "";

	cout << "Enter a phrase: ";
	cin.get(msg1, 80);

	cout << "\n\nEnter another phrase: ";
	cin.get(msg2, 80);

	cout << "Phrase " << msg1 << " has " << strlen(msg1) << " characters.";
	cout << "\n\nPhrase " << msg2 << " has " << strlen(msg2) << " characters.";

	_getch();
	return 0;
}
Last edited on
cin.get(msg1, 80);
This line will read until it finds a new line character but it will not remove the new line character from the stream so next time you call cin.get you will find the same new line character again, right away which makes msg2 an empty string.

If you use getline instead it will not leave the new line character in the stream.
cin.getline(msg1, 80);
alright thanks that makes sense now.
Topic archived. No new replies allowed.