我的函数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
答案 0 :(得分:2)
在&
之前删除&符号n1, n2, n3
。
scanf
想要内存的地址,它应该从用户输入写入值。在这里,地址为n1
等。如果您只有int n1
,那么您需要获取n1
的地址,因此您需要使用&
}。但是,由于您有int * n1
,n1
已经拥有整数需要存储的地址。因此,无需使用&
。
答案 1 :(得分:0)
首先通过将返回值更改为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()
,我认为如果用product
类型定义变量long long int
会更安全一些,特别是因为它包含{{1}类型的3个整数的乘积1}}!通过这种方式,您将减少超出限制的概率!
当然不要忘记打印的int
(注意printf()
)
%lli
让我用一个必须牢记的笔记完成这个答案
希望它有所帮助!