Thread: Sorry if this has been posted before, but...

  1. #1
    Unregistered
    Guest

    Sorry if this has been posted before, but...

    I'm a C newbie and I can't figure out how to have the user be able to type in a long word instead of a limit to 4 characters; for example:

    char a;

    cin>>a;
    if (a=='alongword')
    {
    do stuff
    }

    What's the easiest way to do this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char a;

    A 'char' is a single character. As such, your program should only be acceptign a single character. What you need is an array.

    char a[NUMBER];

    Replace number with the number of characters you want them to be able to enter, then add one to that number.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    And read a little more bout the cin. There's also cin.getline
    what does signature stand for?

  4. #4
    Unregistered
    Guest
    char a;
    cout << "enter a single char" << endl;
    if(a == 'i')
    cout << "you entered the character i" << endl;

    char buffer[80];
    cout << "enter a single word up to 79 char long" << endl;
    //if(buffer == 'alongword') won't work because alongword is a string of char and should be so indicated by using "" not ' '. Also can't use the equality operator on strings, have to use strcmp(), or one of it's relatives.

  5. #5
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Use <cstring> header file to manipulate strings. It's got a lot of good functions.
    what does signature stand for?

Popular pages Recent additions subscribe to a feed