我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦

I've read a lot about the 2d array but I'm having troubles using it on my assignment

本文关键字:作业 麻烦 遇到 数组 2d 信息      更新时间:2023-10-16

这是作业的链接

我设法让除void finalSales()功能之外的大部分项目正常工作。我不太确定如何使用一维和二维数组并相应地显示它。我下周要参加期中考试,包括这一章。我在下面添加了一些关于函数功能的评论:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
bool isPlaying = true, isPlaying_Two = true, isPlaying_Three = true, isLooping = true;
string salesPerson[6] = { "Jericho Rosales", "lisa Soberano", "Kim Chiu", "maja Salvador", "Katheryn Bern", "Daniel Padilla" },
items[3] = { "Washing Machine", "Refrigerator", "Music System" };
double table[6][3], totalSales[6] = {0};
int salesNum, productNum;
double saleAmount;

void Sales();
void person();
void prod();
void finalSales();

int main()
{
while (isLooping) {
Sales();
}

}
//main game 
void Sales() {
while (isPlaying)
{
person();
if (salesNum > 0 && salesNum < 7) {
//cout << "debug " << salesPerson[salesNum - 1];//debug
cout << "n";
while (isPlaying_Two)
{
prod();

if (productNum > 0 && productNum < 4) {
//cout << "debug " << items[productNum - 1];//debug
while (isPlaying_Three)
{
finalSales();
}
}
else
{
cout << "n";
cout << "Input out of range, please try againn";
cout << "n";
}
}
}
else {
cout << "n";
cout << "Input out of range, please try againn";
cout << "n";
}
}
}
//user selects which salesperson
void person() {
cout << "Sales Registryn";
for (int i = 0; i < 6; i++)
{
cout << i + 1 << ". " << salesPerson[i] << "n";
}
cout << "Select the Salesperson by typing his ordinal number: ";
cin >> salesNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid entry, enter again ";
cin >> salesNum;
}
}

//user selects which product
void prod() {
for (int i = 0; i < 3; i++)
{
cout << i + 1 << ". " << items[i] << "n";
}
cout << "Select the product by typing the product ordinal number: ";
cin >> productNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid entry, enter again ";
cin >> productNum;
}
}
//get the sales amount(enter a random number in) and 
//if the user wants to enter more sales it goes back to the beginning and asks again while remembering the previous values
//if not then the program adds up the amount (if there is more than one) and displays it accordingly to the salesperson and the product
void finalSales() {
string again;
cout << "Enter the sales amount: ";
cin >> saleAmount;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
cout << "Do you want to enter more sales? Type y for yes and n for no: ";
cin >> again;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
if (again == "y" || again == "Y") {
Sales();
}
else {
cout << "n";
cout << "Salesperson" << "tt" << "Washing Machine" << "tt" << "Refrigerator" << "tt" << "Music systemn";
cout << "****************************************************************************************************";
cout << "n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];
for (int y = 0; y < 3 ; y++)
{

cout << "ttt" << table[x][y]; 
}
cout << "n";
}
}
}

您需要从person()返回索引,然后prod()将它们传递给finalSales。还将数组传递给它并按saleAmount增加产品量。不要忘记让你的布尔变量false否则你最终会得到无限循环。在Sales()中创建两个 int 变量。通过从person()返回索引和从product()返回另一个索引来初始化它们。

void finalSales(int idx1, int idx2,double table[6][3]) {
string again;

cout << "Enter the sales amount: ";
cin >> saleAmount;
table[idx1][ idx2] = saleAmount;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
cout << "Do you want to enter more sales? Type y for yes and n for no: ";
cin >> again;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
if (again == "y" || again == "Y") {
Sales();
}
else {
::isPlaying_Three = false;
::isPlaying_Two = false;
::isPlaying = false;
::isLooping = false;
cout << "n";
cout << "Salesperson" << "tt" << "Washing Machine" << "tt" << "Refrigerator" << "tt" << "Music systemn";
cout << "****************************************************************************************************";
cout << "n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];
for (int y = 0; y < 3; y++)
{
cout << "ttt" << table[x][y];
}
cout << "n";
}
}
}

这对person()来说是一样的,对prod()也是如此.

int person() {
cout << "Sales Registryn";
for (int i = 0; i < 6; i++)
{
cout << i + 1 << ". " << salesPerson[i] << "n";
}
cout << "Select the Salesperson by typing his ordinal number: ";
cin >> salesNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid entry, enter again ";
cin >> salesNum;
}
return salesNum-1;
}

不要忘记将索引减少 1。