I am trying to get my 2d array to total up values. It is laid out perfectly, but my values do not calculate. Is my math wrong?

Please help??

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;

int main()
{
   const int DAY = 7;
   const int PRODUCTS = 5;
   double purchased[ DAY ][ PRODUCTS ] = { 0 };
   double quantity;
   double totalQ;
   double productQ[ PRODUCTS ] = { 0 };
   int dayNum;
   int product=0;
   
   cout << "Enter the Day Number (1 - 6), \n"
        << "\nProduct Number (1 - 4), \n\nQuantity Purchased,\n"
        << "\nEnter -1 for the Day Number to end input.\n";
   cin >> dayNum;
   
   while ( dayNum != -1 ) 
   {
      cin >> dayNum >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      cin >> dayNum;
   }

   cout << setw(30) << "Products\n" << setw(0)
	   << "Day #" << setw( 6 ) << 1 << setw( 10 ) << 2
	   << setw( 10 ) << 3 << setw( 10 ) << 4
	   << setw( 13 ) << "Total\n\n";
       

   for ( int i = 1; i < DAY; ++i ) 
   {
	   totalQ = 0;
	   cout << i;
      
      for ( int j = 1; j < PRODUCTS; ++j ) {
         totalQ += purchased[ i ][ j ];
         cout << setw( 10 ) << purchased[ i ][ j ];

         productQ[ j ] += purchased[ i ][ j ];
      }

      cout << setw( 10 ) << totalQ << '\n';
   }
   
   cout << "\nTotal" << setw( 6 ) << productQ[ 1 ];

   for ( int j = 2; j < PRODUCTS; ++j )
      cout << setw( 10 ) << productQ[ j ];

   cout << endl;
   return 0;
}

I am trying to get my 2d array to total up values. It is laid out perfectly, but my values do not calculate. Is my math wrong?

Please help??

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;

int main()
{
   const int DAY = 7;
   const int PRODUCTS = 5;
   double purchased[ DAY ][ PRODUCTS ] = { 0 };
   double quantity;
   double totalQ;
   double productQ[ PRODUCTS ] = { 0 };
   int dayNum;
   int product=0;
   
   cout << "Enter the Day Number (1 - 6), \n"
        << "\nProduct Number (1 - 4), \n\nQuantity Purchased,\n"
        << "\nEnter -1 for the Day Number to end input.\n";
   cin >> dayNum;
   
   while ( dayNum != -1 ) 
   {
      cin >> dayNum >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      cin >> dayNum;
   }

   cout << setw(30) << "Products\n" << setw(0)
	   << "Day #" << setw( 6 ) << 1 << setw( 10 ) << 2
	   << setw( 10 ) << 3 << setw( 10 ) << 4
	   << setw( 13 ) << "Total\n\n";
       

   for ( int i = 1; i < DAY; ++i ) 
   {
	   totalQ = 0;
	   cout << i;
      
      for ( int j = 1; j < PRODUCTS; ++j ) {
         totalQ += purchased[ i ][ j ];
         cout << setw( 10 ) << purchased[ i ][ j ];

         productQ[ j ] += purchased[ i ][ j ];
      }

      cout << setw( 10 ) << totalQ << '\n';
   }
   
   cout << "\nTotal" << setw( 6 ) << productQ[ 1 ];

   for ( int j = 2; j < PRODUCTS; ++j )
      cout << setw( 10 ) << productQ[ j ];

   cout << endl;
   return 0;
}

Not sure if this is a problem or not, but you are asking for dayNum twice every time through this loop. Do you want to?

while ( dayNum != -1 ) 
   {
      cin >> dayNum >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      cin >> dayNum;
   }

It might help to give some simple sample input, what the output should be for the sample output, and what the ACTUAL output is. Are you sure the right data values are getting into the array BEFORE you start totalling? Is it possible you want one of the variables above to be "product", not dayNum?

This is the output or similar

Product Number         Day
  Day #       1      2      3     4        Total
- - - - -|- - - - - - - - - - - - - - - | - - - -
     1   |   37     12    102    77     |   228    
     2   |   23     42    115    21     |   201
     3   |   38      2     99     0     |   139
     4   |   47     25    213    54     |   339
     5   |   53     17    125    16     |   211
     6   |   29     28      0    41     |    98
