help please

i am totaly new to c++
i wanted to write a program like
user to enter his name
and
cin >> name
cout << name

but it shows only 1 letter and not full nae including spaces
why how?
please help
nason

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
void main()
{
	char x;
	std::cout << "Enter your full name : \n";
	std::cin >> x;

	std::cout << x;

}
	
it shows only 1 letter
Variable char x; can hold just a single character.

You need to use a string variable. It is recommended to use the C++ std::string.
http://www.cplusplus.com/reference/string/string/


In order to input the complete name, including any spaces, use the getline() function. There are two differing syntaxes, one suited to c-strings, the other to std:: string.

http://www.cplusplus.com/reference/string/string/getline/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<string>
#include<iostream>

using namespace std;

int main(){

string name;
cout<<"Enter your name: ";
getline(cin, name);
cout<<name;

return 0;
}
Last edited on
closed account (4jzvC542)
@mutexe & @Chervil && @Group of Eight
thank you all very much

@mutexe thanks a lot i learned a totally new thing and its great....
thanks for the code man...
You're welcome dude.
Topic archived. No new replies allowed.