[Solved] std::cin Will not work


Reading input with std::cin

std::cin is used for input, and you have to store the value you want to read into a variable.

For example,

std::string word;
std::cin >> word;

std::cin >> word; will assign to word the word the user has entered. Hence, it makes no sense to pass a string literal ("hello" for example) to std::cin, because it won’t know what to do with it.

If you want to show a message to the user to tell them to enter something, just use std::cout as you did to print your other message.

Other interesting things about std::cin

You may also use some other types such as int, float or others to use directly with std::cin.

Do note that when inputting a string with std::cin, it will only read one word (separated by whitespace), which means that if the user inputs hello world, word‘s value will be hello – if you do std::cin >> word; again, you will get world. To read a whole line from std::cin, refer to this thread.

If you want to read multiple things at once (to avoid putting std::cin >> a lot of times in your code), you can “chain” the inputs :

std::string word1, word2;
int number;
std::cin >> word1 >> number >> word2;

This will work as expected for an input such as firstword 443351 lastword.

2

solved std::cin Will not work