- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码有很多问题,并且有人尝试向我解释它,但我仍然无法理解我做错了什么......这一切在我的脑海中似乎都是正确的。据我了解,我的字符扫描不正确。我也有返回值的问题,并且我的“int * 与 int 的间接级别不同”。我该如何修复我的代码?我完全迷失了。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH flush()
// PROTOTYPE FUNCTIONS
void displayMenu();
void flush();
char getUserChoice();
int newSale(int *price, char *firstTime, char *veteran, char *student, char *lastDay);
int outputPrice(int carSales[], int *carsSold, int *avgCarPrice, int *totalRevenue);
// MAIN
main() {
int carSales[500] = { 0 };
int price = 0, avgCarPrice = 0, totalRevenue = 0, carsSold = 0;
char firstTime = 'n', veteran = 'n', student = 'n', lastDay = 'n';
char userSelection;
do {
userSelection = getUserChoice();
switch (userSelection) {
case 'A': // ENTER A NEW SALE
newSale(&price, &firstTime, &veteran, &student, &lastDay); // call function to ask questions
printf("\nPRICE OF CAR: %i\n", price);
carsSold++; // will add to +1 to the amount of cars sold
carSales[carsSold] = price; // add the car to carSales array
PAUSE;
break;
case 'B': // OUTPUT STATS
outputPrice(&carSales[500], &carsSold, &avgCarPrice, &totalRevenue);
PAUSE;
break;
case 'Q': // Quit Program
printf("Thanks....\n");
break;
default: // Invalid Selection
printf("Invalid selection...try again!\n");
PAUSE;
break;
} // end switch
} while (userSelection != 'Q');
PAUSE;
} // end of main
// DISPLAY THE MENU
void displayMenu() {
CLS;
printf("========== MAIN MENU ==========\n");
printf("A. ENTER NEW CAR\n");
printf("B. OUTPUT STATS\n");
printf("Q. QUIT\n");
printf("Enter your choice: ");
} // end displayMenu
// FLUSH
void flush() {
while (getchar() != '\n');
} // end flush
// GET THE USER CHOICE
char getUserChoice() {
char result;
displayMenu();
scanf("%c", &result); FLUSH;
result = toupper(result);
return result;
} // end getUserChoice
int newSale(int *price, char *firstTime, char *veteran, char *student, char *lastDay) {
// ASK QUESTIONS
printf("What is the sticker price of the car?\n");
scanf("%i", &price);
printf("Are you a first time buyer? (y/n)\n");
scanf("%s", firstTime);
printf("Are you a veteran? (y/n)\n");
scanf("%s", veteran);
printf("Are you a student (y/n)\n");
scanf("%s", student);
printf("Is it the last day of the month? (y/n)\n");
scanf("%s", lastDay);
// CALCULATE PRICES
if (*lastDay == 'y') { // car is discounted by 3% if user said yes
*price = *price - (((int)(*price) * 3) / 10); // (int) is added to keep the cocmpiler from complaining due to an implicit cast to floating point.
}
if (*firstTime == 'y') { // if it's the user's first time buying, $100 is given in credit
*price = *price - 100;
}
if (*student == 'y' && firstTime == 'y') { // car is discounted by $200 if user is a student and first time buyer- they also recieve the other discounts
*price = *price - ((int)(*price) - 200);
}
if (*veteran == 'y') { // veterans recieve 2% off the final price of the car
*price = *price - (((int)(*price) * 2) / 10);
}
return price;
}
int outputPrice(int carSales[ ], int *carsSold, int *avgCarPrice, int *totalRevenue) {
printf("The total cars sold is: %i", carsSold); // Display the amount of cars sold
for (int i = 0; i < 500; ++i) { // add all the prices in carSales
*totalRevenue = *totalRevenue + carSales[i];
}
printf("The Average car sold price: %i", avgCarPrice); // Display the avg price of the cars
printf("Total revenue: %i", totalRevenue); // Display total revenue
return;
}
最佳答案
首先,outputPrice
函数的返回类型应该是void
而不是int
;当我最初尝试编译您的代码时,这给了我一个错误。
接下来,在 newSale
函数中,该行
scanf("%i", &price);
应改为
scanf("%i", price);
因为price
已经是一个指针。在其他四个 scanf
调用中,%s
应更改为 `%c;例如
scanf("%s", firstTime);
应改为
scanf("%c", firstTime);
%s
是字符串的格式说明符; %c
用于字符。另请注意,参数仍然不需要通过在其名称前添加 &
来引用,因为它们也已经是指针。第三个 if
语句的第一行应更改为
if (*student == 'y' && firstTime == 'y') {
至
if (*student == 'y' && *firstTime == 'y') {
请注意 firstTime
前面的 &
。这是必要的,因为 firstTime
是一个指针,而不是字符,因此将其与字 rune 字 'y'
进行比较将产生不正确的结果结果。但是,firstTime 是指向字符的指针,因此使用 *
取消引用它会返回一个字符,然后可以将其与字 rune 字进行比较,而不会出现问题。 return语句也有问题:
return price;
应该是
return *price;
因为函数的返回类型是int
,而price是一个int
指针。
在 outputPrice
函数中,所有 printf
语句中的参数都需要取消引用。例如
printf("The total cars sold is: %i", carsSold);
应该是
printf("The total cars sold is: %i", *carsSold);
因为 carsSold
是一个 int
指针,而不是 int
本身。
此外,需要为 main
指定返回类型 int
以避免编译器警告。
关于c - scanf 格式字符串需要类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48455657/
我的运动需要一些帮助。 Question of exercise 这是我的代码: #include main() { int price,new_price; char code; printf(
这个问题在这里已经有了答案: scanf() leaves the newline character in the buffer (7 个答案) 关闭 5 年前。 我有这段代码(由于逻辑是家庭作业
我这里有一段代码,在输入带空格的字符串时会出现一些不需要的行为。例如 print illegal_argument。当输入包含多个单词时,我希望它只注册第一个单词。 目前发生的是: christian
我的测试输入是 [1,2,3] [4,5,6] [7,8,9] [9,9] 或者它可以是单个 2d 坐标之前的任意数量的 3d 坐标,我的代码是 #include #include #includ
这个问题在这里已经有了答案: What does space in scanf mean? [duplicate] (6 个答案) 关闭 7 年前。 像这样在scanf中加一个空格的目的是什么 sc
#include int main() { int n; scanf("%d", &n); printf(n); return 0; } 这是我的代码,我很愚蠢,可能
程序要求用户输入 2 名学生的姓名、ID 等,将数据写入文件并从文件中读回数据。 #include #include #include #include #include int main(
这个问题在这里已经有了答案: abnormal behavior of scanf [duplicate] (3 个答案) 关闭 8 年前。 今天我遇到了一个问题,当我使用两次接受字符作为输入的 s
我正在用 c 编写一个程序来读取文件的内容。代码如下: #include void main() { char line[90]; while(scanf("%79[^\
main() { int d,a; printf("Enter the digit :"); scanf("%d",&d); printf("Enter another
为什么为 scanf 输入添加多个句点会跳过下一个 scanf 函数。示例输入:b. b. int main (void) { char b[7] = " "; printf("Thi
我想我已经尝试过任何方法(刷新 stdin、scanf 以使用换行符等),但没有任何效果如我所希望的那样。由于某种原因,第三个 scanf 在以下代码中修改了第二个 scanf 中的变量: #incl
这个问题在这里已经有了答案: Scanf skips every other while loop in C (10 个答案) 关闭 6 年前。 我想做一个计算器,只是一个带有循环和基本操作的简单计
如果没有初始化 ("mod=0") ,这段代码进入死循环。我不明白为什么这段代码会循环,即使我使用了 getchar();删除缓冲区。当我先输入“1”,然后再输入“a”时,就会出现无限循环。谁能帮助我
当我为 scanf() 输入一个值时 它只是跳过紧随其后的第二个、第三个和任何其他 scanf()。 这是我的代码: #define _CRT_SECURE_NO_WARNINGS #include
#include #include void sstring(); int main() { char ch1[10],ch2; printf("Ent
这是一个计算房间内人员年龄的简单程序。我正处于初始阶段,现在我看到我不知道哪些变量(我的意思是我在 scanf 之前声明的变量,然后是 scanf 中的占位符)用于 scanf;如何选择和应用正确的变
这个问题在这里已经有了答案: scanf() leaves the newline character in the buffer (7 个答案) 关闭 5 年前。 我知道 scanf() 的用法,
我已经阅读了扫描集的行为。通过研究和测试,我遇到了一个问题。 难道 scanf("%[^\n]") 的行为与 scanf("%s") 一样吗? scanf("%s") 正在消耗字符,直到在 stdin
我遇到了一个问题,当我使用 scanf 将字符串存储到 char 指针中时,我有 3 个输入 - 姓名、姓氏和年龄,姓氏的最后一个 char 值被替换为年龄输出以更好地解释。 Q-riosity v0
我是一名优秀的程序员,十分优秀!