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

时间:2022-03-19 18:32:29

Having lots of issues with my code, and have had people try explaining it to me but I still can't understand what I'm doing wrong... it all seems right in my head. As far as I understand, my char is not scanning correctly. I also have issues with returning values, and my "int * differs in levels of indirection from int". How can I fix my code? I'm completely lost.

我的代码有很多问题,有人试图向我解释,但我还是不明白我做错了什么……这一切在我的脑海里似乎都是对的。据我所知,我的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 个解决方案

#1


0  

First of all, the outputPrice function should have return type void rather than int; this gave me an error when I tried to compile your code originally.

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

Next, in the newSale function, the line

接下来,在newSale函数中,这条线。

scanf("%i", &price);

should be changed to

应该更改为

scanf("%i", price);

because price is already a pointer. In the other four scanf calls, %s should be changed to `%c; e.g.

因为价格已经是一个指针。在其他四个scanf调用中,%s应改为' %c;如。

scanf("%s", firstTime);

should be changed to

应该更改为

scanf("%c", firstTime);

%s is the format specifier for a string; %c is for characters. Also note that the arguments still do not need to be referenced by prepending a & to their names, because they are also already pointers. The first line of the third if statement should be changed from

%s是字符串的格式说明符;% c字符。还要注意,参数仍然不需要通过prepending a和它们的名称来引用,因为它们也已经是指针了。第三个if语句的第一行应该从

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

to

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

Notice the & prepended to firstTime. This is necessary because firstTime is a pointer, not a character, and so comparing it to the character literal 'y' is going to yield incorrect results. However, firstTime is a pointer to a character, and so dereferencing it with * returns a character, which can then be compared to the character literal without a problem. The return statement also has a problem:

注意第一次&的前缀。这是必要的,因为firstTime是一个指针,而不是一个字符,因此将它与字符文字“y”进行比较将产生不正确的结果。然而,firstTime是一个指向字符的指针,因此使用*取消引用将返回一个字符,然后可以将其与字符文字进行比较,而不会产生任何问题。退货声明还有一个问题:

return price;

should be

应该是

return *price;

because the return type of the function is int, and price is an int pointer.

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

In the outputPrice function, the arguments in all of your printf statements need to be dereferenced. E.g.

在outputPrice函数中,需要取消所有printf语句中的参数。如。

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

should be

应该是

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

because carsSold is an int pointer, not an int itself.

因为carsSold是一个int指针,而不是int本身。

Also, main needs to be given a return type of int to avoid a compiler warning.

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

#1


0  

First of all, the outputPrice function should have return type void rather than int; this gave me an error when I tried to compile your code originally.

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

Next, in the newSale function, the line

接下来,在newSale函数中,这条线。

scanf("%i", &price);

should be changed to

应该更改为

scanf("%i", price);

because price is already a pointer. In the other four scanf calls, %s should be changed to `%c; e.g.

因为价格已经是一个指针。在其他四个scanf调用中,%s应改为' %c;如。

scanf("%s", firstTime);

should be changed to

应该更改为

scanf("%c", firstTime);

%s is the format specifier for a string; %c is for characters. Also note that the arguments still do not need to be referenced by prepending a & to their names, because they are also already pointers. The first line of the third if statement should be changed from

%s是字符串的格式说明符;% c字符。还要注意,参数仍然不需要通过prepending a和它们的名称来引用,因为它们也已经是指针了。第三个if语句的第一行应该从

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

to

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

Notice the & prepended to firstTime. This is necessary because firstTime is a pointer, not a character, and so comparing it to the character literal 'y' is going to yield incorrect results. However, firstTime is a pointer to a character, and so dereferencing it with * returns a character, which can then be compared to the character literal without a problem. The return statement also has a problem:

注意第一次&的前缀。这是必要的,因为firstTime是一个指针,而不是一个字符,因此将它与字符文字“y”进行比较将产生不正确的结果。然而,firstTime是一个指向字符的指针,因此使用*取消引用将返回一个字符,然后可以将其与字符文字进行比较,而不会产生任何问题。退货声明还有一个问题:

return price;

should be

应该是

return *price;

because the return type of the function is int, and price is an int pointer.

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

In the outputPrice function, the arguments in all of your printf statements need to be dereferenced. E.g.

在outputPrice函数中,需要取消所有printf语句中的参数。如。

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

should be

应该是

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

because carsSold is an int pointer, not an int itself.

因为carsSold是一个int指针,而不是int本身。

Also, main needs to be given a return type of int to avoid a compiler warning.

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