gpt4 book ai didi

c++ - 我的程序在 scanf 语句后不工作

转载 作者:行者123 更新时间:2023-11-30 05:04:36 24 4
gpt4 key购买 nike

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

}

最佳答案

问题是您正在使用 scanf(或其同类 scanf_s)。 scanf 问题适合交互式输入,会导致像您所看到的那样令人困惑的结果。

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

关于c++ - 我的程序在 scanf 语句后不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com