## ELBUF

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;
}
}``````

``````#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.

## jonsca 1,059

You should declare count (and initialize it!) before the while loop then increment it within. What you are doing there doesn't make any sense syntactically. I also question doing this much processing in the overloaded << method. Normally the second parameter is of const Bignumber & type, so perhaps delegate some of the processing to your print method and call that within operator<< .

## ELBUF

What you are doing there doesn't make any sense syntactically. I also question doing this much processing in the overloaded << method. Normally the second parameter is of const Bignumber & type, so perhaps delegate some of the processing to your print method and call that within operator<<

If I do that, Id put it in which operator? Or would I have to create another? Is that what you mean?

## ELBUF

I intialized count outside the loop and have started working on my main.cpp as seen below. These are some ideas im having. I need to prompt the user to use one of the operations I overloaded, then read in 2 numbers (coefficient and exponent) for the first number and then read in 2 more for the 2nd number (again coefficient and exponent). How would I go about doing that? This is what I have so far, but I am not 100% yet. Im pretty sure I will end up using my print() that I created Im just not sure exactly where. Anyways while I figure that out, hows this going?
Main.cpp

``````#include <iostream>
#include <cmath>
#include <string>
#include "bignumber.h"
#include "bignumber.cpp"
using namespace std;

int main(){

//cout << "What operation would you like to perform? " << variable for operator? << endl;
//cin >> variable ;
//I put variable there because I am not sure how to have the program know what operation the user put in
// and then how to have it run what I coded.

cout << "What is your first number (coefficient followed by exponent) ? " << endl;
cin >> double a, int b; // Here I get "unexpected double" because if I just leave it a,b I get an uninitialized error.
//For these two, I am trying to ask the user two numbers for the first coefficient and first exponent
//and ask again for the second coefficient and the second exponent.
//Could I use the cin like this? Or am I missing something from these?
cout << "What is your second number (coefficient followed by exponent) ? " << c,d << endl;
cin >> c,d;

//These are examples and ideas I was putting down as I brainstorm.
//They could be useful later, not sure exactly when/where yet.

//This is piece of code:
//BigNumber a(32,11); // a big number 3 x 10^12
//BigNumber b(4,800); // a big number 4 x 10^800
//cout << a << " " << b << endl;

//if (a<b) {
//    cout << "a is less than b" << endl;
//}
//if (a>b) {
//    cout << "a is greater than b" << endl;
//}
//cout << a * b << endl;
//cout << b / a << endl;
//Would print this out:
//3x10^12 4x10^800
//a is less than b
//1x10^813
//1x10^788

}``````

What do you guys think so far? I would appreciate any help or comments, also if some of this isnt clear just ask and I will try to make it clearer.

## jonsca 1,059

Maybe move lines 59-63 to the constructor (still changing the int count statement). Then you can change the signature of `ostream & operator<<(ostream & out,const BigNumber & n)` . Then you'll just have 65-71 left in the operator method.

## ELBUF

when you say move those lines to the constructor, your saying move it into there? Or the constructor in the header file? Or the print statement as you mentioned above? I dont think Im understanding correctly.

``````BigNumber::BigNumber(double _coefficient, int _exponent){
coefficient = _coefficient;
exponent = _exponent;
}``````

And if i leave those lines in there, you said it would be too much for the operator?

## jonsca 1,059

Scratch the idea about the print statement you'd have to change it to return a string for it to work. Move it right under line 3 in that code that you have posted.

It's some processing that I think would best be done elsewhere. The << operator customarily does not modify the object it's outputting.

Putting that code from 59-63 in the constructor seemed like a logical choice though it could have been a completely separate private method. If it's in the constructor, it's over and done with and in simplified form.

## Lerner 582

``````ostream & operator << (ostream &out, BigNumber n)
{
double temp = n.getCoefficient();
if (temp == 0)
{
out << 0;
}
else
{
out << temp << " x 10^" << n.getExponent();
}
return out;
}``````

## Lerner 582

>>I put variable there because I am not sure how to have the program know what operation the user put in and then how to have it run what I coded.

A menu combined with a switch statement is one common approach to this.

cout << "What is your second number (coefficient followed by exponent) ? " << endl;
cin >> c >> d;

