C++ 在scanf语句之后,我的程序不工作

C++ 在scanf语句之后,我的程序不工作,c++,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

我的代码在第二个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;

}
包括
内部主(空)
{
//介绍信息
printf(“此程序将接受客户的订单,然后从库存列表中减去他们的订单。计算后,将出现一个新的库存列表\n”);
//声明变量
面团、酱汁、奶酪、辣香肠、香肠、青椒、黑橄榄、蘑菇;
int num1;
炭比萨饼;
//创建库存
面团=50;
酱汁=50;
奶酪=50;
辣香肠=50;
香肠=50;
青椒=50;
黑橄榄=50;
蘑菇=50;
//显示库存
printf(“\n当前库存为:\n面团:%d”,面团);
printf(“\n酱汁:%d”,酱汁);
printf(“\n奶酪:%d”,奶酪);
printf(“\n意大利香肠:%d”,意大利香肠);
printf(“\n香肠:%d”,香肠);
printf(“\n青椒:%d”,青椒);
printf(“\n黑橄榄%d”,黑橄榄);
printf(“\n蘑菇:%d”,蘑菇);
//获取客户输入
printf(“\n您想要什么类型的比萨饼?请键入一个小写字母,并只选择一个。C型代表奶酪,P型代表辣香肠,S型代表香肠,V型代表蔬菜。\n”);
scanf_s(“%c”和比萨饼);
printf(“\n您要订购多少比萨饼?最多10个。\n”);
扫描单位(“%d”和num1);
//这就是它停止的地方
printf(“它在这里剪切,为什么在这行之后我看不到任何东西?”);
//计算要取出的库存量
奶酪=奶酪-num1;
面团=面团-num1;
酱汁=酱汁-num1;
开关(比萨饼)
{
案例“c”:
奶酪=50努姆1;
打破
案例“p”:
辣香肠=辣香肠-num1;
打破
案例s:
香肠=香肠-num1;
打破
案例“v”:
黑橄榄=黑橄榄-num1;
青椒=青椒-num1;
蘑菇=蘑菇-num1;
打破
}
//显示最终库存
printf(“新库存为:\n面团:%d”,面团);
printf(“\n酱汁:%d”,酱汁);
printf(“\n奶酪:%d”,奶酪);
printf(“\n意大利香肠:%d”,意大利香肠);
printf(“\n香肠:%d”,香肠);
printf(“\n青椒:%d”,青椒);
printf(“\n黑橄榄%d”,黑橄榄);
printf(“\n蘑菇:%d”,蘑菇);
返回0;
}

问题在于您使用的是
scanf
(或者它的近亲
scanf\u
)。
scanf
问题不适合交互式输入,会导致像您看到的那样令人困惑的结果


相反,使用
fgets
函数将数据读入字符数组。如果你需要把那个字符数组转换成一个数字,使用<代码> ATOI 。< /P>我对C++仍然是新的,看起来你根本就不使用<代码> C++ >代码>。这看起来像是
c
code。一个
c++
解决方案将完全不同。建议阅读:表达“不工作”太模糊了。输入是什么?预期产量是多少?实际产量是多少?它们有何不同?是否存在运行时错误?请用答案的文本编辑你的帖子;没有链接,没有屏幕快照。调试器。使用调试器。调试器允许您单步执行程序,查看变量中的值。通常,使用调试器比正确地向STAKOFF发布正确的消息,并等待某人检查代码或调试它。我将标记返回到C++,因为OP明确地提到它应该是C++。即使它看起来像C,如果OP使用C++编译器,那么它是C++(虽然不是最好的)为什么SCANF不适合交互式输入?我不知道它还能用来做什么。你是说用fget来输入字符还是int。或者两者都适用?@john:
scanf
有时适合从文件中读取格式化输入。但是,当以交互方式使用时,它会一直读取所有内容,直到您按Enter键,但不会消耗所有内容-并将未消耗的内容缓冲到下一次调用
scanf
@N.Rice:我建议在所有情况下都使用
fgets
,而不是
scanf
进行交互输入。您也可以将其用于字符输入,只需查看用户键入的行的第一个字符即可。@Greg Hewgill fgets也可以使用,而无需在底部添加最后两行代码,因此感谢您向我展示该方法。