The title isnt too clear, and I wasnt sure what to title it, so Ill explain it here. I am making a calculator that can handle big numbers such as scientific notation and powers. A big number is one decimal (coefficient) times ten to a power (exponent). Ex. 3x10^15 or 6x10^730. This will basically prompt the user for an operation to use (* / < >) representing times,divide, and comparisons. I will then read in the first number (the coefficient) and then to the power of 10. Read in the second number (the exponent). Then print out the result.
I stored the big number in a class BigNumber, which overloads the operators << * / < >. The << is used to print the value of the class to an ostream.
I made it so if the coefficient is 0, the print out will read just 0. If the coefficient is 2 digits or more, my program will divide by 10 until it gets a single digit number. Each time it divides, it stores it in a counter which at the end will add to the exponent the number of times it divided.
This is where Im having the most trouble, once I run my program I come up with syntax errors reading
"missing ';' before '++'
and binary '=' no operator found which takes a right hand operand of type 'int'
Hopefully this makes sense. Here is my code to help you guys help me. Let me know what you think, and if im using my operators correctly.
This is my cpp file
#include <iostream>
#include <ostream>
#include <cmath>
#include <string>
#include "bignumber.h"
using namespace std;
BigNumber::BigNumber(double _coefficient, int _exponent){
coefficient = _coefficient;
exponent = _exponent;
}
double BigNumber::getCoefficient(){
return coefficient;
}
int BigNumber::getExponent(){
return exponent;
}
BigNumber BigNumber::operator *(BigNumber other){
BigNumber ret(coefficient * other.coefficient, exponent * other.exponent);
return ret;
}
BigNumber BigNumber::operator /(BigNumber other){
BigNumber ret(coefficient / other.coefficient, exponent / other.exponent);
return ret;
}
BigNumber BigNumber::operator < (BigNumber other){
BigNumber ret(coefficient < other.coefficient, exponent < other.exponent);
return ret;
}
BigNumber BigNumber::operator > (BigNumber other){
BigNumber ret(coefficient > other.coefficient, exponent > other.exponent);
return ret;
}
void BigNumber::print(){
cout << coefficient << " x 10^" << exponent;
}
void BigNumber::setCoefficient(double x){
coefficient = x;
}
void BigNumber::setExponent(int y){
exponent = y;
}
ostream & operator << (ostream &out, BigNumber n){
while (n.getCoefficient() / 10 != 0){
n.setCoefficient(n.getCoefficient() / 10);
int count++; //HERE is the syntax error of missing ';' before '++' as well as the syntax error ';'
}
n.setExponent(n.getExponent() + count);
if (n.getCoefficient() == 0){
out = 0;
return out;
}
else {
out << n.getCoefficient() << " x 10^" << n.getExponent();
return out;
}
}
Here is my header file
#ifndef BIGNUMBER_H
#define BIGNUMBER_H
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class BigNumber{
double coefficient;
int exponent;
public:
BigNumber(double _coefficient, int _exponent);
BigNumber();
double getCoefficient();
int getExponent();
BigNumber operator*(BigNumber other);
BigNumber operator/(BigNumber other);
BigNumber operator<(BigNumber other);
BigNumber operator>(BigNumber other);
void setCoefficient(double x);
void setExponent(int y);
void print();
};
#endif
Let me know what you guys think. You guys always give good feedback and I appreciate it.