Enter key

I am having trouble with one of my class assignments. The goal is to mirror the user's input until they press the enter key, but I don't know how to represent the enter key. This is what I have so far.

1
2
3
4
5
6
7
8
  string a = "parrot";
	while (a != )
	{
		cout << "Type something!" << endl;
		cin >> a;
		cout << a << endl;
	}
	
Hello Inig0,

I believe what you are looking for is while (a != '\n').

Hope that helps,

Andy
Thank you Andy, but when I try that, an error will pop up, saying "no operator "!=" matches these operands
To fix this, I replace string with int, and the error is solved, but now I have an int space, and not a string
The user always has to press Enter after typing in text and so if you set up the loop to end when Enter is pressed it would only run for 1 iteration. Are you sure you have to mirror user's input until they press Enter and not some other key? The following code shows what happens if you try to exit on Enter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;


int main()
{
   string a = "parrot";
	do
	{
		cout << "Type something!" << endl;
		cin >> a;
		cout << a << endl;
	}while  ( cin.get() != '\n');


While this shows how to exit on pressing some other key:

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

int main()
{
    string a = "parrot";
    bool fQuit = false;
    while (!fQuit)
	{
		cout << "Type something!" << endl;
		cin >> a;
		if(a == "1")
        {
            fQuit = true;
            break;
        }
        else
        {
            cout << a << endl;
        }
    }
}
closed account (48T7M4Gy)
The goal is to mirror the user's input until they press the enter key

This specification is quite ambiguous, almost meaningless, given that keyborad input generally always copies the user keystrokes and that input ends by pressing the return key.

Please provide an example of the input and the corresponding output.
I think I should have specified by saying that I was supposed to output whatever the user inputted. It may be meaningless, but it it's just part of a set of loop practices.

ex

Type something!
something
something
Type something!
again?
again?
Type something!

closed account (48T7M4Gy)
Show us the question you have been presented with. Exactly as the teacher has given it to you.
1. Parrot: echo the user's input until the user enters nothing.
Hello Inig0,

gunnerfunner has two good ideas for your problem and here is a third:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{

	std::string input{ "parrot" };

	while (input.length() > 0)
	{
		std::cout << "\n Type something: ";
		std::getline(std::cin, input);
		std::cout << "\n " << input << std::endl;
	}

	std::cout << "\n\n\n\n Fin Press any key to continue -- > ";
	while (!_kbhit()) {}
	return 0;
}

give this a try if you like.

Hope that helps,

Andy
Actually we can indeed get exactly what OP wants:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    string input{ "parrot" };

	do
	{
		std::cout << "\n Type something: ";
		std::getline(std::cin, input);
		std::cout << "\n " << input << std::endl;
	} 	while (input.size() != 0);
}


@Handy Andy:

string::length: Note that string objects handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8).

http://www.cplusplus.com/reference/string/string/length/
Topic archived. No new replies allowed.