Array problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

Array problem

 
0
  #1
Nov 17th, 2006
I am back this week with another homework problem. My array is not working like I thought it would. My brain is about to explode trying to comprehend what is going on. This code will compile but, it won't do what I want it to. The array is supposed to have columns of employee numbers and rows of product numbers. Then you can input an employee number and product number and add to that total for a given number of days. For some reason my for statement for the number of days is not working. And the employee number and product numbers are not going where I think they should be. The total code at the bottom is a little sloppy and I can fix that once I can get the array problem figured out. Any help would be greatly appreciated.

Here is the code.

  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5.  
  6. int main ()
  7. {
  8. int days = 0;
  9. int productTotal = 0;
  10.  
  11. int sales[5][5] = {{0},{0}};
  12. int employeeNumber = 1;
  13.  
  14. cout << "Enter number of days and program will request input for each day: ";
  15. cin >> days;
  16.  
  17. cout << endl;
  18.  
  19. for ( int x=0; x < days; x++)
  20. {
  21. while (employeeNumber > 0)
  22. {
  23. int productNumber = 1;
  24. cout << "Enter employee number(1-5) for day " << x << "(to move to next day enter 0): ";
  25. cin >> employeeNumber;
  26.  
  27. cout << endl;
  28.  
  29. switch (employeeNumber)
  30. {
  31. case 0:
  32. break;
  33.  
  34. case 1:
  35.  
  36. cout << "Enter product number(1-5) (enter 0 to exit): ";
  37. cin >> productNumber;
  38. if (productNumber = 0)
  39. break;
  40. else
  41. {
  42. cout << endl;
  43. cout << "Enter product total as a positive whole number: ";
  44. cin >> productTotal;
  45. cout << endl;
  46. sales[0][productNumber-1] += productTotal;
  47. cout << endl;
  48. }
  49. break;
  50.  
  51. case 2:
  52. while (productNumber > 0)
  53. {
  54. cout << "Enter product number(1-5) (enter 0 to exit): ";
  55. cin >> productNumber;
  56. if (productNumber = 0)
  57. break;
  58. else
  59. {
  60. cout << endl;
  61. cout << "Enter product total as a positive whole number: ";
  62. cin >> productTotal;
  63. cout << endl;
  64. sales[1][productNumber-1] += productTotal;
  65. cout << endl;
  66. }
  67. }
  68. break;
  69.  
  70. case 3:
  71. while (productNumber > 0)
  72. {
  73. cout << "Enter product number(1-5) (enter 0 to exit): ";
  74. cin >> productNumber;
  75. if (productNumber = 0)
  76. break;
  77. else
  78. {
  79. cout << endl;
  80. cout << "Enter product total as a positive whole number: ";
  81. cin >> productTotal;
  82. cout << endl;
  83. sales[2][productNumber-1] += productTotal;
  84. cout << endl;
  85. }
  86. }
  87. break;
  88.  
  89. case 4:
  90. while (productNumber > 0)
  91. {
  92. cout << "Enter product number(1-5) (enter 0 to exit): ";
  93. cin >> productNumber;
  94. if (productNumber = 0)
  95. break;
  96. else
  97. {
  98. cout << endl;
  99. cout << "Enter product total as a positive whole number: ";
  100. cin >> productTotal;
  101. cout << endl;
  102. sales[3][productNumber-1] += productTotal;
  103. cout << endl;
  104. }
  105. }
  106. break;
  107.  
  108. case 5:
  109. while (productNumber > 0)
  110. {
  111. cout << "Enter product number(1-5) (enter 0 to exit): ";
  112. cin >> productNumber;
  113. if (productNumber = 0)
  114. break;
  115. else
  116. {
  117. cout << endl;
  118. cout << "Enter product total as a positive whole number: ";
  119. cin >> productTotal;
  120. cout << endl;
  121. sales[4][productNumber-1] += productTotal;
  122. cout << endl;
  123. }
  124. }
  125. break;
  126. }
  127. }
  128. }
  129.  
  130. cout << "The sales totals per employee per product number are as follows: " << endl;
  131. cout << "Employee#: 1 2 3 4 5" << endl;
  132.  
  133. int i = 0;
  134. int j = 0;
  135. for ( i = 0; i < 5; i++ )
  136. {
  137. cout << "Product #: " << i+1 << " ";
  138.  
  139. for (j = 0; j < 5; j++ )
  140. {
  141. cout << sales[i][j] << " ";
  142. }
  143. cout << endl;
  144. }
  145.  
  146.  
  147. for ( i = 0; i < 5; i++ )
  148. {
  149. int total = 0;
  150. for ( j = 0; j < 5; j++ )
  151. {
  152. total += sales[i][j];
  153. }
  154. cout << "Employee #" << i+1 << " month total: " << total << " ";
  155. cout << endl;
  156. }
  157.  
  158. for ( j = 0; j < 5; i++ )
  159. {
  160. int total = 0;
  161. for (i = 0; j < 5; j++ )
  162. {
  163. total += sales[j][i];
  164. }
  165. cout << "Product #" << j+1 << " month total: " << total << " ";
  166. cout << endl;
  167. }
  168. }
Last edited by dmmckelv; Nov 17th, 2006 at 10:19 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: johnpeter1989 is an unknown quantity at this point 
Solved Threads: 1
johnpeter1989 johnpeter1989 is offline Offline
Newbie Poster

Re: Array problem

 
1
  #2
Nov 17th, 2006
In the statements "if(productnumber=0)" , i suppose you must have got a warning as i did. the test expression for the if statement is supposed to be a logical expression evaluating to true or false and not an assignment statement. i guess you meaned to write
"if(productnumber==0)" with 2 equal to signs.


sales[4][productNumber-1] += productTotal;

The above statement has been used once in each case of the switch statement with the switch variable "employeeNumber" and the only thing i noticed different in each case was in the idices of the 2d array sales. i guess this is what you must have meant

sales[employeeNumber-1][productNumber-1] += productTotal;

i didnt have time to go in to the logic of the program in too much detail but maybe sometimes correcting these if possible and reducing the number of switch statements to just a single line may help reduce the complexity and the size of the program to a large extent.

hope this solves your problem...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Array problem

 
0
  #3
Nov 17th, 2006
The question I have is with your switch statement. What is the difference between all the cases? Could you describe the differences between each?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

Re: Array problem

 
0
  #4
Nov 17th, 2006
It was just an easy way for me to allow the user to select which employee sold a product.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: johnpeter1989 is an unknown quantity at this point 
Solved Threads: 1
johnpeter1989 johnpeter1989 is offline Offline
Newbie Poster

Re: Array problem

 
0
  #5
Nov 18th, 2006
well , you could have just said "employeenumber-1" in the first index of the array, as that is the only thing i can see as the difference in the diff cases.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC