gpt4 book ai didi

c - scanf 格式字符串需要类型参数

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

我的代码有很多问题,并且有人尝试向我解释它,但我仍然无法理解我做错了什么......这一切在我的脑海中似乎都是正确的。据我了解,我的字符扫描不正确。我也有返回值的问题,并且我的“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/

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