| | |
Array problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 32
Reputation:
Solved Threads: 0
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.
Here is the code.
c Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; using std::endl; int main () { int days = 0; int productTotal = 0; int sales[5][5] = {{0},{0}}; int employeeNumber = 1; cout << "Enter number of days and program will request input for each day: "; cin >> days; cout << endl; for ( int x=0; x < days; x++) { while (employeeNumber > 0) { int productNumber = 1; cout << "Enter employee number(1-5) for day " << x << "(to move to next day enter 0): "; cin >> employeeNumber; cout << endl; switch (employeeNumber) { case 0: break; case 1: cout << "Enter product number(1-5) (enter 0 to exit): "; cin >> productNumber; if (productNumber = 0) break; else { cout << endl; cout << "Enter product total as a positive whole number: "; cin >> productTotal; cout << endl; sales[0][productNumber-1] += productTotal; cout << endl; } break; case 2: while (productNumber > 0) { cout << "Enter product number(1-5) (enter 0 to exit): "; cin >> productNumber; if (productNumber = 0) break; else { cout << endl; cout << "Enter product total as a positive whole number: "; cin >> productTotal; cout << endl; sales[1][productNumber-1] += productTotal; cout << endl; } } break; case 3: while (productNumber > 0) { cout << "Enter product number(1-5) (enter 0 to exit): "; cin >> productNumber; if (productNumber = 0) break; else { cout << endl; cout << "Enter product total as a positive whole number: "; cin >> productTotal; cout << endl; sales[2][productNumber-1] += productTotal; cout << endl; } } break; case 4: while (productNumber > 0) { cout << "Enter product number(1-5) (enter 0 to exit): "; cin >> productNumber; if (productNumber = 0) break; else { cout << endl; cout << "Enter product total as a positive whole number: "; cin >> productTotal; cout << endl; sales[3][productNumber-1] += productTotal; cout << endl; } } break; case 5: while (productNumber > 0) { cout << "Enter product number(1-5) (enter 0 to exit): "; cin >> productNumber; if (productNumber = 0) break; else { cout << endl; cout << "Enter product total as a positive whole number: "; cin >> productTotal; cout << endl; sales[4][productNumber-1] += productTotal; cout << endl; } } break; } } } cout << "The sales totals per employee per product number are as follows: " << endl; cout << "Employee#: 1 2 3 4 5" << endl; int i = 0; int j = 0; for ( i = 0; i < 5; i++ ) { cout << "Product #: " << i+1 << " "; for (j = 0; j < 5; j++ ) { cout << sales[i][j] << " "; } cout << endl; } for ( i = 0; i < 5; i++ ) { int total = 0; for ( j = 0; j < 5; j++ ) { total += sales[i][j]; } cout << "Employee #" << i+1 << " month total: " << total << " "; cout << endl; } for ( j = 0; j < 5; i++ ) { int total = 0; for (i = 0; j < 5; j++ ) { total += sales[j][i]; } cout << "Product #" << j+1 << " month total: " << total << " "; cout << endl; } }
Last edited by dmmckelv; Nov 17th, 2006 at 10:19 am.
•
•
Join Date: Nov 2006
Posts: 7
Reputation:
Solved Threads: 1
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...
"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...
![]() |
Similar Threads
- Array problem (Java)
- Array problem (C#)
- Is there a simplest way to work this array problem? (C++)
- class array problem! (C++)
- C++ help with student array problem (C++)
- i have problem with array plz help (Java)
- Large Array Problem (PHP)
Other Threads in the C++ Forum
- Previous Thread: Operator Overloading
- Next Thread: System function, output to file
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






