C编程初学者问题

时间:2015-04-01 20:49:02

标签: c

好的,所以我试图建立一个基本程序来计算披萨订单的价格。我想要询问客户是否订购了。如果他们输入y然后我希望循环继续,如果输入任何其他字符我想让它停止。当我输入任何字符时,程序只是连续打印出我的所有printf语句。我正在使用代码块。这是我的代码。我得到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]|

第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 ***** 好的,感谢所有的帮助,它现在运行良好。如果有人可以查看它并给出任何提示来收紧或做我更容易做的事情,请告诉我。这对我来说是学习经验,我通过反复试验来做到这一点。该程序运行,但我有一种感觉,我错误地构造它。这就是我所拥有的:

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

}

3 个答案:

答案 0 :(得分:2)

您无法比较c中的字符串,可能就是

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

但是你应该修复scanf()

scanf(" %c", &keepGoing);

int keepGoing = "y";
如果禁用编译器警告,

编译好,但是错误。

你的编译器确实告诉你它错了,因为int和指针是不兼容的类型。

答案 1 :(得分:0)

您正在将integer变量与character数据进行比较。

替换int keepGoing = "y";

char keepGoing = 'y';

并替换

while (keepGoing == "y")

while (keepGoing == 'y')

或者如果您想处理案件,请将其替换为

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

答案 2 :(得分:0)

你要求提出收紧你正在做的事情的提示。我花了一点时间,概述了围绕Pizza Shop Menu的披萨店的方法。这不是提示每个循环中有多少 - 而是在每个循环中提供一个小菜单,您可以选择L表示大,S表示小而D表示饮料,然后填写在数量上。 (菜单条目可以是大写或小写)不允许使用无效的菜单选项(不显示错误,菜单只是重新显示)

我没有等到最后总和和总数,而是更容易简单地保持每种类型的销售总额,小计,税金等。您可以比较两种方法并查看您喜欢的方式。

此外,在每个scanf输入(格式字符串不使用newline)之后,清空输入缓冲区以防止scanf出现跳过条目。如果您有任何疑问,请查看并告诉我。有许多方法可以做到这一点,没有比下一个更好的方法(只要它们是正确的)。这一切都归结为那时的品味和效率。 (注意:如果是限制,则不会包含其他标头文件,因此可以使用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;
}

示例:

$ ./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
 ------------------------