Why can't I input anything?

Whenever I run the program a number is automatically input and i just get this,
1 What is your name?
2 Hello � !

Code
#include <iostream>
using namespace std;

int main()
{
char name;
cout << "What is your name? \n";
cin>> name;
cout << "Hello " << name;
cout << " !";
return 0;
}
Last edited on
Please remember to put your code between the source code brackets so it can be read easily.


Your code is doing exactly what you wrote it to do: print one letter. A char is a single character. If you want to print out an entire word, you need a char array.

But even then, it will only output the first name (if both first and last names are written) because a space stops the input. If you want both names to be printed, use the getline function.
Last edited on
If you want to print out an entire word, you need a char array.


Nope. You Need a string, don't use char arrays.

1
2
3
4
5
6
7
8
9
int main()
{
string name;
cout << "What is your name? \n";
cin>> name;
cout << "Hello " << name;
cout << " !";
return 0;
}
Yes, I suppose you can also use a string.
Yes, I suppose you can also use a string.


Should, not could. Should use string. You can use char array if you program in C. Dont do it in c++.
Can you explain why though? Is there a specific benefit of string over char array in this case?
Last edited on
Topic archived. No new replies allowed.