Need some help with my C++ program
Go to solution
Solved by JamieF,
-snip
You need an int before the main function so int main()
The problem, from what I found, is this if statement:
if(num == tool[MAX])
num is define earlier as the number of tools which the user has to enter, but the tool array is the tool codes, so 1 for kitchen, 2 for garden and 3 for garden.
else if(code != tool[MAX]) { cout << "Invalid input." << endl; break; }
You can put this as just an else as any other input if it's not 1,2 or 3 will be invalid. You'll need a correction though to make sure they can put in the input again.
The last problem is the if statements.
if(code == 1) { cout << "Kitchen tool" << endl; kitchen++; } else if(code == 2) { cout << "Gardening tool" << endl; gardening++; } else if(code == 3) { cout << "Machining tool" << endl; machining++; }
Remember arrays start at 0 so 0 will be tool code 1, 1 will be tool code 2 and 2 will be tool code 3.
Here's the full code:
#include<iostream>#define MAX 3using namespace std;int main(){ int tool[MAX] = {1,2,3}; int num, code, kitchen = 0, gardening = 0, machining = 0, x = 0; cout << "How many tools are to be processed?: "; cin >> num; do { cout << "Input tool code: "; cin >> code; x++; if(code == tool[0]) { cout << "Kitchen tool" << endl; kitchen++; } else if(code == tool[1]) { cout << "Gardening tool" << endl; gardening++; } else if(code == tool[2]) { cout << "Machining tool" << endl; machining++; } else { cout << "Invalid input." << endl; cin >> code; } }while(x < num); cout << "The number of Kitchen tool(s) is: " << kitchen << endl; cout << "The number of Gardening tool(s) is: " << gardening << endl; cout << "The number of Machining tool(s) is: " << machining << endl; system("pause");return 0;}
Edit: I forgot to mention I changed the value of x to 0 and put while(x <num)
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now