在C中输入三个整数的函数

x4shl7ld  于 2023-01-04  发布在  其他
关注(0)|答案(2)|浏览(138)

我的函数getThreeIntegers遇到了问题。我如何正确地用指针编写它并让它返回结果?当我运行它时,所有内容都显示为0。我正在努力编写正确的格式。

#include <stdlib.h>
#include <stdio.h>
#define pause system("pause")
#define cls system("cls")
#define flush fflush(stdin)

//Prototype Functions Here
void displayAverage(int n1, int n2, int n3);
void displayLowest(int n1, int n2, int n3);
void displayProduct(int n1, int n2, int n3);
void displaySum(int n1, int n2, int n3);
void getMenu();
char getUserChoice();
int getThreeIntegers(int *num1, int *num2, int *num3);

main() {
        //Declare Variables Here
        char choice = ' '; //used by menu
        int num1 = 0, num2 = 0, num3 = 0;

        do{
        choice = getUserChoice();
        switch(choice) {
            case 'A':
                getThreeIntegers(&num1, &num2, &num3);
                pause;
                break;
            case 'B':
                displaySum(num1, num2, num3);
                pause;
                break;
            case 'C':
                displayProduct(num1, num2, num3);
                pause; 
                break;
            case 'D': 
                displayAverage(num1, num2, num3);
                pause;
                break;
            case 'E':
                displayLowest(num1, num2, num3);
                pause;
                break;
            case 'F':
                printf("Goodbye! \n\n");
                pause;
                break;
            default:
                printf("Invalid Selection...\n\n");
                pause;
                break;
        } // end switch
    }while(choice != 'F');
} // end main

void displayAverage(int num1, int num2, int num3) {
    float average = 0.0;
    int sum = num1 + num2 + num3; 
    average = sum/3; 

        printf("The average of the integers is: %.2lf\n", average);

} //end displayAverage

void displayLowest(int num1, int num2, int num3) {
    int lowest;
    int a = 0, b = 0, c = 0; 

    if(a < b && a < c)
            lowest = a;
    printf("The lowest integer is: %i\n", a);
} // end displayLowest

void displayProduct(int num1, int num2, int num3) {
    int product;
    product = num1*num2*num3;
    printf("The product of the integers is: %i\n", product);
} // end displayProduct

void displaySum(int num1, int num2, int num3) {
    int sum;
    sum = num1 + num2 + num3;
    printf("The sum of the integers is: %i\n", sum);

} // end displaySum

void getMenu() { //displays menu onto output
    cls;
    printf("\t MAIN MENU\n");
    printf("*************************\n\n");
    printf("A. Enter three integers.\n");
    printf("B. Display the sum.\n");
    printf("C. Display the product.\n");
    printf("D. Display the average. \n");
    printf("E. Display the lowest number. \n");
    printf("F. Quit. \n\n");
    printf("*************************\n");
    printf("Enter user choice: "); 
    return; 
} // getMenu

char getUserChoice(){
   char result;
   getMenu(); //call to function
   scanf("%c", &result); //user enters their choice
   result = toupper(result); //converts char to capital
   flush;
   return result;
} // end getChoice

int getThreeIntegers(int *n1, int *n2, int *n3) {
    printf("Enter first integer: ");
    scanf("%i", &n1);
    printf("Enter second integer: ");
    scanf("%i", &n2);
    printf("Enter third integer: ");
    scanf("%i", &n3);
    flush;
    return n1, n2, n3; 
} //end getThreeIntegers
roejwanj

roejwanj1#

删除n1, n2, n3前面的与号&
scanf需要存储器的地址,它应该将用户输入的值写入其中。这里,地址是n1,等等。如果您只有int n1,那么您需要获得n1的地址,因此您需要使用&。但是,由于您有int * n1n1已经有了需要存储整数的地址,所以不需要使用&

liwlm1x9

liwlm1x92#

首先,通过将返回值更改为void而不是int来更改函数的原型,因为这些值是通过其地址传递的,因此所做的任何更改都将永久影响它们

void  getThreeIntegers(int *n1, int *n2, int *n3);

现在你需要在函数块内部做如下的改变

void  getThreeIntegers(int *n1, int *n2, int *n3) { // change the return value to void 
    printf("Enter first integer: ");
    //you need to pass the address of the variable in scanf()
    //and as n1 hold the address you pass like it is 
    // delete the & sign from every variable in `scanf()`
    scanf("%i", n1);
    printf("Enter second integer: ");
    scanf("%i", n2);
    printf("Enter third integer: ");
    scanf("%i", n3);
    //flush;           // delete this line
    //return n1, n2, n3; // no need for return as the variable are passed by reference 
     // 
} //end getThreeIntegers

如果你定义了3个整数abc

int a=0,b=0,c=0;

此时函数的调用将为:

getThreeIntegers(&a,&b,&c);

关于函数displayLowest,你想从三个整数中得到最小的变量,所以我们的想法是比较两个整数,然后你需要比较它们之间的最小值和第三个,所以这里是你如何用三元运算符?:把它写在一行中

void displayLowest(int num1, int num2, int num3) {

     printf("The lowest integer is: %i\n", (num1 < num2 ? num1 : num2) < num3 ? (num1 < num2 ? num1 : num2) : num3);

} // end displayLowest

对于函数displayProduc(),我认为如果你用long long int类型定义变量product会更安全一些,特别是因为它保存了3个int类型的整数的乘积!这样你会减少超出极限的概率!
当然,不要忘记用于打印的printf()(注意%lli

printf("The product of the integers is: %lli\n", product);

让我用一个必须牢记的注解来结束这个回答

切勿使用flush(stdin),因为这会导致程序出现未定义的行为

希望能有所帮助!

相关问题