Simple question about strings..

This is a discussion on Simple question about strings.. within the C++ Programming forums, part of the General Programming Boards category; How do I store an input as 20 characters long, with spaces included. For example when storing an address '31 ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    44

    Simple question about strings..

    How do I store an input as 20 characters long, with spaces included.
    For example when storing an address '31 High Street',
    only stores the '31'??

    Can someone please help me??
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    Cheers V

    But could someone please explain what each part of the command does??

    I understand that the '20' is the maxium number of characers.

    But is the 'string' the variable type to be entered or is it the name of the variable.

    And also I how I program some code that operates when the maximum charaters is more than 20, ie. using the '{' and '}'??

    Thanks alot JamMan..
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    cin.getline's the function you're using

    string's the name of the variable it's being read into.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    Cheers Gotvcheese, thats what I thought it was. But does this only works on strings??

    Can anyone help me with the following:
    "And also I how I program some code that operates when the maximum charaters is more than 20, ie. using the '{' and '}'??"

    Thanks once again JamMan..
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  5. #5
    Unregistered
    Guest
    there are several versions of getline(). The usual version has prototype

    getline(char StringWhereInputWillBeStored, int NumberOfCharToBeStored, char terminatingChar);

    The terminating character defaults to newline char so you frequently see only two parameters being written for.

    If the input is longer than the max indicated char then the remaining char remain in the ifstream and can cause havoc if not appropriately dealt with. I am not sure what you mean regarding use of the curly braces.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    By the curly bracket I mean:

    Code:
    if //length of a string is equal to or more than 20, need to know the code to put here??
    {
    //then do this
    }
    Thanks alot
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  7. #7
    Unregistered
    Guest
    Well you could do this:

    char dummy[3000]; //use a big enough array to hold just about any input;
    char input[21];//only really interested in first 20 char however, the last element is for the null char.

    cin.getline(dummy, 2999, '\n');
    if(strlen(dummy) < 21)
    strcpy(input, dummy);
    else
    {
    for(int i = 0; i < 20; i++)
    {
    input[i] = dummy[i];
    }
    input[20] = '\0';
    }
    //now do whatever you want with dummy as the first 20 char of dummy have been copied into input.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    Cheers whoever you are..

    You know there are some very good programers, that never register and stay as 'unregistered', why dont they register??

    Thanks anyway..

    JamMan..
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

  9. #9
    zzido
    Guest

    because

    they can't be bothered!

    programmers are lazy thats why we reuse code and why OOP was invented.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    44
    True..

    .. and a great thanks to everyone who helped me with this problem.
    I'm using Bloodsheds Dev-C++ Compiler.

    JamMan..

    Curious if I can live forever? CLICK HERE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21