Jump to content

Need some help with my C++ program

infernowheels
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)

I seriously don't know what I'm missing here. The program only runs perfectly if I input 2 as the value of num.

 

Basically, this program should:

>Use a do-while loop.

>Use an array for the tools.

>Ask how many tools have to be inputted by the user.

>Categorize each tool where:

Tool code - Description

1                - Kitchen tool

2                - Gardening tool

3                - Machining tool

>Count how many of each tool have been inputted.

>Terminate if the inputted tool code is not in the tool array.

 

#include<iostream>
#define MAX 3

using namespace std;

main()
{
      int tool[MAX] = {1,2,3};
      int num, code, kitchen = 0, gardening = 0, machining = 0, x = 1;
      
      cout << "How many tools are to be processed?: ";
      cin >> num;
      
      do
      {
      cout << "Input tool code: ";
      cin >> code;
      x++;
          if(num == tool[MAX])
          {
              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++;
              }
          }
          else if(code != tool[MAX])
          {
              cout << "Invalid input." << endl;
              break;
          }                   
      }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;
}

 

Any help will be greatly appreciated! ^^

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

-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)

Link to comment
Share on other sites

Link to post
Share on other sites

You need an int before the main function so int main()

 

The problem, from what I found, is this if statement:

-snip

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.

 -snip

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.

-snip

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:

-snip

Edit: I forgot to mention I changed the value of x to 0 and put while(x <num)

Great, now I understand~! Thanks a bunch~!

 

BTW, my compiler/editor really doesn't find an "int" before "main()" necessary. (Dev C++ 4.9.9.2)

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×