#include <stdio.h>
#include <stdlib.h>
int main() {
int item = 0;
int buyovst = 0;
while (buyovst == 0) {
printf("Do you want to 1. buy or 2.visit items or 3. to exit the shop?\n");
scanf_s(" %d", &buyovst);
if (buyovst == 1) {
printf("What do you want to buy? you can only choose between the 4 items.\n");
scanf_s(" %d", item);
if(item > 0){
if (item == 1) {
printf("You bought Chips in 12$.\n");
}
if (item == 2) {
printf("You bought Banana in 15$.\n");
}
if (item == 3) {
printf("You bought a Book in 20$.\n");
}
if (item == 4) {
printf("You bought a Pencil in 1$.\n");
}
}
buyovst = 0;
}
if (buyovst == 2) {
printf("There are 4 items on the shop.\n 1. Chips \t 12$\n 2.Banana \t 15$\n 3.Book \t 20$\n 4.Pencil \t 1$\n");
buyovst = 0;
}
if (buyovst == 3) {
printf("Goodbye!\n");
}
}
system("pause");
return 0;
}
出于某种原因,我收到了这个错误:
我正在尝试编写一个程序,询问用户是否
如果他选择1 - &gt;他得到 问他想买什么。如果他选择1,他会获得筹码2香蕉 等等...然后他回到第一个问题(如果要买访问 或退出)如果他选择2 - &gt;他看到了物品清单和他们的物品 价格。然后他回到第一个问题(如果要买访问或 出口)
答案 0 :(得分:3)
该行
scanf_s(" %d", item);
错了。将指针传递给变量而不是变量的值。
用
替换该行scanf_s(" %d", &item);
(在&
之前添加item
)