|
-
May 16th, 2004, 01:54 AM
#1
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
-
May 16th, 2004, 04:51 AM
#2
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.
-
May 16th, 2004, 10:22 AM
#3
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.
-
May 16th, 2004, 11:19 AM
#4
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;
}
}
-
May 17th, 2004, 01:43 PM
#5
What type of storage is tempName? char buffer, CString, std::string?
-
May 17th, 2004, 01:55 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|