Thread: Basic C++ q's

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    Basic C++ q's

    If I wanted to check if a user entered value was blank ie. the user presses enter would I use this code??:

    cout <<"Enter city code:"<<endl;
    cin >>cityCode;
    if (cityCode == '')

    secondly how can I clear an array of 28 chars in one command??

    Andrew Wilmut

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It depends on the type of cityCode. Simply using the operator >> of cin will not a blank line. You would need to use the cin.getline method to get input that potentially stores a blank string.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    12
    citCode is an array of 4 chars


    if I also wanted to clear the contents of char how could i do it??

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    To clear the contents of a char just set all the elements of it to become null-terminating characters ('\0')
    for ex:

    char cityCode[4];

    for (int x = 0; x < 4; x++)
    cityCode[x] = '\0'; // the sets each element of
    // cityCode to become a '\0' character

  5. #5
    Unregistered
    Guest
    Or simply strcpy(CityCode,"\0");

    That will clear the string in one fell swoop.

    To check for a space:

    int validate( char string )
    {
    int bad_string = 1;

    for (int i = 0; i < strlen(string); i++)
    {
    if(string[i] == ' ')
    {
    bad_string = 0;
    }
    }
    return bad_string;
    }



    Then in main call it like this:

    if(!validate(CityCode))
    {
    //process error
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Or simply strcpy(CityCode,"\0");

    That will clear the string in one fell swoop.

    To check for a space:

    int validate( char string )
    {
    int bad_string = 1;

    for (int i = 0; i < strlen(string); i++)
    {
    if(string[i] == ' ')
    {
    bad_string = 0;
    }
    }
    return bad_string;
    }



    Then in main call it like this:

    if(!validate(CityCode))
    {
    //process error
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob with basic q's
    By SimplyComplex in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2006, 01:17 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM