Strings in C++

How do you handle strings in C++ . Mainly Input. Now You might be thinking that I should post this on the beginner forum but I am actually quite intermediate in C++ . It's just that I am 13 and I am teaching myself C++ and Java. I use Microsoft Visual C++ Compiler 2010. I have written this code, it compiles but doesn't work.

#include <iostream>// input and output
#include <string> //do you declare this like it is?
using namespace std;//namespace standard

string name; //is this how you make a string?

int main()//the main body
{
cout<<"What is your name?";//output

cin>>name;//input to string

cout<<"Hello" << name;//output

}
The program works until it needs to output hello name.
Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;/d
//Avoid using namespace std. It is a bad practice

string name;
//Globals are usually a bad practicce too

int main()
{
    std::string name; //local variable
    std::cout << "What is your name?" << std::endl; //New line after question

    std::cin >> name;

    std::cout << "Hello, " << name; //Should work. note whitespace at the end of literal.
}
Your code should work. How it is not working? What output is?
Last edited on
Topic archived. No new replies allowed.