我的程序在scanf语句之后不起作用

时间:2018-02-15 17:11:13

标签: c++

我的代码在第二个scanf语句之后没有工作。我还是C ++的新手,所以我不确定我做错了什么。我和其他人一样看过这个网站,到目前为止还没找到任何东西。我也尝试了fflush声明,但也没有解决它,任何其他建议将非常感谢。

include <stdio.h>

int main(void)
{
    //Intro message
    printf("This program will take the order of a customer and then subtract their order from the inventory list. After calculations a new inventory list will appear\n");

// declare variables
int dough, sauce, cheese, pepperoni, sausage, greenPeppers, blackOlives, mushrooms;
int num1; 
char pizza;

// create inventory
dough = 50;
sauce = 50;
cheese = 50;
pepperoni = 50;
sausage = 50;
greenPeppers = 50;
blackOlives = 50;
mushrooms = 50;
//display the inventory
printf("\n The currrent inventory is: \n Dough:%d", dough);
printf("\n Sause:%d", sauce);
printf("\n Cheese:%d", cheese);
printf("\n Pepperoni:%d", pepperoni);
printf("\n Sausage:%d", sausage);
printf("\n Green Peppers:%d", greenPeppers);
printf("\n Black Olives%d", blackOlives);
printf("\n Mushrooms:%d", mushrooms);

//get customer input

printf("\nWhat type of pizza would you like? Please type a lowercase letter and only chose one. Type C for Cheese, P for Pepperoni, S for Sausage, and V for Veggie.\n");
scanf_s(" %c", &pizza);

 printf("\nHow many pizzas would you like to order? Maximum of 10.\n");
 scanf_s("  %d", &num1);

//This is where it stops
 printf("It cuts out here, why can I not see anything after this line?");


// calculate what inventory will be taken out
cheese = cheese - num1;
dough = dough - num1;
sauce = sauce - num1;
switch (pizza)
{
case 'c': 
    cheese = 50 - num1;
    break;
case 'p':
    pepperoni = pepperoni - num1;
    break;
case 's':
    sausage = sausage - num1;
    break;
case 'v':
    blackOlives = blackOlives - num1;
    greenPeppers = greenPeppers - num1;
    mushrooms = mushrooms - num1;
    break; 

}

// display final inventory

printf("The new inventory is: \n Dough:%d", dough);
printf("\n Sause:%d", sauce);
printf("\n Cheese:%d", cheese);
printf("\n Pepperoni:%d", pepperoni);
printf("\n Sausage:%d", sausage);
printf("\n Green Peppers:%d", greenPeppers);
printf("\n Black Olives%d", blackOlives);
printf("\n Mushrooms:%d", mushrooms);


return 0;

}

1 个答案:

答案 0 :(得分:1)

问题是您使用的是scanf(或其堂兄scanf_s)。 scanf问题 不适用于交互式输入,并导致令人困惑的结果,例如您所看到的。

相反,使用fgets函数将数据读入字符数组。如果需要将该字符数组转换为数字,请使用atoi

相关问题