I'm not sure if I'm not comparing the strings properly or not, but for some reason my toppingCost function will only return 0 or a huge wacky number (when I mess around with the code a bit). Does anyone see anything wrong with this?

// represent the cost of pizza, in pennies
#define NICKS_PIZZA 1000
#define RAYS_PIZZA 1500
#define MIKES_PIZZA 2000

// pizza eaters count
#define TAMMY_AND_SUE 2
#define TAMMY_SUE_MARY 3
#define TAMMY_SUE_MARY_CHRIS_TOM TAMMY_SUE_MARY + 2
// string constants for customers
const char *TammySue = "Tammy and Sue";
const char *TammySueMary = "Tammy, Sue and Mary";
const char *TammySueMaryChrisTom = "Tammy, Sue, Mary, Chris and Tom";

// pizza type indicator
#define PIZZA_PLAIN 0
#define PIZZA_PEPPERONI 1
#define PIZZA_SAUSAGE_ONION 2

// pizza cost of add-ons, and taxes
#define NICKS_PEPPERONI 100
#define RAYS_PEPPERONI 100
#define MIKES_PEPPERONI 150
#define NICKS_SAUSAGE_ONION 100
#define RAYS_SAUSAGE_ONION 125
#define MIKES_SAUSAGE_ONION 150
#define NYC_TAX_RATE 0.0825

// string constants for pizza dining establishments
const char *nicks_restaurant = "Nick's Pizza";
const char *rays_restaurant = "Ray's Pizza and Subs";
const char *mikes_restaurant = "Mike's Pizza and Deli Station";

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

//string constants for pizza types
const char *Plain = "plain";
const char *Pepperoni = "pepperoni";
const char *SausageAndOnion = "sausage and onion";
//function prototypes
char* allLower(char*);
int toppingCost(char*, char*);

//change string to all lowercase
char* allLower(char* name)
{
    int i = 0;
    while(name[i] != '\0') 
    {
        name[i] = tolower(name[i]);
        ++i;
    }
    //return string in lowercase
    return name;
}

//determine the topping cost
int toppingCost(char* pizza, char* location){
    int cost;
    //set topping cost to 0 for plain pizza
    if(strcmp(pizza, Plain)){
        cost = 0;
    }
    //set topping cost for pepperoni pizza
    else if(strcmp(pizza, Pepperoni)){
        if((strcmp(location, nicks)) || (strcmp(location, rays))){
            cost = 100;
        }
        else if(strcmp(location, mikes)){
            cost = 150;
        }
        else{
            puts("Error determining pepperoni cost.\n");
        }
    }
    //set topping cost for sausage and onion pizza
    else if(strcmp(pizza, SausageAndOnion)){
        if(strcmp(location, nicks)){
            cost = 100;
        }
        else if(strcmp(location, rays)){
            cost = 125;
        }
        else if(strcmp(location, mikes)){
            cost = 150;
        }
        else{
            puts("Error determining sausage and onion cost.\n");
        }
    }
    //catch errors
    else{
        puts("An error occured while determining the pizza type.");
    }
    //return cost
    return cost;
}

int main(){
    
    char tempstring[20];
    char tempchar;
    char *pizza_establishment;
    int number_of_pizza_eaters;
    char *pizza_type;
    
    puts("Which pizza place did you visit today? \nNicks, Rays, or Mikes?");
    fgets(tempstring,20,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(tempstring,20,stdin);
        pizza_establishment = allLower(tempstring);
    }
    
    printf("\nIncluding yourself, how many people?");
    //fgets(tempchar,10,stdin);
    //number_of_pizza_eaters = atoi(tempchar);
    scanf("%d", &number_of_pizza_eaters);
    getchar();
    while((number_of_pizza_eaters < 1) || (number_of_pizza_eaters == 4) || (number_of_pizza_eaters > 5)){
        if(number_of_pizza_eaters == 0){
            puts("Who do you think's going to eat this pizza?");
        }
        else{
            puts("Bringing along some unexpected guests today?");
        }
        printf("\nIncluding yourself, how many people?");
        scanf("%d", &number_of_pizza_eaters);
        
    }
    
    //printf("%s %d\n\n", pizza_establishment, number_of_pizza_eaters);


    puts("\nWhat type of pizza will you be eating today? Plain, Pepperoni, or Sausage and Onion?");
    fgets(tempstring,20,stdin);
    //printf("%s", tempstring);
    pizza_type = allLower(tempstring);
    while((pizza_type == "plain") || (pizza_type == "pepperoni") || (pizza_type == "sausage and onion")){
        puts("Incorrect Pizza Type Entered");
        printf("\nWhat type of pizza will you be eating today? Plain, Pepperoni, or Sausage and Onion?");
        fgets(tempstring,20,stdin);
        pizza_type = allLower(tempstring);
        
    }
    //printf("%s %d\n\n", pizza_establishment, number_of_pizza_eaters);
    
    
    double price;
    char customers[50];
    strcpy (customers,TammySue);
    
    
    if(strcmp(pizza_establishment, nicks)){
        price = NICKS_PIZZA;
        printf("%s had lunch today at %s.\n",customers, nicks_restaurant); 
    }
    else if(strcmp(pizza_establishment, rays)){
        price = RAYS_PIZZA;
        printf("%s had lunch today at %s.\n",customers, nicks_restaurant);
    }
    else if(strcmp(pizza_establishment, mikes)){
        price = MIKES_PIZZA;
        printf("%s had lunch today at %s.\n",customers, nicks_restaurant);
    }
    else{
        puts("An error occured while determining which restaurant was visited.");
    }
    
    printf("%s", pizza_type);
    //assign topping cost to toppings
    int toppings = toppingCost(pizza_type, pizza_establishment);
    printf("\n\n%d\n\n", toppings);
    
/*
Tammy had lunch today at Nick's Pizza with Sue and Mary.
They had a plain pizza today.
Total for the pizza, with tax, is $10.83.
Each of the 3 people owe $3.61 for the pizza.
    
Tammy had lunch today at Mike's Pizza with Sue.
They had a pepperoni pizza today, adding $1.50 extra cost.
Total for the pizza, with tax, is $23.27.
Each of the 2 people owe $11.63 for the pizza.    
   
costINcents / 100 = $dollars.change
*/
    
    return( EXIT_SUCCESS );
}

