-4

I am very new to programming and trying to learn as much as I can. I am curious as to what cin char does exactly. When I input a word, the only thing outputted is the first letter of the word. Is this supposed to happen?

#include <iostream>

using namespace std;

int main()
{
char ch;
cin >> ch;
cout << "Thank you for entering " << ch << endl; 
return 0;
}

I would like to output the whole word or phrase, but that does not seem to be happening. Thanks!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
sung
  • 33
  • 1
  • 4
  • 9
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jul 18 '19 at 16:51
  • 2
    You're inputing into a char. A char cannot hold a word or phrase--it's a character. – Jonathan Hall Jul 18 '19 at 16:51
  • 2
    In `c++` a string is `std::string`. Note that `cin` will stop at the first whitespace character so if your string has spaces you will want [std::getline](https://en.cppreference.com/w/cpp/string/basic_string/getline) Please look at the example in the link for getline. That should help a lot. – drescherjm Jul 18 '19 at 16:53

4 Answers4

5

A char is a single character. When that's the destination of your read, a single character is what you'll get.

If you want to read multiple characters (a string), then read into a std::string rather than a char.

To read more than a word (since std::cin will stop at whitespace), look into std::getline.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • More so `std::cin >> std::string` will stop at whitespace, not `std::cin` itself. –  Jul 18 '19 at 18:02
  • Thanks! If I use std::getline is there a limit to the input? – sung Jul 18 '19 at 21:25
  • @sungjina "is there a limit to the input?" - Yes. The end of the line. And also the amount of memory in your computer, if you input a crazily long line. There are *always* *some* limits. – Jesper Juhl Jul 18 '19 at 21:26
1

Problem:

Yes this is supposed to happen. char in C++ is a type of variable that can only store a character. Therefore, cin will only take the first letter from the input stream. If you want to output a word use the string variable.

For more information on C++ variable types: http://www.cplusplus.com/doc/tutorial/variables/

  • May I suggest using https://cppreference.com as a superior reference site to send people to, rather than cplusplus.com. cppreference is known to be much more complete and not full of factual errors.. – Jesper Juhl Jul 18 '19 at 17:09
  • @JesperJuhl, for what it's worth, as a new developer, I *much* prefer cplusplus.com because it's easier to read. cppreference.com may be more correct, but the "sugar" that cplusplus.com gives really helps the medicine go down. – JohnFilleau Jul 18 '19 at 17:14
  • @John In the long run you need to be able to read documentation without the "sugar". And factually correct and accurate documentation is worth more than easy to read (actually, I find cppreference to be quite easy to read; what's the problem?), incorrect documentation. – Jesper Juhl Jul 18 '19 at 17:17
  • @JesperJuhl Noted. – u_yousafzai54 Jul 18 '19 at 19:25
  • Thanks guys, I'll definitely be checking out cppreference.com and then cplusplus if I can't read the former haha. – sung Jul 18 '19 at 21:27
1

That's because you use char type. Char means 1 character of input string. Just use string type (included in "string" module).

PS: predicting your next question – you can use cin.getLine() function for reading whole input string next time. Because cin function read whole symbols until the whitespace character (as space or EOL (end of line))

Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
pavbox
  • 26
  • 4
1

I would like to output the whole word or phrase, but that does not seem to be happening

To output a whole word or phrase you have to reserve memory for the word or phrase.

Objects of the type char as in your declaration

char ch;

can store only one character.

Use instead an object of the standard C++ class std::string. The program can look the following way

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;

    getline( cin, s );

    cout << "Thank you for entering " << s << endl; 

    return 0;
}

Or without the using-directive (that is adviced)

#include <iostream>
#include <string>

int main()
{
    std::string s;

    std::getline( std::cin, s );

    std::cout << "Thank you for entering " << s << std::endl; 

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335