gpt4 book ai didi

c++ - 我已经阅读了很多有关2d数组的信息,但是在分配作业时遇到了麻烦

转载 作者:行者123 更新时间:2023-12-02 10:00:44 26 4
gpt4 key购买 nike

Here is the link to the assignment
除了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 again\n";
cout << "\n";
}
}
}
else {
cout << "\n";
cout << "Input out of range, please try again\n";
cout << "\n";
}
}
}

//user selects which salesperson

void person() {
cout << "Sales Registry\n";

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" << "\t\t" << "Washing Machine" << "\t\t" << "Refrigerator" << "\t\t" << "Music system\n";
cout << "****************************************************************************************************";
cout << "\n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];

for (int y = 0; y < 3 ; y++)
{

cout << "\t\t\t" << table[x][y];
}
cout << "\n";
}
}
}

最佳答案

您需要从person()prod()返回索引,然后将它们传递给finalSales。还要将数组传递给它,并通过saleAmount增加乘积的数量。并且不要忘了将 bool(boolean) 变量设为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" << "\t\t" << "Washing Machine" << "\t\t" << "Refrigerator" << "\t\t" << "Music system\n";
cout << "****************************************************************************************************";
cout << "\n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];

for (int y = 0; y < 3; y++)
{

cout << "\t\t\t" << table[x][y];
}
cout << "\n";
}
}
}
那是 person()的,也是 prod()的。
int person() {
cout << "Sales Registry\n";

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。

关于c++ - 我已经阅读了很多有关2d数组的信息,但是在分配作业时遇到了麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62682733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com