简体   繁体   中英

C programming begginner issue

Ok so I'm trying to build a basic program to calculate the price of a pizza order. I want it to ask if the customer is done ordering. If they enter y then I want the loop to continue, if any other character entered I want it to stop. When I enter any character the program just continuously prints out all my printf statements. I am using codeblocks. Here is my code. I get 2 warnings.

warning: initialization makes integer from pointer without a cast [enabled by default] at line 17 where i declare the keepgoing variable.

warning: comparison between pointer and integer [enabled by default]|

at line 19 where the while loop starts.

#include <stdio.h>
#include <stdlib.h>

main()
{
#define LARGEPIZZAPRICE
#define SMALLPIZZAPRICE
#define LARGEPIZZATOPPING
#define SMALLPIZZATOPPING
#define DRINK

int numberOfLargePizzas;
int numberOfSmallPizzas;
int numberOfLargeToppings;
int numberOfSmallToppings;
int numberOfDrinks;
int keepGoing = "y";

while (keepGoing == "y")
{
    printf("How many large pizza's do you want\n");
    scanf(" %d", &numberOfLargePizzas);

    printf("How many large toppings do you want\n");
    scanf(" %d", &numberOfLargeToppings);

    printf("How many small pizza's do you want\n");
    scanf(" %d", &numberOfSmallPizzas);

    printf("How many small toppings do you want\n");
    scanf(" %d", &numberOfSmallToppings);

    printf("Would you like to order more.  Enter a y or n\n");
    scanf(" %i", &keepGoing);
}



}`

*****UPDATE***** Ok thanks for all the help, it's running good now. If somebody can look at it and give any tips to tighten it up or do what i'm doing easier, please let me know. This is learning experience for me and I'm doing it through trial and error. The program runs but I have a feeling I'm structuring it wrong. Here's what I have:

#include <stdio.h>
#include <stdlib.h>

main()
{
#define LARGEPIZZAPRICE 12
#define SMALLPIZZAPRICE 10
#define LARGEPIZZATOPPING 2
#define SMALLPIZZATOPPING 1.50
#define DRINK  1.50
#define TAXRATE .05
int numberOfLargePizzas;
int numberOfSmallPizzas;
int numberOfLargeToppings;
int numberOfSmallToppings;
int numberOfDrinks;
char keepGoing ='y';
float largePizzaTotal;
float smallPizzaTotal;
float drinkTotal;

while (keepGoing == 'y')
{
        printf("How many large pizza's do you want\n");
        scanf(" %d", &numberOfLargePizzas);

    if(numberOfLargePizzas != 0){
        printf("How many large toppings do you want\n");
        scanf(" %d", &numberOfLargeToppings);
        }

        printf("How many small pizza's do you want\n");
        scanf(" %d", &numberOfSmallPizzas);
    if(numberOfSmallPizzas !=0){
        printf("How many small toppings do you want\n");
        scanf(" %d", &numberOfSmallToppings);
        }

        printf("How many drinks would you like\n");
        scanf(" %int", &numberOfDrinks);
        printf("Would you like to order more.  Enter a y or n\n");
        scanf(" %c", &keepGoing);

}
largePizzaTotal = (LARGEPIZZAPRICE*numberOfLargePizzas)+(LARGEPIZZATOPPING*numberOfLargeToppings);
smallPizzaTotal=(SMALLPIZZAPRICE*numberOfSmallPizzas)+(SMALLPIZZATOPPING*numberOfSmallToppings);
drinkTotal = DRINK*numberOfDrinks;

    printf("Subtotal: %2f", largePizzaTotal + smallPizzaTotal + drinkTotal);

}

You can't compare strings that way in c, probably you meant

char keepGoing = 'y';
if (keepGoing == 'y')

but then you should fix the scanf() too

scanf(" %c", &keepGoing);

The

int keepGoing = "y";

compiles well if you have compiler warnings disabled, but it's wrong.

Your compiler is indeed telling you that it's wrong, because int and pointer are incompatible types.

You are comparing an integer variable with character data.

Replace int keepGoing = "y";

with

char keepGoing = 'y';

and replace

while (keepGoing == "y")

with while (keepGoing == 'y')

or if you want to take care of cases, replace it with

while ( keepGoing == 'y' || keepGoing == 'Y')

You asked for tips to tighten up what you are doing. I took a bit of time and outlined an approach to your pizza shop that surrounds a Pizza Shop Menu . Rather than prompting for how many of each -- during every loop, this just presents a small menu where you choose L for large, S for small and D for drink, and then fill in the quantity. ( menu entries can be either upper or lower case ) Invalid menu selections are not allowed (no error is shown, the menu just redisplays)

Rather than waiting to sum and total at the end, I find it easier to simply keep a running total of each type sold, the subtotal, tax, etc.. You can compare both methods and see which you prefer.

Also, after each scanf input (where the format string does not consume the newline ), the input buffer is emptied to prevent scanf from appearing to skip over entries. Take a look and let me know if you have questions. There are many, many ways to do this, none more right than the next (as long as they are correct). It all comes down to format taste and efficiencies at that point. ( note: no additional header files were included in case that is a limitation, so there are easier ways to check for a valid entry with strchr , etc.. using string.h , etc.)

#include <stdio.h>
#include <stdlib.h>

/* defines are preprocessor commands that simply cause the
 * preprocessor to do a global search-and-replace of the
 * labels with values. They traditionally go at the top.
 * You can add storage type suffixes to the numbers to 
 * prevent ambiguity. (e.g. F float, U unsigned, ...)
 */
#define LARGEPIZZAPRICE 12.0F
#define SMALLPIZZAPRICE 10.0F
#define LARGEPIZZATOPPING 2.0F
#define SMALLPIZZATOPPING 1.50F
#define DRINK  1.50F
#define TAXRATE .05F

/* empty input buffer after reading from stdin */
void flush_stdin ()
{
    int c = 0;
    while ((c = getchar()) != '\n' && c != EOF);
}

int show_menu ()
{
    int c = 0;

    /* quick & dirty menu */
    printf ("\n Pizza Shop Menu\n\n");
    printf ("           L)  Large Pizza\n");
    printf ("           S)  Small Pizza\n");
    printf ("           D)  Drink\n");
    printf ("          --------------------\n");
    printf ("           C)  Checkout\n\n");
    printf (" Choice: ");

    c = getchar ();
    flush_stdin ();

    /* return only upper case letters */
    return c > 'Z' ? c - 'a' + 'A' : c;
}

int main (void)
{
    /* ALWAYS INITIALIZE ALL VARIABLES 
     * accidentally reading from an uninitialized variable
     * is Undefined Behavior.
     */
    int numberOfLargePizzas = 0;
    int numberOfSmallPizzas = 0;
    int numberOfLargeToppings = 0;
    int numberOfSmallToppings = 0;
    int numberOfDrinks = 0;
    float price = 0.0;
    float subtotal = 0.0;
    float tax = 0.0;

    while (1) 
    {
        int n = 0;

        switch (show_menu())
        {
            case 'L' :
                printf ("\n    How many large pizza's do you want : ");
                scanf ("%d", &n);
                flush_stdin ();
                if (n) 
                {
                    numberOfLargePizzas += n;
                    price = n * LARGEPIZZAPRICE;
                    tax += price * TAXRATE;
                    subtotal += price;
                    n = 0;
                    printf ("    How many large toppings do you want: ");
                    scanf ("%d", &n);
                    flush_stdin ();
                    if (n)
                    {
                        numberOfLargeToppings += n;
                        price = n * LARGEPIZZATOPPING;
                        tax += price * TAXRATE;
                        subtotal += price;
                    }
                }
                printf ("\n  Subtotal : %.2f\n       Tax : %.2f\n     Total : %.2f\n",
                        subtotal, tax, subtotal + tax);
                break;

            case 'S' :
                printf ("\n    How many small pizza's do you want : ");
                scanf ("%d", &n);
                flush_stdin ();
                if (n) 
                {
                    numberOfSmallPizzas += n;
                    price = n * SMALLPIZZAPRICE;
                    tax += price * TAXRATE;
                    subtotal += price;
                    n = 0;
                    printf ("    How many small toppings do you want: ");
                    scanf ("%d", &n);
                    flush_stdin ();
                    if (n)
                    {
                        numberOfSmallToppings += n;
                        price = n * SMALLPIZZATOPPING;
                        tax += price * TAXRATE;
                        subtotal += price;
                    }
                }
                printf ("\n  Subtotal : %.2f\n       Tax : %.2f\n     Total : %.2f\n",
                        subtotal, tax, subtotal + tax);
                break;

            case 'D' :
                printf ("\n    How many drinks would you like: ");
                scanf ("%d", &n);
                flush_stdin ();
                if (n)
                {
                    numberOfDrinks += n;
                    price = n * DRINK;
                    tax += price * TAXRATE;
                    subtotal += price;
                }
                printf ("\n  Subtotal : %.2f\n       Tax : %.2f\n     Total : %.2f\n",
                        subtotal, tax, subtotal + tax);
                break;

            case 'C' :
                printf ("\nOrder:\n");
                printf (" ------------------------\n");
                printf ("   Large Pizzas : %2d\n Large Toppings : %2d\n", numberOfLargePizzas, numberOfLargeToppings);
                printf ("   Small Pizzas : %2d\n Small Toppings : %2d\n", numberOfSmallPizzas, numberOfSmallToppings);
                printf ("         Drinks : %2d\n", numberOfDrinks );
                printf (" ------------------------\n");
                printf ("       Subtotal : %6.2f\n            Tax : %6.2f\n          Total : %6.2f\n",
                        subtotal, tax, subtotal + tax);
                printf (" ------------------------\n\n");
                return 0;

            default:
                /* uncomment to show bad menu entry */
                // printf (" <--invalid entry-->\n"); 
                break;
        }
    }

    return 0;
}

Example:

$ ./bin/pizzas

 Pizza Shop Menu

           L)  Large Pizza
           S)  Small Pizza
           D)  Drink
          --------------------
           C)  Checkout

 Choice: l

    How many large pizza's do you want : 2
    How many large toppings do you want: 2

  Subtotal : 28.00
       Tax : 1.40
     Total : 29.40

 Pizza Shop Menu

           L)  Large Pizza
           S)  Small Pizza
           D)  Drink
          --------------------
           C)  Checkout

 Choice: d

    How many drinks would you like: 4

  Subtotal : 34.00
       Tax : 1.70
     Total : 35.70

 Pizza Shop Menu

           L)  Large Pizza
           S)  Small Pizza
           D)  Drink
          --------------------
           C)  Checkout

 Choice: c

Order:
 ------------------------
   Large Pizzas :  2
 Large Toppings :  2
   Small Pizzas :  0
 Small Toppings :  0
         Drinks :  4
 ------------------------
       Subtotal :  34.00
            Tax :   1.70
          Total :  35.70
 ------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM