Char variable question

okay so this is my code for a really simple variable test,

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

using namespace std;

int main()
{
    int thisisanumber;  //A non-decimal number
    char yourname;      //A letter or group of letters

    cout<<"Enter a number: ";
    cin>> thisisanumber;
    cin.ignore();
    cout<<"This is the number you entered: "<< thisisanumber <<" Wait whats your name again: ";
    cin>> yourname;
    cin.ignore();
    cout<<"Hey guess what: "<< yourname <<" You built this code\n";
    cin.get();
}


so anyway, if when i compile it and run the code, it works fine until it comes to the part that says cout<<"Hey guess what: "<< yourname <<" You built this code\n";
over in that line, in the "yourname" variable area, only the first letter of the name i enter is shown. does anyone know how to fix this?
you need to define yourname as std::string, BTW, don't forget to include its header
I'm sorry. I'm really newb at this. Can you please explain how to do this?
The declaration of yourname is for a single character. If you want to allow for someone's name then you would need to declare char array - take a look at this article for more information: http://www.cplusplus.com/doc/tutorial/ntcs/

Although as EricDu mentioned you might be better off using the C++ standard string as it's much easier to understand and use: http://www.cplusplus.com/reference/string/

Kloid
thank you man. in the end all i had to do is type in [15] at the end of the variable statement
Topic archived. No new replies allowed.