## jonsca 1,059

``````ostream & operator << (ostream &out, BigNumber n)
{
double temp = n.getCoefficient();
if (temp == 0)
{
out << 0;
}
else
{
out << temp << " x 10^" << n.getExponent();
}
return out;
}``````

That wasn't really at issue. OP was trying to "reduce" the number to scientific notation so if I wrote 100 x 10^2 it would display as 1x10^4.

## ELBUF

Well I asked another question after that, while attempting to figure out the part you helped with jonsca. I believe that's the part hes referring to, although im not quite sure what exactly he means other than what i can decipher from his code.

## ELBUF

Nevermind my last post, I was zoned out. But I did ask another question regarding my main.

## Lerner 582

1) At this time does the << operator reduce input to scientific notation before displaying the result? As my first post indicated there were other errors besides the initialization of the variable count to zero before the loop.

2) This syntax is likely to cause trouble:
cin >> double a, int b;

If it does, try something like this:
//declare variables first;
double a;
int b;

Then enter data into them;
cin >> a >> b;

Note use of the >> operator between the variables as opposed to the comma operator.

The same goes for this line (see my previous post):
cin >> c,d;

3) To allow user to select which operator they want to use display a menu of choices. Here's an example of a menu:

int choice;
cout << "Enter the number of the operation you wish to use" << endl;
cout << "1) Addition" << endl;
cout << "2) Subtraction" << endl;
cout << "3) Exit calculator" << endl;
cin >> choice;

When coupled with a switch statement like this;

``````switch(choice)
{
case 1:
break;
case 2:
//do subtraction
//etc
}``````

you get the functionality you want (see my previous post). Be aware there are some subtleties involved in writing a switch statement so look up how to do it in your favorite reference.

## ELBUF

Ok so Im moving that one piece into the constructor (thank you jonsca), and now I have several other questions. My comparison operators (< >) need to be bools to determine whether or not one number is bigger or smaller than the other. So I need them to be bools, however when I changed the syntax it gave me errors. Ill fiddle with it a bit to see what I come up with, and figure out the syntax. My other question is, in my main, I ask the user to choose an operator, so I thought I read in a char and then figure out which symbol it is? Does that make sense? How would I go about doing that? And lastly, the numbers I read in when I ask the user, I initalized them, read them in, but now I have to use them with my operator.
Here is my code so far:
The main file:

``````int main(){
double coef1;
int expo1;
double coef2;
int expo2;

//cout << "What operation would you like to perform? " << char here << endl;
//cin >> read in a char ;
cout << "What is your first number (coefficient followed by exponent) ? " << coef1 << expo1 <<endl;
cin >> coef1, expo1;
cout << "What is your second number (coefficient followed by exponent) ? " << coef2 << expo2 << endl;
cin >> coef2, expo2;

//I stored the numbers, but now what? Could I do something like,
//BigNumber a(coef1, expo1);
//BigNumber b(coef2, expo2);
//Then use an operation?
//Does that make sense?``````

cpp file

``````//I need to make these operators into bool. With this syntax I get:
//binary operator < has too few parameters
//an error saying coefficient and exponent are undeclared identifiers
//cannot access private member class BigNumber
bool operator < (BigNumber other){//These have to be bools.
BigNumber compare1(coefficient < other.coefficient, exponent < other.exponent);
return false;
}
//Same errors here.
bool operator > (BigNumber other){//These have to be bools.
BigNumber compare2(coefficient > other.coefficient, exponent > other.exponent);
return true;
}``````

``````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);
bool BigNumber operator<(BigNumber other);//This needs to be bool
//I get error: types preceding BigNumber (constructor with return type, or illegal redefinition of current class-name?)
bool BigNumber operator>(BigNumber other);//This needs to be bool
void setCoefficient(double x);
void setExponent(int y);
};
#endif``````

Again any feedback would be appreciated, Ill keep looking stuff up and figuring out what I can. Ill update if anything changes.

## ELBUF

I didnt get a chance to read your post Lerner but thats what I ended up doing in my main. I just realized I pasted my main with the cin >> a,b instead of using the >>. I changed it here though. I really like the idea of a menu of choices. Ill look up more on the switch statement.

## ELBUF

