I have no idea why, but I'm having problems with getting my strings to compare properly. Below is my code. The part I'm having trouble with is in the while loop. For some reason the programs looping even when my string matches the constant. Any help would be appreciated.

//string constants for nicks, rays, mikes
const char *nicks = "nicks";
const char *rays = "rays";
const char *mikes = "mikes";


char* allLower(char*);

char* allLower(char* name)
{
    int i = 0;
     
     while(name[i] != '\0') 
     {
           name[i] = tolower(name[i]);
           ++i;
     }
     
    printf("%s",name);
    return name;
}

int main(){
    
    char tempstring[20];
    char* pizza_establishment;
    int number_of_pizza_eaters;
    char pizza_type[17];
    
    printf("Which pizza place did you visit today? \nNicks, Rays, or Mikes?");
    fgets(tempstring,10,stdin);
    pizza_establishment = allLower(tempstring);
    printf("%s", pizza_establishment);
    while((pizza_establishment != nicks) || (pizza_establishment != rays) || (pizza_establishment != mikes)){
        printf("Error while entering restaurant name.  Make sure you entered the name correctly.");
        printf("Which pizza place did you visit today? \nNicks, Rays, or Mikes?");
        fgets(pizza_establishment,10,stdin);
    }

return( EXIT_SUCCESS );
}

Recommended Answers

All 10 Replies

Use strcmp to compare strings.

Use strcmp to compare strings.

Alright, so I skimmed through some related threads and I attempted to implement the following code to no success. What am I doing wrong here?

while((strcmp(pizza_establishment,nicks) != 0) || strcmp(pizza_establishment,rays) != 0) || strcmp(pizza_establishment,mikes) != 0)){
        printf("Error while entering restaurant name.  Make sure you entered the name correctly.");
        printf("Which pizza place did you visit today? \nNicks, Rays, or Mikes?");
        fgets(tempstring,10,stdin);
        pizza_establishment = allLower(tempstring);
    }

Check the logic of the while condition. The loop is skipped only when pizza_establishment is nicks, rays and mikes simultaneously.

Check the logic of the while condition. The loop is skipped only when pizza_establishment is nicks, rays and mikes simultaneously.

I messed up my brackets, but no its not? I'm using || not &&

And either way, I removed the rays and mikes condition and it didn't work.

You are comparing pointer with a constant value.
Use *Pointer_name=Constant_value

You are comparing pointer with a constant value.
Use *Pointer_name=Constant_value

Do you mean inside the strcmp function? If so, the program returned Segmentation fault when it hit the loop.

Do you mean inside the strcmp function? If so, the program returned Segmentation fault when it hit the loop.

No in while loop
like

while((*pizza_establishment != 'nicks') || (*pizza_establishment != 'rays') || (*pizza_establishment != 'mikes')).

No in while loop
like

while((*pizza_establishment != 'nicks') || (*pizza_establishment != 'rays') || (*pizza_establishment != 'mikes')).

First off, you need double quotes or you get a compiler error because its a string, or combination of characters. But even after that issue I got this: warning: comparison between pointer and integer

I've tried narrowing it down as much as possible, removing the allLower function and removing the lines where I reassign tempstring to pizza_establishment and nothing seems to point me in the right direction.

First off, you need double quotes or you get a compiler error because its a string, or combination of characters. But even after that issue I got this: warning: comparison between pointer and integer

The problem with the while loop is that even if your string matches the constant the remaining two condition yields true and loop executes because of ||.
Another problem might be that the parameter to function allLower is pointer so try using

pizza_establishment = allLower(&tempstring);

in your.
Please let me know if u can resolve the problem.

The problem with the while loop is that even if your string matches the constant the remaining two condition yields true and loop executes because of ||.
Another problem might be that the parameter to function allLower is pointer so try using

pizza_establishment = allLower(&tempstring);

in your.
Please let me know if u can resolve the problem.

Oh wow, I just realized I screwed up intensely. the while statement was supposed to be == not !=

Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.