943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1453
  • C++ RSS
Nov 17th, 2006
0

Array problem

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dmmckelv is offline Offline
33 posts
since Nov 2006
Nov 17th, 2006
1

Re: Array problem

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...
Reputation Points: 11
Solved Threads: 1
Newbie Poster
johnpeter1989 is offline Offline
7 posts
since Nov 2006
Nov 17th, 2006
0

Re: Array problem

The question I have is with your switch statement. What is the difference between all the cases? Could you describe the differences between each?
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,744 posts
since May 2006
Nov 17th, 2006
0

Re: Array problem

It was just an easy way for me to allow the user to select which employee sold a product.
Reputation Points: 10
Solved Threads: 0
Light Poster
dmmckelv is offline Offline
33 posts
since Nov 2006
Nov 18th, 2006
0

Re: Array problem

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.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
johnpeter1989 is offline Offline
7 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Operator Overloading
Next Thread in C++ Forum Timeline: System function, output to file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC