找出一组整数中最小的一个 C++

find the smallest of a set of integers c++

#include <stdio.h>
int main () {
    int n, smNum = 1;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n != 0 || smNum != 0){
        printf("Enter a number: ");
        scanf("%d", &smNum);
            if (smNum< n)   smNum = n;
    }
    printf("The smallest number is: %d", n);
}

这个程序必须确定最小的提示数并打印出来。当用户输入数字“0”时,程序必须停止。那是发生错误的时候。程序一直要求输入。

首先,代码看起来比 C++ 更像 C。您应该在 C++ 中使用 <iostream> 类,而不是 <stdio.h> 函数。无论哪种方式,您都应该在对用户采取行动之前验证用户实际输入了有效数字。

话虽这么说...

int n, smNum = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0 || smNum != 0){

无论用户输入什么作为他们的第一次输入,循环将始终至少进入 1 次,因为表达式 smNum != 0 在第一次迭代时始终为真,因为 smNum被初始化为 1 而不是 0.

printf("Enter a number: ");
scanf("%d", &smNum);
if (smNum< n)   smNum = n;

在循环内,无论用户在后续输入中输入什么,n 将始终是第一个输入的值,因此表达式 smNum < n 仅对新输入为真低于第一个输入,但在这种情况下 smNum 将被第一个输入的值覆盖。

如果用户第一次输入 0,循环将继续,直到用户再次输入 0n 将始终为 0,而 smNum 将始终为 0 或正数,因为负数输入将被丢弃。

如果用户第一次输入没有输入0,循环将永远继续下去,因为n永远不会是0,所以表达式n != 0永远是真的。

printf("The smallest number is: %d", n);

您应该打印出 smNum,而不是 n

话虽这么说,但试试更像这样的东西:

#include <stdio.h>

int askForNumber(const char *msg)
{
    int n, ret;
    printf("%s: ", msg);
    do {
        ret = scanf("%d", &n);
        if (ret == 1) break;
        if (ret == 0){
            while ((ret = getchar()) != EOF && ret != '\n');
        }
        if (ret == EOF) exit(0);
        printf("Invalid input, try again: ");
    }
    while (true);
    return n;
}

int main () {
    int n = askForNumber("Enter a number");
    if (n != 0){
        int smNum = n;
        while ((n = askForNumber("Enter another number")) != 0) {
            if (n < smNum) smNum = n;
        }
        printf("The smallest number is: %d", smNum);
    }
    return 0;
}

或者,使用 <iostream>:

#include <iostream>
#include <limits>
#include <cstdlib>
using namespace std;

int askForNumber(const char *msg)
{
    int n;
    cout << msg << ": ";
    while (!(cin >> n)) {
        if (cin.eof()) exit(0);
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input, try again: ";
    }
    return n;
}

int main () {
    int n = askForNumber("Enter a number");
    if (n != 0){
        int smNum = n;
        while ((n = askForNumber("Enter another number")) != 0) {
            if (n < smNum) smNum = n;
        }
        cout << "The smallest number is: " << smNum;
    }
    return 0;
}

我认为这个问题有点混乱。我会试着去了解你想做什么,也许能解决你的一些疑惑。

首先,在 C++ 中使用 stdio.h 函数看起来有点奇怪。即使它可以使用并且肯定会起作用,我还是建议使用 C,或者使用 C++ 函数进行 IO。

我猜你想实现类似的东西:“创建一个 C/C++ 程序,要求用户输入一些整数,当用户输入 0 时,程序停止并打印最小的整数用户已输入(不包括 0)"

我将维护您程序的总体结构,这样您可能会发现它更容易阅读:

#include <stdio.h>
int main() {
    int n;    // It will hold the user's input
    int min;  // It will hold the smallest number entered so far
    printf("Enter a number: ");
    scanf("%d", &n);
    min = n;          // This is the first number, so we save in the min variable so that we can compare it to the other values later
    while (n != 0) {  // As we said, we stop when n is equal to 0
        printf("Enter a number: ");
        scanf("%d", &n);
        if (n < min && n != 0) min = n;  // If n is smaller than min, it has to become the new min.
        // But this is not true when it is equal to 0, we just have to go out of the loop in that case
    }
    printf("The smallest number is: %d", min);
}