While loop problem

Hi!

Im trying to make a program that takes your first and last name and writes it out with the last name first. The name has to be stored in one char variable and besides the char variable every other variable has to be of the type Int.

This is my code so far.. I tried to put in while( name[i] > ' ' && name[i] < 0 ) but that didn't seem to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void printWithLastNameFirst(const char* name)
{
int i = 0;

while( name[i] != 0 )
{
cout << name[i];

i++;
}

cout << endl;
}

int main()
{
    char name[30];
    cout << "Give me your full name: ";
    cin.get(name, 30);

printWithLastNameFirst(name);

    return 0;
}


Any help is appreciated, thanks!
I think you had the right idea with your suggestion in bold, but "name < 0" doesn't make sense (char interpretation and negative int values are unrelated) and the other ones are [i]odd at best. What are you trying to do there?

You're probably looking for these comparisons:
1
2
myChar != '\o' // Not 'end of string'
myChar != ' ' // Not a space 


With just those two, finding the last name and first name should be a breeze [assuming the input is valid].
I think you should define two character arrays in your function printWithLastNameFirst to store first and last names. For example

1
2
3
4
5
void printWithLastNameFirst(const char* name)
{
   char theFirstName[20] = { '\0' };
   char theLastName[20] = { '\0' };
...


and then you should extract first and last names from source string. After that you can display first and last names in any order.
Last edited on
Topic archived. No new replies allowed.