Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何显示数组中的最高值_C++_Arrays - Fatal编程技术网

C++ 如何显示数组中的最高值

C++ 如何显示数组中的最高值,c++,arrays,C++,Arrays,我写的代码有问题。我试图在void函数中获得数组的最大值,但在编译器中得到的只是第四个数组的值,而不管其他值如何。 所以现在如果我输入40,30,20,10,它将赋值10作为最高值。有人能解释一下我做错了什么吗 #include <iostream> #include <iomanip> #include <string> #include <string.h> using namespace std; string divName[4] =

我写的代码有问题。我试图在void函数中获得数组的最大值,但在编译器中得到的只是第四个数组的值,而不管其他值如何。 所以现在如果我输入40,30,20,10,它将赋值10作为最高值。有人能解释一下我做错了什么吗

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

using namespace std;

string divName[4] = { "Northeast", "Southeast", "Northwest", "Southwest" };

double getSales(string name)
{
    double Sales;
    while (1)
    {
        cout << fixed << setprecision(2) << "Enter the quarterly sales for the " << name << " division: ";
        cin >> Sales;
        if (Sales != 0)
            break;
    }
    return Sales;
}

void findHighest(double sales[4])
{
    double highest = 0;
    int division = 0;
    for (int i = 0; i<4; i++)
    {
        if (sales[i] > highest);
        {

            highest = sales[i];
            division = i;
        }

    }
    cout << std::endl;
    cout << fixed << setprecision(2) << "The " << divName[division] << " division had the highest sales this quarter." << std::endl << "Thier sales were $" << highest;
    cout << std::endl;
}

int main()
{
    double sales[4];

    for (int i = 0; i<4; i++)
    {
        sales[i] = getSales(divName[i]);
    }
        findHighest(&sales[0]);

    system("PAUSE");
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
字符串divName[4]={“东北”、“东南”、“西北”、“西南”};
双getSales(字符串名称)
{
双重销售;
而(1)
{
cout
double-getSales(字符串名称)
{
双重销售;
而(1)
{

cout问题在于比较语句中额外的分号:

if (sales[i] > highest);  // <<< This semicolon
{
    highest = sales[i];
    division = i;
}

if(sales[i]>highest);//在findHighest函数中不需要使用两个变量。它只能与一个变量一起使用

void findHighest(double sales[4])
{
    int maxIndex = 0;   

    for (int i = 1; i < 4; i++)
    {
        if (sales[maxIndex] < sales[i])
            maxIndex = i;
    }

    cout << std::endl;
    cout << fixed << setprecision(2) << "The " << divName[maxIndex] << " division had the highest sales this quarter." << std::endl << "Thier sales were $" << maxIndex;
    cout << std::endl;
}
void findHighest(双倍销售额[4])
{
int maxIndex=0;
对于(int i=1;i<4;i++)
{
if(销售[maxIndex]<销售[i])
maxIndex=i;
}

这不能解决问题-如果你看看他是如何使用返回值的,他会将返回值放入数组中。
Sales
是一个本地问题。这个问题似乎离题了,因为它是由输入错误引起的。请参阅此语句
if(Sales[i]>最高)是的,它现在工作得很好。我不敢相信我错过了这么小的东西。非常感谢!
void findHighest(double sales[4])
{
    int maxIndex = 0;   

    for (int i = 1; i < 4; i++)
    {
        if (sales[maxIndex] < sales[i])
            maxIndex = i;
    }

    cout << std::endl;
    cout << fixed << setprecision(2) << "The " << divName[maxIndex] << " division had the highest sales this quarter." << std::endl << "Thier sales were $" << maxIndex;
    cout << std::endl;
}