All of us started from being a rookie, and I'm one of those rookies who are new in this course. Maybe its time for you
to share your expertise unto the newbies? Hope anyone might help me willingly in my program.Here's the problem:

Use Pointers and Functions:

Ask a user wether (+), (-), (*), or (/) is to be executed, After choosing the operation, ask the user how many
operands will it use. Display the correct corresponding output.

Example: Chose an Operation: *
Number of Operands: 3
Enter 1st Operand: 2
Enter 2nd Operand: 1
Enter 3rd Operand: 3
The Product is 6.

My code (Haven't got it yet):

  #include <iostream>
    #include <conio.h>
    using namespace std;

    void addition (int *b)

         int operands, c;
         cout << "Number of Operands: ";
         cin >> operands;

         for ( int i = 0; i < operands; i++)
         {
             cout << "Enter " << i+1 << " Operand: ";
             cin >> *b;
             *b = *b + *b;
             }
             }


    void subtraction (int *b)
    {
         int c, d, e;
         cout << "Enter 1st Operand: ";
         cin >> c;
         cout << "Enter 2nd Operand: ";
         cin >> d;
         cout << "Enter 3rd Operand: ";
         cin >> e;
         *b = c - d - e;
         }

    void multiplication (int *b)
    {
         int c, d, e;
         cout << "Enter 1st Operand: ";
         cin >> c;
         cout << "Enter 2nd Operand: ";
         cin >> d;
         cout << "Enter 3rd Operand: ";
         cin >> e;
         *b = c * d * e;
         }

    void division (int *b)
    {
         int c, d, e;
         cout << "Enter 1st Operand: ";
         cin >> c;
         cout << "Enter 2nd Operand: ";
         cin >> d;
         cout << "Enter 3rd Operand: ";
         cin >> e;
         *b = c / d / e;
         }

    int main ()
    {
        char input = 0;
        int a;

        cout << "Choose an Operation: ";
        cin >> input;


        if (input == '+')
        {
                  addition (&a);
                  cout << a;
                  }

        else if (input == '-')
        {
                  subtraction (&a);
                  cout << a;
                  }

        else if (input == '*')
        {
                  multiplication (&a);
                  cout << a;
                  }

        else if (input == '/')
        {
                  division (&a);
                  cout << a;
                  }

        else
        {
            cout << "Not in the choices. ";
            }

        getch ();
        return 0;{


}

Hoping that anyone with a kind heart would willingly help me. Good day and thanks in advance. :)

Recommended Answers

All 4 Replies

  1. Initialize the variable a to 0.
  2. Since this is C++, don't pass pointers, but better to use references. IE: void addition (int& b) called as addition (a); - that eliminates the need to check for void pointers. Your task is to fix up the code for the functions.
  3. Remember the KISS (keep it short and simple) principal.

Post corrected code here again for more help, nagging, whatever.

write a programe that compute addition substraction multiplication as asimple callculator.

mulusew: Please do not hijack other people's threads, especially if all you are going to do is beg for someone to do you homework for you. Please start a new hread of your own, and demonstrate that you have put some effort into it yourself before you expect us to be able to help you.

Since you mentioned pointers were to be used as part of your solution ...

I'm wondering if the prof meant ... function pointers?

If so ... then this might get you started:

int add( int a, int b )
{
    return a+b;
}
int sub( int a, int b )
{
    return a-b;
}
int mul( int a, int b )
{
    return a*b;
}
int dvd( int a, int b )
{
    if( b ) return a/b;
    // else ...
    cout << "DIVIDE by ZERO ERROR ... INVALID "
         << INT_MIN << " returned ...\n";
    return INT_MIN; // divide by zero returned error value //
}

// f is a function pointer ... //
int binOP( int a, int b, int (*f) (int, int) )
{
    return f( a, b );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.