如何从用户输入

时间:2015-07-11 20:12:40

标签: c++ input add

我要让用户输入他想输入多少个浮动数字。一旦键入了浮动数字,我就会收到一条消息,上面写着“你输入的最大数字是这样的”。< / p>

如何识别用户输入的最大数字。

#include <stdio.h>
#include <iostream>
#include <iomanip>

using namespace std; 

int main()
{
    float count; 
    float input; 
    float large; 

    cout << "Enter the number of floating numbers you wish to input: ";
    scanf("%f", &count); 

    do
    {
        cin >> input; 
        count--; 

    }
    while(0 < count); 

    return 0;

}

2 个答案:

答案 0 :(得分:2)

此方法快速而干净,基本上读取指定次数的值,每次数字大于当前最大值时,将max替换为读取的值。

    int main()
    {
        int num_entries;
        float num;
        float max = 0;
        cin >> num_entries;
        while (num_entries-- > 0){
            cin >> num;
            if (num > max) {
                max = num;
            }
        }
    }

答案 1 :(得分:1)

以下是“for”循环

的示例
int main()
{
    int conut = 0;
    float number = 0;
    float max_number = 0;

    for (int i = 0; i != count; ++i)
    {
         cin >> number; // the user input
         if (number > max_number) max_number = number; // if input is  highter than actuall the highest number then max_number = number
    }

    std::cout << "max_number = " << max_number;
}