Hey, I'm trying to create a class within its own file, and then functions, and what not. I'm having a bit of trouble, and know I'm doing this horribly wrong. Can someone point out some errors, and steer me in the right direction with this?
Math.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef MATH_H
#define MATH_H
class Math
{
public:
int Adding();
int Subtracting();
int Multiplying();
int Dividing();
};
#endif // MATH_H
#include "Math.h"
#include<iostream>
usingnamespace std;
Math::int Adding(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
Math::int Subtracting(int a, int b)
{
int difference;
difference = a - b;
return difference;
}
Math::int Multiplying(int a, int b)
{
int product;
product = a * b;
return product;
}
Math::int Dividing(int a, int b)
{
int quotient;
quotient = a / b;
return quotient;
}
#include "Math.h"
#include<iostream>
usingnamespace std;
Math::Adding(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
Math::Subtracting(int a, int b)
{
int difference;
difference = a - b;
return difference;
}
Math::Multiplying(int a, int b)
{
int product;
product = a * b;
return product;
}
Math::Dividing(int a, int b)
{
int quotient;
quotient = a / b;
return quotient;
}
#include "Math.h"
#include<iostream>
usingnamespace std;
int Math::Adding(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
int Math::Subtracting(int a, int b)
{
int difference;
difference = a - b;
return difference;
}
int Math::Multiplying(int a, int b)
{
int product;
product = a * b;
return product;
}
int Math::Dividing(int a, int b)
{
int quotient;
quotient = a / b;
return quotient;
}
Oh in your math.h your method prototypes don't have the same parameters as the actual method declarations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef MATH_H
#define MATH_H
class Math
{
public:
int Adding();
int Subtracting();
int Multiplying();
int Dividing();
};
#endif // MATH_H
should be
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef MATH_H
#define MATH_H
class Math
{
public:
int Adding(int a, int b);
int Subtracting(int a, int b);
int Multiplying(int a, int b);
int Dividing(int a, int b);
};
#endif // MATH_H
since in the header your methods didn't have any parameters the compiler will expect the declarations to have no parameters as well. So that's where your compiler is complaining now.
#include<iostream>
#include<Math.h>
usingnamespace std;
int main(){
int ProgramOption;
int ExitNumber;
int a;
int b;
while(ExitNumber!=0){
cout << "Enter a number for what you would like to calculate \n";
cout << "1)Adding \n" << "2)Subtracting \n" << "3)Multiplying \n" << "4)Dividing \n";
cin >> ProgramOption;
switch (ProgramOption){
case 1:
cout << "Enter the first number: ";
cin >> a;
cout << endl << "Enter the second number: ";
cin >> b;
Math AddingObject;
AddingObject.Adding(a,b);
cout << endl << "the sum of the two number is: ";
break;
cout << endl << "if you would like to do another calculation /nEnter 1 to exit enter 0";
cin >> ExitNumber;
}
}
return 0;
}
sweet. That solved that problem. Now the issue is the number that gets displayed for sum..I'm not sure if the the values are getting passed to my adding function, or sum is being returned. in the class or not, but the number that gets displayed for sum is wayy off..
#include<iostream>
#include "Math.h"
usingnamespace std;
int main(){
int ProgramOption;
int ExitNumber;
int a;
int b;
while(ExitNumber!=0){
cout << "Enter a number for what you would like to calculate \n";
cout << "1)Adding \n" << "2)Subtracting \n" << "3)Multiplying \n" << "4)Dividing \n";
cin >> ProgramOption;
switch (ProgramOption){
case 1:
cout << "Enter the first number: ";
cin >> a;
cout << endl << "Enter the second number: ";
cin >> b;
Math AddingObject;
AddingObject.Adding(a,b);
int sum;
cout << endl << "the sum of the two number is: " << sum << endl;
break;
cout << endl << "if you would like to do another calculation /nEnter 1 to exit enter 0";
cin >> ExitNumber;
}
}
return 0;
}
line 27 and 28 is wrong. I added comments to make it clearer what's happening in the code
1 2 3 4 5
AddingObject.Adding(a,b); //adding the values but the result isn't saved anywhere
int sum; // declare a new variable which has some random value.
// prints out the value in sum, which is actually a random value
cout << endl << "the sum of the two number is: " << sum << endl;
it should be
1 2 3 4
int sum = AddingObject.Adding(a, b); // add the values and store the result in sum.
// print out the value of sum which is the result of the operation.
cout << endl << "the sum of the two number is: " << sum << endl;