0

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;
}
5
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by firstPerson
0

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.

1

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.

2

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;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.