Thread: Two-dimensional array Sales

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    30

    Two-dimensional array Sales

    Hello, Everyone:

    I'm writing a programing using a two-dimensional array to solve the following problem. A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains the following:
    a. The salesperson number
    b. The product number
    c. The total dollar value of that product sold that day.

    Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month’s sales and summarize the total sales by salesperson by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, print the results in tabular format with each of the columns representing a particular salesperson and each of the rows representing a particular product. Cross total each row to get the total sales by salesperson for the last month. Your tabular printout should include these cross totals to the right of totaled rows and to the bottom of the totaled columns.

    Here is my code:

    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    using namespace std;
    
    void instruct (void);
    
    int main ()
    {
       const int EMPLOYEE = 5, PRODUCTS = 6;
       double sales[ EMPLOYEE ][ PRODUCTS ] = { 0.0 }, value,
              totalSales, productSales[ PRODUCTS ] = { 0.0 },
              grandTotal = 0;
       int salesPerson, product;
       ifstream inDataFile ("A:/datafile.txt", ios::in );
    
       instruct ();
    {
            if ( !inDataFile ) {
                    cerr << "File could not be opened\n";
                    exit( 1 );
            }
       cout << "\t  ***************** Reading Data From File *********"
            << "*******";
       inDataFile >> salesPerson;
       while ( salesPerson != -1 ) {
       inDataFile  >> product;
       inDataFile >> value;
                  sales[ salesPerson ][ product ] += value;
       inDataFile >> salesPerson;
       }
    
         cout << "\n\t  The total sales for each sales person "
              << "are displayed\n\t  at the end of each row,"
              << "and the total sales for each\n\t  product "
              << "are displayed at the bottom of each column.\n"
                      << "-----------------------------------------------------"
                      << "--------------------------\n\n\n"
                      << "Sales |\t<---------------------- Products -----------"
                      << "---------->\n"
                      << "Person|\n"
                      << "_____________________________________________________"
                      << "__________________________\n"
              << setw( 10 ) << 1 << setw( 13 ) << 2
              << setw( 13 ) << 3 << setw( 13 ) << 4
              << setw( 13 ) << 5 << setw( 16 ) << "Total\n"
                      << "_____________________________________________________"
                      << "__________________________\n\n"
              << setiosflags( ios::fixed | ios::showpoint );
            
       for ( int i = 1; i < EMPLOYEE; ++i ) {
          totalSales = 0.0;
          cout << i;
          for ( int j = 1; j < PRODUCTS; ++j ) {
             totalSales += sales[ i ][ j ];
             cout << setw( 13 ) << setprecision( 2 )
                  << sales[ i ][ j ];   
             productSales[ j ] += sales[ i ][ j ];
          }
                      
          cout << setw( 13 ) << setprecision( 2 )  
               << totalSales << '\n';
       }
       cout << "\n_____________________________________________________"
            << "__________________________\n"
            << setw( 80 ) << " Grand Total\n"
            << setw( 80 ) << "-----------\n";
       cout << "Total " << setw( 6 ) << setprecision( 2 )
            << productSales[ 1 ];
          
       for ( int j = 2; j < PRODUCTS; ++j )
          cout << setw( 13 ) << setprecision( 2 )
               << productSales[ j ];
                    for ( int j = 0; j < PRODUCTS; ++j)
                grandTotal += productSales[j];
                     cout << setw( 13 ) << grandTotal
                              << "\n" << endl;
                      
    }
               
    return 0;
    }
            
    void instruct (void)
    {
         // Declaration section
         cout << "This program will access the sales data information for last "
              << "months sales by\neach salesperson. This information is contained in "
              << "the disk file datafile.txt.\nThe program will then take the data and "
              << "compute the total sales by salesperson\nfor last month, and the total "
              << "sales of each product during the last month and\ndisplay the results "
              << "on the screen.\n"
              << "\n_____________________________________________________"
              << "__________________________\n"
              << endl;
        // Executable section
    }
    I created a datafile.txt, so the program can read it.
    Code:
    2  3  86
    3  5  1411
    1  5  179
    3  3  1069
    2  3  381
    1  3  2234
    3  1  1358
    1  1  415
    4  4  2115
    2  2  2260
    1  4  606
    4  1  1237
    1  5  1381
    3  3  1599
    2  2  1480
    2  4  2194
    1  3  1323
    4  3  2149
    3  2  563
    2  2  302
    4  3  1006
    4  2  1622
    1  4  1688
    3  4  46
    3  1  623
    1  3  1066
    2  1  217
    4  5  1985
    1  3  2425
    3  2  1261
    1  5  2386
    2  4  1293
    1  5  959
    2  3  2391
    3  1  1091
    1  4  733
    1  3  1039
    3  2  756
    3  5  2341
    1  4  1599
    2  3  1720
    2  2  1203
    4  2  707
    4  4  2121
    4  3  2109
    1  2  369
    3  2  1444
    3  5  834
    3  3  472
    4  4  654
    3  1  1219
    2  2  2368
    3  5  795
    4  2  1202
    4  1  1402
    3  2  1179
    3  2  1
    4  1  1909
    1  2  127
    4  1  2359
    2  3  917
    1  5  1388
    1  4  461
    4  4  1617
    4  5  1330
    2  4  684
    2  1  916
    3  1  2481
    1  3  1999
    1  1  33
    3  2  682
    1  3  2468
    1  1  1812
    1  1  831
    3  4  1741
    4  5  1903
    2  4  286
    3  1  94
    4  4  1285
    4  3  1553
    4  2  643
    4  1  1762
    2  1  1850
    4  2  688
    1  2  886
    4  3  2441
    3  4  118
    1  4  2206
    1  1  1583
    2  4  997
    4  4  1162
    4  1  155
    2  5  371
    2  4  44
    1  2  2195
    2  5  1144
    4  5  306
    2  4  1191
    3  5  966
    1  2  1829
    1  2  2153
    3  2  1864
    4  4  82
    3  4  1085
    4  5  2061
    2  4  1046
    3  3  2105
    4  2  2326
    1  4  702
    2  3  2326
    2  1  865
    3  2  2336
    1  2  1542
    3  5  2276
    4  3  2496
    4  4  710
    2  4  2145
    1  1  1463
    1  1  839
    3  2  752
    1  3  2123
    2  4  2290
    1  3  1242
    4  4  2299
    1  2  173
    3  1  714
    1  4  1382
    2  4  383
    3  5  1408
    3  1  1117
    3  1  1341
    2  5  1881
    3  2  1801
    1  5  1910
    1  4  1378
    3  2  1019
    4  2  1825
    4  5  75
    3  1  377
    4  2  981
    3  3  1755
    2  4  2461
    4  2  2054
    4  1  2474
    4  3  2206
    4  2  1059
    2  4  1330
    3  4  169
    3  2  756
    2  2  843
    2  4  1357
    2  1  1074
    4  3  1251
    2  5  997
    1  1  75
    1  1  822
    3  2  90
    2  3  942
    2  2  368
    3  5  2156
    2  3  940
    3  4  1110
    1  4  1906
    1  4  636
    2  2  2059
    3  5  1511
    4  5  1260
    2  2  767
    4  1  2303
    1  3  783
    3  5  754
    1  4  2086
    4  4  381
    1  5  1434
    2  2  1963
    2  1  2336
    3  1  1532
    1  2  1421
    2  5  472
    4  1  382
    2  5  178
    1  1  641
    1  2  1234
    4  1  2422
    4  2  66
    1  4  729
    2  4  581
    3  2  1472
    2  2  811
    2  3  1189
    1  2  931
    4  1  296
    2  1  1121
    3  3  1516
    4  1  2032
    1  5  151
    3  3  249
    3  2  877
    3  5  2451
    3  4  1136
    2  4  184
    3  3  563
    4  4  970
    3  5  2142
    4  4  518
    3  2  1980
    2  5  1029
    1  4  2207
    3  5  1816
    4  5  203
    1  1  1153
    4  1  2053
    1  4  738
    1  5  1332
    1  1  953
    4  1  829
    2  4  1543
    2  1  1284
    2  3  1336
    3  4  2186
    3  1  1155
    1  3  1188
    2  3  753
    3  1  1463
    2  2  1560
    4  5  456
    2  4  2498
    4  4  1750
    4  5  900
    1  2  2212
    1  2  395
    1  3  1323
    3  1  2149
    3  1  371
    1  3  1494
    4  1  412
    2  1  1335
    4  1  407
    1  5  2376
    4  4  2394
    2  2  30
    1  3  271
    1  1  1247
    1  1  472
    1  3  1507
    4  4  367
    3  5  2366
    4  5  1592
    1  3  1365
    2  3  97
    1  1  2499
    3  5  368
    3  5  957
    3  4  579
    4  5  1010
    4  1  1502
    3  5  84
    2  5  1931
    1  1  1376
    3  4  853
    -1
    However, when I compile the program, it says that "File could not be opened"

    Code:
    g++ salesperson.cpp
    a.out
    This program will access the sales data information for last months sales by
    each salesperson. This information is contained in the disk file datafile.txt.
    The program will then take the data and compute the total sales by salesperson
    for last month, and the total sales of each product during the last month and
    display the results on the screen.
    
    _______________________________________________________________________________
    
    File could not be opened
    I understand it seems too long to read but I really need to know how I can make the program read the datafile.txt, so the program can display the results I want. Please someone help me.
    I will greatly appreciate.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ...why do I get the feeling that you didn't write this program?

    oh wait, maybe it's because you don't understand YOUR OWN DAMN ERROR MESSAGE.

    where is the file you're trying to read from?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    I just want to know my errors, its my professor's programming. The homework is to figure out a way to put the datafile.txt so the computer can read it

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    please don't offend me major_small, I'm just looking for help not looking for someone to judge me

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    what exactly "salesPerson" get from the data file. It seems like the "salesPerson" gets the data once but it doesn't make any sense when look into the text file. And i really don't understand this lines of code:
    Code:
    int main ()
    {
       const int EMPLOYEE = 5, PRODUCTS = 6;
       double sales[ EMPLOYEE ][ PRODUCTS ] = { 0.0 }, value,
              totalSales, productSales[ PRODUCTS ] = { 0.0 },
              grandTotal = 0;
       int salesPerson, product;
       ifstream inDataFile ("A:/datafile.txt", ios::in );
    
       instruct ();       
    {              <---------why this bracket starts right here.
            if ( !inDataFile ) {
                    cerr << "File could not be opened\n";
                    exit( 1 );
            }
       cout << "\t  ***************** Reading Data From File *********"
            << "*******";
       inDataFile >> salesPerson;
       while ( salesPerson != -1 ) {
       inDataFile  >> product;
       inDataFile >> value;
                  sales[ salesPerson ][ product ] += value;
       inDataFile >> salesPerson;
       }
    Hello, testing testing. Everthing is running perfectly...for now

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    hdragon, thank you for your time.

    The reason I putted the bracket is because I could separate between the two conditions, the one to say "file not found" in case the program doesn't find the datafile.txt and the inDataFile. However, my confusion is there because I am really not sure how can I state the program to read the datafile.txt the professor gave in class in order to function the program. How can I simply or state to the program to read datafile.txt

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    first off, you need to know where your data file is. The error you're getting is an error the professor put in there. He put in a check to make sure you opened the file, possibly knowing that it would fail. You have to search his code to find the error.

    to cut to the chase, change this line:
    Code:
       ifstream inDataFile ("A:/datafile.txt", ios::in );
    so that the address in the quotes points to the data file.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    Quote Originally Posted by major_small
    first off, you need to know where your data file is. The error you're getting is an error the professor put in there. He put in a check to make sure you opened the file, possibly knowing that it would fail. You have to search his code to find the error.

    to cut to the chase, change this line:
    Code:
       ifstream inDataFile ("A:/datafile.txt", ios::in );
    so that the address in the quotes points to the data file.
    Yes, I got the error. Sorry for my frustration;
    I droped A:
    He might purposely put that so students couldn't find a simple error in a big program. Thanks a million!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  2. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  3. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 05:27 AM
  4. 2 dimensional pointer array to a struct
    By AdamLAN in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2005, 05:38 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM