I am trying to get my program to run when I input two integer numbers in the subroutine, and then do the following math (x+y)/5. Dont know where I am going wrong.

#include<iostream>
using namespace std; 
int Divide(int X,int Y);
int main()
{
    int X;
    int Y;

    cout<<"first number:";
    cin>>X;
    cout<<" second number:";  
    cin>>Y;
    cin.ignore();
    cout<<"The difference of your two numbers is "<< Divide(X,Y)<<"\n ";
    cin.get();
}
int  Divide(int X, int Y)
{
    return (X+Y)/5;
}

Recommended Answers

All 4 Replies

So, you post some code. You don't indicate what your problem is (although there are a number of problems with your code)...

On writing to cout, terminate your lines with << endl;, otherwise you won't get your output displayed on the screen before you are asking for the input.

On writing to cout, terminate your lines with << endl;, otherwise you won't get your output displayed on the screen before you are asking for the input.

That's wrong. cin and cout are tied together under-the-hood such that asking for input from cin automatically triggers a flushing of the output stream, exactly for that kind of situation.

Also the return type of a division operation should be double or float.

In C++ 1/2 = 0 because of inteeger division. Thus you want something like so:

float divide(float x, float y){ return x/y;}
int main(){
 float x = 0, y = 1;
 cin >> x >> y;
 cout << divide(x,y) << endl;
}
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.