I was solving this problem and I'm struck in some problem..could you guys help me out?thanks
Problem: Write the definition of a function that takes as input three numbers and
returns the sum of the first two numbers multiplied by the third number.
solution:

#include <iostream>

   #include <conio.h>
    #include <math.h>
    using namespace std;
     int addTwoMultiplyOne(int, int, int);
     int main()
     {
        cout<<addTwoMultiplyOne()<<endl;

        getch();
        return 0;
     }

    int addTwoMultiplyOne(int x, int y, int z)
    {
        cout<<"Enter x,y,z"<<endl;
        cin>>x>>y>>z;
     return ((x+y)*z);

    }

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

As I said previously, you need to look at your notes. For one you're not passing any values to your function cout<<addTwoMultiplyOne()<<endl;

According to the problem statement and according to the function declaration, what you should do is take the three numbers as input (cin) before calling the function, and then pass those three numbers to the function to get the result. As so:

#include <iostream>
#include <cmath>      // <-- notice, the standard math header is "cmath" not "math.h".

using namespace std;

int addTwoMultiplyOne(int, int, int);

int main()
{

    cout << "Enter x y z: " << endl;   //
    int x,y,z;                         // take input first.
    cin >> x >> y >> z;                //

    cout << addTwoMultiplyOne(x, y, z) << endl;  // then, call the function.

    char c;       //
    cin.ignore(); // use this instead of the non-standard "getch()" (remove "conio.h") 
    cin >> c;     //
    return 0;
}

int addTwoMultiplyOne(int x, int y, int z)
{
    return ((x+y)*z);
}

Also, I hope you notice how much nicer it looks when the code is properly indented and spaced-out.

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.