Thread: Help....

  1. #1
    Unregistered
    Guest

    Help....

    i have a hello program that asks for your name and the says hello then the name you input

    #include <iostream.h>
    #include <string>

    int main ()
    {

    char name;
    cout << "Enter your name : ";
    cin >> name;



    //print out the text string "Hello";

    cout << "Hello \a" << name;


    return 0;

    }

    but instead of giving the whole name inputted it gives just the first letter

    so it looks like this
    c:\ > hello.exe
    Enter your name : Joe
    Hello J

    why is this?

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    char only specifies 1 character,
    so it gets the first character as that is the only space you have specified.
    To get the whole name you need to use an array

    eg:

    char name[12];
    cout<<"Enter you name?";
    cin>>name;
    cout<<"Hello "<<name;
    getch();



    hth
    Monday - what a way to spend a seventh of your life

Popular pages Recent additions subscribe to a feed