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;
}
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>
usingnamespace 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:
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!
#include <iostream>
usingnamespace 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).