Use a two-dimensional array to track data in structure - C++ Data Type

C++ examples for Data Type:struct

Description

Use a two-dimensional array to track data in structure

Demo Code

#include <iostream>

struct ticket {//from  w w  w .  ja  v a  2 s  . com
    int salesPerson;
    int productNumber;
    int value;
};

const int salesPeople = 3;
const int totalProducts = 5;

void processTicket(int[][totalProducts], ticket);

int main(int argc, const char *argv[]) {
    int arrSales[salesPeople][totalProducts] = {{}};

    for (int sp = 0; sp < salesPeople; ++sp) {
        ticket salesTicket;

        salesTicket.salesPerson = sp;

        std::cout << "Enter Product Number 1-5: ";
        std::cin >> salesTicket.productNumber;
        --salesTicket.productNumber;

        std::cout << "Enter total dollar value of product: ";
        std::cin >> salesTicket.value;

        processTicket(arrSales, salesTicket);
    }

    for (int sp = 0; sp < salesPeople; ++sp) {
        std::cout << "\nSales Person: " << sp + 1 << std::endl;
        for (int pr = 0; pr < totalProducts; ++pr) {
            std::cout << "Product: " << pr + 1 << ": " << arrSales[sp][pr]<< std::endl;
        }
    }
    return 0;
}
void processTicket(int arrSales[][totalProducts], ticket tck) {
    arrSales[tck.salesPerson][tck.productNumber] += tck.value;
}

Result


Related Tutorials