I have everything pretty much done. Thanks for all your help guys. I just have one final question and its been driving me crazy. In my main I am trying to print out the following, and I get error C2679: binary << no operator found which takes a right hand operand of type 'BigNumber'.
Any thoughts?

main cpp

``````#include <iostream>
#include <cmath>
#include <string>
#include "bignumber.h"
#include <ostream>
using namespace std;

int main(){
double coef1;
int expo1;
double coef2;
int expo2;
char h;

cout << "What operation would you like to perform? " << h << endl;
cin >> h;
cout << "What is your first number (coefficient followed by exponent) ? " << coef1 << expo1 <<endl;
cin >> coef1 >> expo1;
cout << "What is your second number (coefficient followed by exponent) ? " << coef2 << expo2 << endl;
cin >> coef2 >> expo2;

BigNumber a(coef1, expo1);
BigNumber b(coef2, expo2);

if (h =='*'){
cout << a * b << endl;
}
if (h =='/'){
cout << a / b << endl;
}
if (h =='<'){
cout << a << " is less than " << b << endl;
}
if (h =='>'){
cout << a << " is greater than " << b << endl;
}``````

``````#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);
bool operator <(BigNumber other);
bool operator >(BigNumber other);
void setCoefficient(double x);
void setExponent(int y);
};
#endif``````

## jonsca 1,059

I don't see a declaration for your << operator in the header and you don't show your implementation file so I don't know if it's there either. In you declaration it should be declared as a friend.

## ELBUF

Nice thank you, However now I get a run time error saying h is being used without intialization. But I did at the top of the main. How do I fix that?

``````#include <iostream>
#include <ostream>
#include <cmath>
#include <string>
#include "bignumber.h"
using namespace std;

BigNumber::BigNumber(double _coefficient, int _exponent){
coefficient = _coefficient;
exponent = _exponent;
}

BigNumber::BigNumber(){
int count(0);

while (coefficient / 10 != 0){
coefficient = coefficient / 10;
count++;
}
exponent = exponent + count;
}

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;
}

bool BigNumber::operator < (BigNumber other){//These have to be bools How to make?
if (exponent < other.exponent){
return true;
}
else{
if (exponent > other.exponent){
return false;
}
else{
if(coefficient < other.coefficient){
return true;
}else{
return false;
}
}
}

return false;
}

bool BigNumber::operator >(BigNumber other){
if (exponent > other.exponent){
return true;
}
else{
if(exponent < other.exponent){
return false;
}
else{
if(coefficient > other.coefficient){
return true;
}else{
return false;
}
}
}

return false;
}

void BigNumber::setCoefficient(double x){
coefficient = x;
}

void BigNumber::setExponent(int y){
exponent = y;
}

ostream & operator << (ostream &out, BigNumber n){

if (n.getCoefficient() == 0){
out << 0;
return out;
}
else {
out << n.getCoefficient() << " x 10^" << n.getExponent();
return out;
}
}``````

``````#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);
bool operator <(BigNumber other);
bool operator >(BigNumber other);
void setCoefficient(double x);
void setExponent(int y);
friend ostream & operator << (ostream &out, BigNumber n);
};
#endif``````

Main

``````#include <iostream>
#include <cmath>
#include <string>
#include "bignumber.h"
#include <ostream>
using namespace std;

int main(){
double coef1;
int expo1;
double coef2;
int expo2;
char h;

cout << "What operation would you like to perform? " << h << endl;
cin >> h;
cout << "What is your first number (coefficient followed by exponent) ? " << coef1 << expo1 <<endl;
cin >> coef1 >> expo1;
cout << "What is your second number (coefficient followed by exponent) ? " << coef2 << expo2 << endl;
cin >> coef2 >> expo2;

BigNumber a(coef1, expo1);
BigNumber b(coef2, expo2);

if (h == '*'){
cout << a * b << endl;
}``````

## jonsca 1,059

On line 18 you try to cout the h value before it was read in. I'm not sure what you were trying to do there. You declared it on 16 but you didn't initialize it. Initializing it would be `char h = 'z';` . It never hurts to do that so that it has a value (you can pick something more logical than 'z') but doing it without fixing the other error will just mask the problem.

## ELBUF

Perfect. Thank you so much. Youve been a huge help.