new to coding

0 simeon landers · May 23, 2015
can anyone tell ,e what i did wrong with this code the number always comes out wrong




#include <iostream>

using namespace std;

int subtract() ;
int multiply()  ;
int divide() ;
int add() ;

int main()
{
int x ;
cout << "please read below: \n For Addition enter 1 \n For subtraction enter 2 \n For Multiplication enter 3 \n For division enter 4" << endl ;
    cin >> x ;
    if(x==1){
         add() ;
    }
        if(x==2){
            subtract() ;
        }
            if(x==3){
                 multiply() ;
            }
                if(x==4){
                    divide() ;
                }
    return 0 ;
}
int add() {
    int a ;
    int b ;
    int sum ;
    sum = a + b ;
    cout << "pick your first number \n" ;
    cin >> a ;
    cout << "pick your second number \n" ;
    cin >> b ;
    cout << sum ;

     return 0;
}
int subtract(){
int a ;
int b ;
int sum ;
sum = a - b ;
cout << "pick a number please \n" ;
cin >> a ;
cout << "pick another number please \n" ;
cin >> b ;
cout << sum ;
return 0;
}
int multiply() {
int a ;
int b ;
int sum ;
sum = a * b ;
cout << "pick a number \n" ;
cin >> a ;
cout << "pick a number \n " ;
cin >> b ;
cout << sum ;
}
int divide() {
int a ;
int b ;
int sum ;
sum = a / b ;
    cout << "pick a number \n" ;
    cin >> a ;
    cout << "pick a number \n" ;
    cin >> b ;
    cout <<  sum ;
    cout << " Remainder" << a % b << endl ;
    return 0 ;


Post a Reply

Replies

Oldest  Newest  Rating
+1 Eyeshield Rosalin · May 23, 2015
// Here is my algo for this

int divide() {
int a ;
int b ;
int sum ;
// you declared the sum here already but the user has not entered anything yet
sum = a / b ;

cout << "pick a number \n" ;
cin >> a ;
cout << "pick a number \n" ;
cin >> b ;

//if you cut that line of code above and paste it here, maybe it should work
sum = a / b ;

cout << sum ;
cout << " Remainder" << a % b << endl ;
return 0 ;
0 Adrian S · May 23, 2015
You did the operation sum/substract/etc. with unknown values, not the one you got from the user. Do the addition/etc. with the input values - after getting them from user.
0 Silard Kovac · May 23, 2015
Oh yea... we can use int myFunc(void) when we dont need anything on return
void myFunc(int) when we want a return.. sorry my mistake.
+2 Silard Kovac · May 23, 2015
at the functions divide() etc. dont use int divide() ... use void divide()
int is for numbers
void is for function
  • 1

C++

107,158 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator