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

时间:2018-01-26 04:06:18

标签: c

我的代码存在很多问题,让人们尝试向我解释但我仍然无法理解我做错了什么......这一切似乎都在我脑海中。据我所知,我的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;
}

1 个答案:

答案 0 :(得分:0)

首先,outputPrice函数应该返回类型void而不是int;当我最初尝试编译代码时,这给了我一个错误。

接下来,在newSale函数中,行

scanf("%i", &price);

应改为

scanf("%i", price);

因为price已经是指针。在其他四个scanf调用中,%s应更改为`%c; e.g。

scanf("%s", firstTime);

应改为

scanf("%c", firstTime);

%s是字符串的格式说明符; %c适用于角色。另请注意,参数仍然不需要通过在&前加上它们的名称来引用,因为它们也已经是指针。应该从

更改第三个if语句的第一行
if (*student == 'y' && firstTime == 'y') {

if (*student == 'y' && *firstTime == 'y') {

请注意&前置firstTime。这是必要的,因为firstTime指针,而不是字符,因此将其与字符文字'y'进行比较将产生不正确的结果。但是,firstTime是指向字符的指针,因此使用*取消引用它会返回一个字符,然后可以将该字符与字符文字进行比较而不会出现问题。 return语句也有问题:

return price;

应该是

return *price;

因为函数的返回类型是int,而price是int指针。

outputPrice函数中,需要取消引用所有printf语句中的参数。 E.g。

printf("The total cars sold is: %i", carsSold);

应该是

printf("The total cars sold is: %i", *carsSold);

因为carsSoldint指针,而不是int本身。

此外,main需要返回int的返回类型以避免编译器警告。