- - - - - - - - - - - - - - - - - - - - - - - -
Prod Tot |  227    126    654   209     |  1216

M~

This is the assignment I am trying to solve:

Write a program that will allow the user to continually enter from the keyboard, a product number (1 thru 4), a day number (1 thru 6) and the quantity purchased (an integer). This means that four different products are sold on each of six days. These values will be entered many times (I could input thirty triples of numbers or two hundred triples of numbers - product number, day number and quantity). I might have sold 21 of product 4 on day 3 and later sell 12 more of product 4 on day 3. It is necessary that you accumulate the total for the product each day and not replace the previous entry. As the data is entered, you should store in a two-dimensional array a count of how many products of each type are sold on the day. When the user finishes inputting values, he/she should use a sentinel selected by the programmer (indicate this sentinel in your prompt).
Once the user has finished the input create a NEAT table with labels for the output for the total number of each product sold on each day of the week. Also output within the table, the total of all products sold each day and the total number of each product that is sold for all days.

I am trying to understand your suggestion about the dayNum. My first input should be a Day number, then a product number, and then the quantity. All calculates in rows and columns.

Thanks,
M

This is the assignment I am trying to solve:

Write a program that will allow the user to continually enter from the keyboard, a product number (1 thru 4), a day number (1 thru 6) and the quantity purchased (an integer). This means that four different products are sold on each of six days. These values will be entered many times (I could input thirty triples of numbers or two hundred triples of numbers - product number, day number and quantity). I might have sold 21 of product 4 on day 3 and later sell 12 more of product 4 on day 3. It is necessary that you accumulate the total for the product each day and not replace the previous entry. As the data is entered, you should store in a two-dimensional array a count of how many products of each type are sold on the day. When the user finishes inputting values, he/she should use a sentinel selected by the programmer (indicate this sentinel in your prompt).
Once the user has finished the input create a NEAT table with labels for the output for the total number of each product sold on each day of the week. Also output within the table, the total of all products sold each day and the total number of each product that is sold for all days.

I am trying to understand your suggestion about the dayNum. My first input should be a Day number, then a product number, and then the quantity. All calculates in rows and columns.

Thanks,
M

If you are getting all zeroes in your totals comumns, I think it's because product always equals 0 regardless of what the user inputs. You initialize it to 0, then it never changes. Why? Because you never store any user input in a variable called product in this loop:

while ( dayNum != -1 ) 
   {
      cin >> dayNum >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      cin >> dayNum;
   }

You store user input in dayNum and quantity in line 3, then in dayNum again in line 5. You never store anything in product. Thus it stays as 0. So you might as well change line 4 above to this:

purchased[ dayNum ][ 0 ] += quantity;

because it will be the same result. If that is not what you want, you need to change line 3, line 5, or both, so you store user input in product. You listed the desired output. What is your ACTUAL output for the input values you listed? Is it all zeroes?

I tried and nothing! I am getting all zeros in the columns and rows.

include "Product.h"

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

int main()
{
   const int DAY = 7;
   const int PRODUCTS = 5;
   int purchased[ DAY ][ PRODUCTS ] = { 0 };
   int quantity;
   int totalQ=0;
   int productQ[ PRODUCTS ] = { 0 };
   int dayNum;
   int product=0;
   
   cout << "Enter the Day Number (1 - 6), \n"
        << "\nProduct Number (1 - 4), \n\nQuantity Purchased,\n"
        << "\nEnter -1 for the Day Number to end input.\n\n";
   cin >> dayNum;
   
   while ( dayNum != -1 ) 
   {
      cin >> dayNum >> quantity;
      purchased[ dayNum ][ 0 ] += quantity;
      cin >> dayNum;
   }

   cout << setw(30) << "Products\n" << setw(0)
	   << "\nDay #" << setw( 6 ) << 1 << setw( 10 ) << 2
	   << setw( 10 ) << 3 << setw( 10 ) << 4
	   << setw( 13 ) << "Total\n\n";
       

   for ( int i = 1; i < DAY; ++i ) 
   {
	   totalQ=0;
	   cout << i;
      
      for ( int j = 1; j < PRODUCTS; ++j ) 
	  {
         totalQ += purchased[ i ][ j ];
         cout << setw( 10 ) << purchased[ i ][ j ];

         productQ[ j ] += purchased[ i ][ j ];
      }

      cout << setw( 10 ) << totalQ << '\n';
   }
   
   cout << "\nTotal" << setw( 6 ) << productQ[ 1 ];

   for ( int j = 2; j < PRODUCTS; ++j )
      cout << setw( 10 ) << productQ[ j ];

   cout << endl;
   return 0;
}
Enter the Day Number (1 - 6),

