CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2004
    Location
    Co'Co Island
    Posts
    65

    Checking for space in between, HELP!

    I am trying to check for a space in between on a full name example. " John V Tank" and this is my code to check the space in between but I am getting an error code:

    warning C4390: ';' : empty controlled statement found; is this the intent?


    cin.getline(tempName, tempStorageName);

    for ( int i = 0; i <=60; i++)
    {
    if (tempName[i] != ' ');
    int count = (count + 1);
    }

    my intention is to count how many characters on the full name excluding the space in between and if it is more than 25 characters I need to post an error that is why I have the counter >> count. any help thanks

  2. #2
    Join Date
    Sep 2003
    Location
    Korat, Thailand
    Posts
    112

    cin.getline(tempName, tempStorageName);

    for ( int i = 0; i <=60; i++)
    {
    if (tempName[i] != ' ');
    int count = (count + 1);
    }
    Good try. However, without changing your thought, your code shold be refined like this:

    int count = 0;
    for ( int i = 0; i <=60; i++)
    {
    if (tempName[i] != ' ')
    count = (count + 1);
    }

    Take out the declaring part of 'count' and get out the ';' behind 'if' expression.
    Last edited by Monch; May 16th, 2004 at 04:56 AM.

  3. #3
    Join Date
    Mar 2004
    Posts
    43
    Are y define tempName as char * or something make it as a string?
    i am afriad that y are not very clear about the diffierence between
    the " " and ' ',here " " is a string ,but the ' ' is empty just like your compiler told you,i think what you really need is the following code:
    int count =0;
    for ( int i = 0; i <=60; i++)
    {
    if (tempName[i] != 32) count ++;
    }

    the 32 is the ascii code which stands for space
    good luck
    Last edited by lugangxyz; May 16th, 2004 at 10:25 AM.

  4. #4
    Join Date
    Apr 2004
    Location
    Co'Co Island
    Posts
    65
    Hey, thanks.. this is what I came up with last night before I checked the board this morning. problem is it won't stop at getline () it will skip it and return to main menu.




    system("cls");

    cout << "Please enter the student full name\n";
    cout << "(example: John D Banana) :";
    cin.getline(tempStorageName,tempSpace);

    while (true)
    {
    for (i = 0; i <=60; i++)
    {
    if (tempStorageName[i] > ' ')
    {
    count = count + 1;
    }
    if (count > 25)
    {
    cout << "Your Full Name Is Longer Than 25 Character, which is invalid.\n";
    studentInput();
    }
    }
    break;
    }
    }

  5. #5
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    What type of storage is tempName? char buffer, CString, std::string?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637
    Originally posted by lugangxyz
    Are y define tempName as char * or something make it as a string?
    i am afriad that y are not very clear about the diffierence between
    the " " and ' ',here " " is a string ,but the ' ' is empty just like your compiler told you,i think what you really need is the following code:
    int count =0;
    for ( int i = 0; i <=60; i++)
    {
    if (tempName[i] != 32) count ++;
    }

    the 32 is the ascii code which stands for space
    good luck
    The compiler was bellyaching about the ; terminiating the if statement. Checking for ' ' is valid in this case. Monch correctly identified the problems.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured