Hello all, I was wondering how I could make this with larger numbers instead of scientific notation or whatever it is. I have attached my code and here is what I did:

1. Typed in E for exponent and hit enter.

2. Entered 9 and hit enter.

3. Entered 9 so it can be 9 to the 9th power and hit enter.

4. and it came up with this: 3.8742e+008

I want it to be like a real calculator so it can be: 387420489

Thank you everyone for all your help.

Gangsta gama

Attachments
#include<iostream.h>
#include<math.h>
#include<stdlib.h>

unsigned long Ex;
unsigned long Ey;
unsigned long Sx;
unsigned long Sy;
char answer;
int exponent();
int squareRoot();
      
int squareRoot()
{
    system("CLS");
     cout << "Enter a number." << endl;
     cin >> Sx;
     system("CLS");
     cout << sqrt(Sx) << endl;
     cout << "Would you like to go again? (Y/N)" << endl;
     cin >> answer;
     
     if(answer == 'y' || answer == 'Y')
     {
               squareRoot();
     }
     else if(answer != 'y' || answer != 'Y')
     {
          cin.get();
     }
     
     return 0;
}
      
int exponent()
{
     system("CLS");
     cout << "Enter a number." << endl;
     cin >> Ex;
     system("CLS");
     cout << "Enter an exponent." << endl;
     cin >> Ey;
     system("CLS");
     cout << pow(Ex,Ey) << endl;
     cout << "Would you like to go again? (Y/N)" << endl;
     cin >> answer;
     
     if(answer == 'y' || answer == 'Y')
     {
               exponent();
     }
     else if(answer != 'y' || answer != 'Y')
     {
          cin.get();
     }
     
     return 0;
}

int main()
{      
      system("CLS");
      cout << "Would you like to calculate (E)xponents or (S)quare roots?" << endl;      
      cin >> answer;
      
      if(answer == 'E' || answer == 'e')
      {
                exponent();
      }
      else if(answer == 'S' || answer == 's')
      {
           squareRoot();
      }
      else if(answer != 'S' || answer != 's' || answer != 'E' || answer != 'e')
      {
           cout << "Enter either S for square root or E for exponents." << endl;
           main();
      }
      
      system("PAUSE");
      return 0;
}

Perhaps you should define a double variable like double p;, then do p= pow(Ex,Ey) and cout << p?

1. Fixed (not scientific) notation, no fractional part:

#include <iomanip>
...
cout << fixed << setprecision(0) << pow(Ex,Ey) << endl;

2. Use double for Ex. Ey, Sx, Sy. Declare these variables locally (in functions), no need in global scope. Don't capitalize ordinal variable names...
3. Delete all irrelevant and nonportable system("CLS") calls. Don't use system("pause") too, add cin.get() to wait user input.
4. It's illegal in C and C++ to call main recursively. Don't use recursion for "try again" calls of other calculations. Better define a special function to ask user for repetitive calc, for example:

bool tryAgain()
{
    cout << "Try again (y/n)\? " << flush;
    char answer;
    cin >> answer;
    cin.ignore(1000,'\n');
    return answer == 'y' || answer == 'Y';
}

Let exponent and squareRoot functions ask user to get arguments and print result only:

do
    exponent();
while (tryAgain());

Use the same approach in the main dialog loop (not recursion!)...
5. Correct these funny trailed elseif:

if (answer == 'y' || answer == 'Y')
...
     else if (answer != 'y' || answer != 'Y')

It seems you don't understand the meaning of else clause. Keep it simpler (and correctly):

if (answer == 'y' || answer == 'Y')
   ... yes, yes...
else
   ... NOT yes, no, no

Apropos, else (no) condition is answer != 'y' && answer != 'Y' ...
6. What compiler are you using? In modern C++ you must include <iostream>...

Ooh! Is it all?;)..

Thank you, ArkM.

I will try what you did. The compiler I am using is Dev c++ and it is not working right now, so that is why I am writing back. I have one question though... what does setprecision do?

Thank you,
Gangsta Gama

This article has been dead for over six months. Start a new discussion instead.