简体   繁体   English

C编程初学者问题

[英]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. 如果他们输入y,那么我希望循环继续,如果输入了其他任何字符,我希望它停止。 When I enter any character the program just continuously prints out all my printf statements. 当我输入任何字符时,程序将连续打印出我所有的printf语句。 I am using codeblocks. 我正在使用代码块。 Here is my code. 这是我的代码。 I get 2 warnings. 我收到2条警告。

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. 在第19行,while循环开始。

#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 您无法在c中以这种方式比较字符串,这可能是您的意思

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

but then you should fix the scanf() too 但是然后您也应该修复scanf()

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. 您的编译器确实在告诉您这是错误的,因为int和指针是不兼容的类型。

You are comparing an integer variable with character data. 您正在将integer变量与character数据进行比较。

Replace int keepGoing = "y"; 替换为int keepGoing = "y";

with

char keepGoing = 'y';

and replace 并更换

while (keepGoing == "y")

with while (keepGoing == 'y') 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 . 我花了一些时间,概述了Pizza Shop Menu周围的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. 而不是提示每个循环多少—仅在每个循环中显示一个小菜单,您在其中选择L代表大, S代表小, D代表饮料,然后填写数量。 ( 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. 同样,在每个scanf输入之后(格式字符串不占用newline ),将清空输入缓冲区,以防止scanf出现以跳过条目。 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.) 注意:如果有限制,则不包括其他头文件,因此,有更简单的方法可以使用strchr等检查有效条目。使用string.h等。)

#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
 ------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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