Recommended Answers

All 11 Replies

> if(strcmp(pizza, Plain))
strcmp() returns ZERO when the strings match.

I tried setting it equal to zero, so it would return 1, and it would return something like 6930312.


Edit:
I get this...

An error occured while determining the pizza type.


6139892

Which would indicate that non of the strings matched, which they do.

I mean if ( strcmp(pizza, Plain) == 0 ) Like that.

Also, watch out for operator precedence when you're trying to compare two strings.

I mean if ( strcmp(pizza, Plain) == 0 ) Like that.

Also, watch out for operator precedence when you're trying to compare two strings.

Yes, that's what I have..

//determine the topping cost
int toppingCost(char* pizza, char* location){
    int cost;
    //set topping cost to 0 for plain pizza
    if(strcmp(pizza, Plain) == 0){
        cost = 0;
    }
    //set topping cost for pepperoni pizza
    else if(strcmp(pizza, Pepperoni) == 0){
        if((strcmp(location, nicks)) || (strcmp(location, rays))){
            cost = 100;
        }
        else if(strcmp(location, mikes)){
            cost = 150;
        }
        else{
            puts("Error determining pepperoni cost.\n");
        }
    }
    //set topping cost for sausage and onion pizza
    else if(strcmp(pizza, SausageAndOnion) == 0){
        if(strcmp(location, nicks)){
            cost = 100;
        }
        else if(strcmp(location, rays)){
            cost = 125;
        }
        else if(strcmp(location, mikes)){
            cost = 150;
        }
        else{
            puts("Error determining sausage and onion cost.\n");
        }
    }
    //catch errors
    else{
        puts("An error occured while determining the pizza type.");
    }
    //return cost
    return cost;
}

Great, 2/10 for effort.

What about ALL of the strcmp calls.

This is getting old REALLY fast.

Great, 2/10 for effort.

What about ALL of the strcmp calls.

This is getting old REALLY fast.

I happened to miss those, but even so.. they would at least print the else statement. My bad for being sloppy at 5am.


This still doesn't make sense because I didn't have my strcmp calls equal to zero in main and that runs properly..

Which pizza place did you visit today?
Nicks, Rays, or Mikes?
mikes

Including yourself, how many people?3

What type of pizza will you be eating today? Plain, Pepperoni, or Sausage and Onion?
pepperoni
Tammy and Sue had lunch today at Nick's Pizza.

if(strcmp(pizza_establishment, nicks)){
        price = NICKS_PIZZA;
        printf("%s had lunch today at %s.\n",customers, nicks_restaurant); 
    }
    else if(strcmp(pizza_establishment, rays)){
        price = RAYS_PIZZA;
        printf("%s had lunch today at %s.\n",customers, nicks_restaurant);
    }
    else if(strcmp(pizza_establishment, mikes)){
        price = MIKES_PIZZA;
        printf("%s had lunch today at %s.\n",customers, nicks_restaurant);
    }
    else{
        puts("An error occured while determining which restaurant was visited.");
    }

pepperoni
An error occured while determining the pizza type.


6139892

wonderw:~/lab4>

Another Edit: I realized that it jumps into the if above no matter what is entered and that I forgot to change my constants, not that it matters.

cost isn't initialized in that function. If you get to the else section of the if clauses then you won't assign cost and you will return garbage.

Well I've been able to narrow it down to figure out that

strcmp(pizza_establishment, nicks)

returns 1 even if pizza_establishment = "nicks"

I have no idea why or how to fix it.

Don't tell me I need quotes around nicks, because I don't. nicks is a constant for the string "nicks"

Check to make sure there are no excess spaces in either the nicks string or in the pizza_establishment one. I know it's a constant I saw your string schema up there. Otherwise print out the strings right before that point. Absolute worst case go through them both and get the ASCII for each character.

Check to make sure there are no excess spaces in either the nicks string or in the pizza_establishment one. I know it's a constant I saw your string schema up there. Otherwise print out the strings right before that point. Absolute worst case go through them both and get the ASCII for each character.

Thanks, that helped pinpoint the problem.. I can't figure out why quite yet, but somehow pizza_establishment is being replaced by the pizza_type.

I printed both the pizza_establishment, which is the string the user enters in, with the constant nicks, and I got the following.
(I added in dots to see if there were spaces before or after.)

...plain
nicks...1 this is the value strcmp returns

and I just figured out I screwed up my pizza_type check. My head has not been on straight today. In any case, that strcmp is not working properly either.

I have no idea what is going on, I definitely remember testing my code yesterday when I made it and now I've discovered even more errors.. my while loop to check if a restaurant name entered is one of the three doesn't work.

In any case, that strcmp is not working properly either.

Post your actual code containing all of your latest attempt. Trying to piece together what incremental changes you may or may not have made makes it more difficult for you and everybody else.

Odds are strcmp is working just fine; odds are you are doing things wrong in the attempt to use it.

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.