Thread: user string input

  1. #1
    Unregistered
    Guest

    user string input

    how do i assign the string input of a user to a char? cin>> only gets the part before the first space; for instance, say a user inputs "This is a test." i'd like ot assign that to a char

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > how do i assign the string input of a user to a char? cin>>
    Well, first of all, this is the C board, so I wouldn't use cin.

    > how do i assign the string input of a user to a char? cin>>
    Secondly, you can't assign a string from the user to a char. You'd have to do it to a char * or an array of characters. I'm not sure if you are trying to do this in C or C++. But, I'll show you how to do it in C. If you want to know how to do it in C++, go to that board:
    Code:
    int main()
    {
        char name[50];
        
        printf("Enter your name: ");
        fgets(name, sizeof(name), stdin);
    
        printf("Your name is %s", name);
    
        return (0);
    }
    I don't know about C++, but that's how you get a string from the user in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM