【发布时间】:2018-01-26 04:06:18
【问题描述】:
我的代码有很多问题,并且有人尝试向我解释,但我仍然无法理解我做错了什么......这一切似乎都在我的脑海中。据我了解,我的 char 没有正确扫描。我也有返回值的问题,而且我的“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;
}
【问题讨论】:
-
PAUSE、CLS和FLUSH宏似乎有点没用。 -
scanf("%i", &price);这是太多的间接性。scanf需要一个指针来扫描..price已经是一个指针,它是一个int*。写&price是int**,而不是scanf想要的。 -
谢谢你帮助我更好地理解这部分,我在几分钟前已经修复了它。
-
scanf("%s", firstTime);..%s是输入char的错误格式说明符。%s用于字符串,%c用于单数chars 就像你一样。顺便说一句,这里你有正确的间接级别,因为firstTime是char*。我建议阅读scanf联机帮助页。另外,在使用scanf时要小心你的空格:stackoverflow.com/questions/13542055/… -
6 小时!我不希望我能在现实世界中编写任何代码,并在 6 小时内完成。经常发生,特别是如果我“继承”了代码,并且项目有任何真正的复杂性,那么 6 个月会更现实,需要编写、测试和记录大约 100,000 行代码。
标签: c