user10305126
user10305126

Reputation: 45

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

Here is the link to the assignment

I managed to get the majority of the project working except the void finalSales() function. I'm not too sure how to go about it with a one and two-dimensional array and display it accordingly. I have a midterm exam coming up next week and this chapter is included. I added some comments on what the functions do below:

#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";
            }
        }
    }

Upvotes: 1

Views: 65

Answers (1)

NixoN
NixoN

Reputation: 683

You need to return index from person() and prod() then pass them to finalSales. Also pass array to it and increase the amount of product by the saleAmount. And don't forget to make your bool variables false or you end up with infinite loop. Create two int variables in Sales(). Initialize them by returning the index from person() and the other one from 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";
        }
    }
}

That's for person() and the same for 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;
}

Don't forget to decrease index by 1.

Upvotes: 2

Related Questions