简体   繁体   English

C ++ if / else if语句不起作用

[英]C++ if/else if statement not working

I'm fairly new to C++ and I have to make a C++ program that sorts 3 numbers (smallest-greatest) using pass by reference. 我是C ++的新手,我必须制作一个C ++程序,使用传递引用对3个数字(最小 - 最大)进行排序。 Everything works fine up until my if statement in my function. 一切正常,直到我的函数中的if语句。 I have tried to do a lot of debugging and it seems like whenever I use "<" the statement doesn't execute. 我试图做很多调试,似乎每当我使用“<”时语句都不会执行。 If I do (n1 > n2) the statement executes no matter what. 如果我这样做(n1 > n2) ,无论如何都会执行该语句。 If anyone can help that would be great. 如果有人能提供帮助,那就太棒了。 Here is my code: 这是我的代码:

#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;
}

You are using the wrong format specifier for scanf() . 您使用的是scanf()的错误格式说明符。 Instead of %d (signifying a pointer to an integer), use %lf (Pointer to a double ). 而不是%d (表示指向整数的指针),使用%lf (指向double指针)。 Alternately, change double to int in your code. 或者,在代码中将double更改为int

Note that your printf() statements suffer from a similar problem, and should be using either %f , %g or %e . 请注意,您的printf()语句遇到类似的问题,应该使用%f%g%e

There are 6 possible orders (permutations) of three different numbers. 有三种不同数字的6种可能的顺序(排列)。

Your code appears to check for only 3 of those, namely, in order, 您的代码似乎只检查其中的3个,即按顺序

n3 < n1 < n2 n3 <n1 <n2

and

n3 < n2 < n1 n3 <n2 <n1

and

n1 < n3 < n2 n1 <n3 <n2

In the 3 other cases you just return 0 and that's it. 在其他3个案例中,你只return 0 ,就是这样。

From my systematic listing above, can you guess what the three other cases have in common, how they're similar? 从我上面的系统列表中,您能猜出其他三个案例的共同点,它们是如何相似的吗?


By the way, not asked for, but you'll save yourself from tons of trouble by using C++ cin and cout instead of low level C functions scanf and printf . 顺便说一下,没有要求,但你可以通过使用C ++ cincout而不是低级C函数scanfprintf来避免麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM