我的程序在SCANF语句之后不起作用
My program isnt working after the scanf statement
我的代码在第二个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 appearn");
// 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;
}
问题是您使用的是scanf
(或其堂兄scanf_s
(。scanf
问题是不适合交互式输入的,导致您看到的结果令人困惑。
而是使用fgets
函数将数据读取到字符数组中。如果您需要将该字符数组转换为一个数字,请使用atoi
。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- 将指针传递给函数在简单的情况下有效,但在"class" -izing 之后不起作用
- 我的程序在SCANF语句之后不起作用
- OpenCV Canny 之后的 ConvexPoly 不起作用
- 我的 for 循环之后的代码不起作用,for 循环后的打印语句不会被打印
- % 在 rand() c++ 之后不起作用
- 字符串运算符 += 有时在 string::resize() 之后不起作用
- setOverrideCursor(Qt::WaitCursor) 如果在消息框之后调用,则不起作用
- 在 msgget() 之后,cout 或 printf 不起作用
- 为什么你完成我在点之后不起作用
- 在 Ctrl+Z (EOF) 之后从 iostream::cin 恢复读取?("ignore"不起作用)
- cout/stdout在execvp()之后不起作用
- 在C++中读取一行之后的行不起作用
- 为什么我的第一个 if 语句有效,但我的 else if 语句在它之后不起作用
- 为什么 exec() 在 chroot() 之后不起作用?
- "fwrite"在"fread"之后不起作用?
- cin.getline() 在 cin 之后不起作用