Thread: Mild confusion

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Mild confusion

    I want to have a user input a string, read the length, and then put it into an array if it isn't already. I've been trying variations of cin.getline.. pointers and such, I never got anywhere. So can someone point me in the right direction?
    What I want to try to do is reverse the string.. I got a pretty good idea how to do this, so I don't want any help on it right now. Thanks in advance, Dual.

  2. #2
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    This will work with char* only:
    int x;
    char var;
    cin >> var; // what ever it is.
    x = strlen(var);
    cout << x;

    this will display the # of letters (single word only)

    By the way, look at the tutorial next time before you come here
    Yoshi

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Engineer223,

    I think you meant to allocate more the one char on var.

    He has a legitimate guestion. If you don't like the question don't answer it, but don't tell him to read the tutorial.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    if you wish to allow the user to input a string containing whitespaces like "what's up Doc?", then you need to use getline:

    char * input[80];
    cout << "enter a string up to 79 char long. It may include whitespaces. Use ~ to terminate input. " << endl;

    cin.getline(input, 80, '~');
    cin.ignore(256, ''\n');//attempt to mop up anything entered after the tilde.

    if you have to reverse the c_style string for an assignment by writing your own function, so be it. Otherwise, you can look up strrev() in your compiler's help section to save you a little hassle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By rits in forum C Programming
    Replies: 3
    Last Post: 03-12-2009, 08:00 AM
  2. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  3. a Bit of confusion (haha - Pun)
    By Junior89 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2007, 11:22 PM
  4. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM