Thread: specific length

  1. #1
    Unregistered
    Guest

    specific length

    Hi, i was wondering how i could specify the length of something in a console program like:

    Code:
    #include <iostream.h>
    #include "apstring.h"
    
    int main()
    {
         apstring state;
    
         cout << Which state do you live in?
         cin >> state;
         cout state;
    }
    How would i make it so state was two letters as in AZ or CA?

    thanks!

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    cin.getline(string, 3, '\n');

    Tis what I meant
    Last edited by Dual-Catfish; 04-17-2002 at 03:28 PM.

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    This person should get a cookie for using the code tags (joke from a friend)

    but back to the point....use chars.

    char[2];
    //insert code here.

    that should work (no need for code, ain't posting it)

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    i believe getline would need 3 instead of 2, because it reserves a spot for the null character

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i think he meant

    cin.getline(state,3);

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    if you're working with state abbrev. you probably need a data table with state names to abbrev. the first two or three letters of any state should suffice. (except for mississippi and missouri.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-15-2009, 10:04 AM
  2. Calculating the length of a string
    By BSmith4740 in forum C Programming
    Replies: 32
    Last Post: 07-02-2008, 12:51 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Count the length of strintg without using strlen?
    By yukon in forum C Programming
    Replies: 9
    Last Post: 10-01-2001, 08:32 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM