C编程begginner问题 [英] C programming begginner issue

查看:245
本文介绍了C编程begginner问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我想建立一个基本的程序来计算披萨订单的价格。我希望它问,如果客户完成订购。如果他们进入则y我希望循环继续,如果其他任何输入的字符我​​希望它停下来。当我输入任何字符程序只是不断地打印出我所有的printf语句。我使用$ C $的cblock。这里是我的code。我得到2警告。

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

在第19行,其中while循环开始。

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

}

推荐答案

您不能比较C的方式,也许你的意思的字符串

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

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

但你应该修正 scanf()的

scanf(" %c", &keepGoing);

int keepGoing = "y";

编译好,如果你有编译器警告无效,但它是错误的。

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

您的编译器确实告诉你,这是错误的,因为 INT 和指针是不兼容的类型。

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

这篇关于C编程begginner问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