Not working

I'm new to c++ and I'm excited that I debugged my fist a program by myself form someones source code. The tutorial source code wasn't working so I debugged it!

Now I'm making a program to play around with the features of c++ wanted but it
isn't working.

Look!

//My second program
#include <iostream>

using namespace std;

int main() {
char a;

cout << "Hello whats your name? :";

cin >> a;

cout << "Hello " << a << "!";

system("pause");

return 0;
}

Char only does one character. How do I fix it?
You can use c-style strings, which are character arrays: http://www.cprogramming.com/tutorial/lesson9.html
Or you can use the far superior std::string: http://www.cplusplus.com/reference/string/string/
I would use a getline. It will capture a whole line of user based text. And use a string instead of char in this case...

So this is how your program would look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main() {
string name;

cout << "Hello whats your name? :"<<endl;

getline (cin, name);

cout << "Hello " << name << "!"<<endl;



return 0;
}


Let me know if this works for you. :)

Oh and also, the system pause function complicates things a bit. I perfer not to use it when I program.
Last edited on
Topic archived. No new replies allowed.