我才刚刚开始学习数组,并且对它们掌握得很细。 今天尝试在实验室中制作此程序,并在 MyFunctions.cpp 中不断收到 numJarsSold,typesOfSalsa和totalJarsSold是未声明的标识符的错误。我已经在网上发现人们拥有相同的项目并看到了他们的代码,并且我编写了自己的代码以仅在main上运行,但是以某种方式将其分开却设法破坏了它。任何帮助,将不胜感激。谢谢。
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);
答案 0 :(得分:0)
T