Product Number (1 - 4),

Quantity Purchased,

Enter -1 for the Day Number to end input.

2 1 100
1 2 1

2 1 100
-1
                     Products

Day #     1         2         3         4      Total

1         0         0         0         0         0
2         0         0         0         0         0
3         0         0         0         0         0
4         0         0         0         0         0
5         0         0         0         0         0
6         0         0         0         0         0

Total     0         0         0         0
Press any key to continue . . .

M

He didn't mean you should change it to 0, he meant thats what your code equated to. He meant you should either set or prompt for a product number. The first product in the display is still zero because you started your loops at 1, therefore skipping the 0th element of the array (the one that actually had a value). This is also why there are only 6 days in the week and 4 products.

I got it to work, but the sentnel does not work properly. any suggestions??

#include "Product.h"

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

int main()
{
   const int DAY = 7;
   const int PRODUCTS = 5;
   int purchased[ DAY ][ PRODUCTS ] = { 0 };
   int quantity;
   int totalQ;
   int productQ[ PRODUCTS ] = { 0 };
   int dayNum=0;
   int product=0;
   
   cout << "Enter the Day Number (1 - 6), \n"
        << "\nProduct Number (1 - 4), \n\nQuantity Purchased,\n"
        << "\nEnter -1 for the Day Number to end input.\n\n";

   Product p;
   p.getInput;

   
   

   while(dayNum != -1) 
   {
      cin >> dayNum >> product >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      
   }

   cout << setw(30) << "Products\n" << setw(0)
	   << "\nDay #" << setw( 6 ) << 1 << setw( 10 ) << 2
	   << setw( 10 ) << 3 << setw( 10 ) << 4
	   << setw( 13 ) << "Total\n\n";
       

   for ( int i = 1; i < DAY; ++i ) 
   {
	   totalQ=0;
	   cout << i;
      
      for ( int j = 1; j < PRODUCTS; ++j ) 
	  {
         totalQ += purchased[ i ][ j ];
         cout << setw( 10 ) << purchased[ i ][ j ];

         productQ[ j ] += purchased[ i ][ j ];
      }

      cout << setw( 10 ) << totalQ << '\n';
   }
   
   cout << "\nTotal" << setw( 6 ) << productQ[ 1 ];

   for ( int j = 2; j < PRODUCTS; ++j )
      cout << setw( 10 ) << productQ[ j ];

   cout << endl;
   return 0;
}

death_oclock, no apology necessary. Your post was a good one, no need to take it down, and it wasn't wasted space. It won't fix Mophoro's problem, but it is a good thing to point out. Mophoro, death_oclock pointed out that in C++, array indexes start at 0, not 1. That won't solve the problem in your code and it doesn't cause the problems you are experiencing, but it's a good thing to know for future reference.

As to your problem, yes you are getting all zeroes, and I explained why above. This code that I posted:

while ( dayNum != -1 ) 
   {
      cin >> dayNum >> quantity;
      purchased[ dayNum ][ 0 ] += quantity;
      cin >> dayNum;
   }

was NOT a recommendation of the right way to implement your program. I was pointing out that the way you have it has the EFFECT of what is above, which will give bad results. Your problem, again, is this. You are reading data into the variable dayNum twice above, and not reading into product at all. In this code(your original code):

while ( dayNum != -1 ) 
   {
      cin >> dayNum >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      cin >> dayNum;
   }

change line 3 to:

cin >> product >> quantity;

and see if you get better results.

I got it to work, but the sentnel does not work properly. any suggestions??

while(dayNum != -1) 
   {
      cin >> dayNum >> product >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      
   }

Yes, change it back to your original construct, but with the change I made in my last post, as below:

cin >> dayNum;
   
   while ( dayNum != -1 ) 
   {
      cin >> product >> quantity;
      purchased[ dayNum ][ product ] += quantity;
      cin >> dayNum;
   }

See if that works.

It did the trick! Thank you! I love it!! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.