【问题标题】:Function for inputting three integers in CC语言中输入三个整数的函数
【发布时间】:2015-01-25 22:55:57
【问题描述】:

我的函数 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 

【问题讨论】:

  • scanf("%i", &amp;n1); --> scanf("%i", n1);。同上。 displayLowest 也是错误的。
  • return n1, n2, n3; 表示return n3; 类型也不同。
  • 非常感谢!如何编写 displayLowest 的函数?
  • 你需要比较三个东西:n1n2n3。我不想放弃确切的代码来做到这一点。如果您有 3、4 和 5。您将比较前两个数字,并将其中最小的数字与第三个数字进行比较以获得最小的数字。
  • 如MIN(MIN(num1, num2), num3)

标签: c function pointers choice


【解决方案1】:

删除n1, n2, n3 之前的& 符号&amp;

scanf 想要从用户输入中写入值的内存地址。这里的地址是n1,等等。如果你只有int n1,那么你想得到n1的地址,所以你需要使用&amp;。但是,由于您有int * n1n1 已经有了需要存储整数的地址。所以,不需要使用&amp;

【讨论】:

  • 谢谢!我很感激!
【解决方案2】:

首先通过将返回值更改为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 个整数 a,b,c

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 会更安全一些,特别是因为它包含int 类型的3 个整数的乘积!这样你会降低超越极限的概率!

当然不要忘记用于打印的printf()(注意%lli

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

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

永远不要使用flush(stdin),因为这会导致你的程序出现未定义的行为

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 2022-01-23
    • 2010-10-08
    • 1970-01-01
    • 2020-07-15
    • 2019-01-13
    • 2011-10-12
    • 2016-08-19
    • 2015-09-15
    相关资源
    最近更新 更多