我对 C++ 还很陌生,我必须制作一个 C++ 程序,使用按引用传递对 3 个数字(最小-最大)进行排序。一切正常,直到我的函数中的 if 语句。我尝试进行大量调试,似乎每当我使用“<”时,该语句都不会执行。如果我这样做,则无论如何都会执行(n1 > n2)
该语句。如果有人可以提供帮助,那就太好了。这是我的代码:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
int sortNum(double &n1, double &n2, double &n3);
int main(int argc, char *argv[]) {
double num1, num2, num3;
printf("Welcome to Taylor's Sorting Program \n");
printf("Enter 3 numbers and the program will sort them \n");
printf("Number 1: \n");
scanf("%d", &num1);
printf("Number 2: \n");
scanf("%d", &num2);
printf("Number 3: \n");
scanf("%d", &num3);
sortNum(num1, num2, num3);
printf("Sorted Values \n");
printf("Number 1: %d ", num1);
printf("\t Number 2: %d ", num2);
printf("\t Number 3: %d \n", num3);
system("PAUSE");
return EXIT_SUCCESS;
}
int sortNum(double &num1, double &num2, double &num3) {
double n1, n2, n3;
n1 = num1;
n2 = num2;
n3 = num3;
if (n1 < n2 && n1 > n3) {
num1 = n2;
num2 = n1;
num3 = n3;
} else if (n2 < n1 && n2 > n3) {
num1 = n3;
num2 = n2;
num3 = n1;
} else if (n3 < n2 && n3 > n1) {
num1 = n2;
num2 = n3;
num3 = n1;
}
return 0;
}