For循环和并行阵列:未声明的标识符

问题描述 投票:0回答:1

我刚开始学习有关阵列和我对他们有脆弱的把握。尝试今天做这个节目在实验室和不断收到错误numJarsSold,typesOfSalsa和totalJarsSold在MyFunctions.cpp未申报的标识符。我在网上已经发现,人们有同样的项目,并看到了他们的代码,我已经写了我自己的,仅运行在主但不知何故,运行它分开后,我已经成功地打破它。任何帮助,将不胜感激。谢谢。

Main.cpp的

#include "MyFunctions.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int main()
{
const int SIZE = 5;

string typesOfSalsa[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int numJarsSold[SIZE]; // Holds Number of Jars of Salsa sold for each type

int totalJarsSold = getJarSalesData(typesOfSalsa, numJarsSold);

displayReport(typesOfSalsa, numJarsSold, totalJarsSold);

system("pause");
return 0;
}

MyFunctions.cpp

#include "MyFunctions.h"
using namespace std;


int getJarSalesData(string typesOfSalsa[], int numJarsSold[])
{
    int totalJarsSold = 0;

    for (int type = 0; type < SIZE; type++)
    {
        cout << "Jars sold last month of " << typesOfSalsa[type] << ": ";
        cin >> numJarsSold[type];


        while (numJarsSold[type] < 0)
        {
            cout << "Jars sold must be 0 or more. Please re-enter: ";
            cin >> numJarsSold[type];
        }
        // Adds the number of jars sold to the total
        totalJarsSold += numJarsSold[type];
    }
    return totalJarsSold;
}

int posOfLargest(int array[])
{
    int indexOfLargest = 0;

    for (int pos = 1; pos < SIZE; pos++)
    {
        if (array[pos] > array[indexOfLargest])
            indexOfLargest = pos;
    }
    return indexOfLargest;
}

 int posOfSmallest(int array[])
{
    int indexOfSmallest = 0;
    for (int pos = 1; pos < SIZE; pos++)
    {
        if (array[pos] < array[indexOfSmallest])
            indexOfSmallest = pos;
    }
    return indexOfSmallest;
}

void displayReport(string[], int[], int)
{
    int hiSalesProduct = posOfLargest(numJarsSold);
    int loSalesProduct = posOfSmallest(numJarsSold);

    cout << endl << endl;
    cout << "     Salsa Sales Report \n\n";
    cout << "Name              Jars Sold \n";
    cout << "____________________________\n";
    cout << typesOfSalsa[0] << "                  " << numJarsSold[0] << "\n";
    cout << typesOfSalsa[1] << "                " << numJarsSold[1] << "\n";
    cout << typesOfSalsa[2] << "                 " << numJarsSold[2] << "\n";
    cout << typesOfSalsa[3] << "                   " << numJarsSold[3] << "\n";
    cout << typesOfSalsa[4] << "                 " << numJarsSold[4] << "\n";

    for (int type = 0; type < SIZE; type++)
    {
        cout << left << setw(25) << typesOfSalsa[type] << setw(10) << numJarsSold[type] << endl;
        cout << "\nTotal Sales: " << totalJarsSold << endl;
        cout << "High Seller: " << typesOfSalsa[hiSalesProduct] << endl;
        cout << "Low Seller: " << typesOfSalsa[loSalesProduct] << endl;
    }

}

MyFunctions.h

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int getJarSalesData(string[], int[]);
int posOfLargest(int[]);
int posOfSmallest(int[]);
void displayReport(string[], int[], int);
c++ visual-c++
1个回答
0
投票
In MyFunctions.cpp file the function := void displayReport(string[], int[], int) you didn't declared any variable name 
it should be like 
void displayReport(string typesOfSalsa[], int numJarsSold[], int totalJarsSold)
{
    // copy the code 
}
© www.soinside.com 2019 - 2024. All rights reserved.