Hi guys,
Im trying to write a calculator in c++ to improve my skills but I've come across a problem....I read the type of calculation the user wants to perform by using cases...then it jumps to the type of calculation the user wants to perform...I should say these are all in separate functions including the question...the problem I have is when the user wants to perform the calculation again...I want the user to be able to choose between performing the same calculation again and performing a different calculation...or exiting for that matter....however I dont know how to go about performing the same one again....because I have an again function that the program jumps to after performing the first calculation...any help will be great...Ill post the code...apologies if its messy its a beginners work in progress!!! Ill proabably change the options choices in again() to cases too however if its easier to read in a word and call the word I can do that! Thanks!
int squareroot();
int again();
int power();
int question();
int squared();
using namespace std;
int calculation;
string calc;
int main ()
{
question();
return 0;
}
int question()
{
cout << "What calculation would you like to perform? Exit = 0";
cout << "\n1: Square root\n2: Square\n3: Power\n";
cin >> calculation;
switch (calculation)
{
case 0:
cout << "Ok thank you for using the calculator\n";
cout << "Press any key to continue...";
getch();
break;
case 1:
squareroot();
break;
case 2:
squared();
break;
case 3:
power();
break;
default:
cout << "Sorry I couldn't recognise that please try again.\n";
question();
break;
}
}
int squared()
{
double number, result;
cout << "Please enter a number to get the square: ";
cin >> number;
result = number * number;
cout << "The square is: " << result;
again();
}
int power()
{
double number, power, result;
cout << "Please enter a number: ";
cin >> number;
cout << "Please enter a power: ";
cin >> power;
result = pow(number, power);
cout << "The result is: " << result;
again();
}
int squareroot()
{
double number, result;
cout << "Please enter a number to get the square root: ";
cin >> number;
if (number < 0)
{
cout << "Sorry no negative numbers!\n";
squareroot();
}
else
{
result = sqrt (number);
cout << "The square root is: " << result;
}
again();
}
int again()
{
string answer;
cout << "\nWould you like to perform another calculation?\nOr a different calculation (Same/Diff/No) ";
cin >> answer;
for (int i=0;i<sizeof(answer);i++)
{
answer[i] = tolower(answer[i]);
}
if (answer == "same" || answer == "s")
{
squareroot();
}
else if (answer == "diff" || answer == "d")
{
question();
}
else if (answer == "no" || answer == "n")
{
cout << "Ok thank you for using the calculator\n";
getch();
}
else
{
cout << "\nSorry I didn't understand your answer please try again.";
again();
If i were you, I would completely get rid of the again function and just loop around the program and when they want to exit they will type in "0" when they are asked the question. then jump out. A real calculator doesnt ask me if I want to do the same calculation or a different one. It just gives me the result and then I am free to choose what I want(just call the question function again). Also you again() function has problems. If you choose to run the "same" option it will always run the square root and not necessarily the one you are on.
Another way to simulate the again() function would be to put a while loop inside each calculation function. This while loop would do the calculation and then ask if they would like to go again etc. as soon as they type no, it will leave the function.
Hope that helped. Also it would be very helpful next time if you used code fomat. it its a symbo that looks like "<>" in the format box to the right when you are typing something.
Thanks I didn't know how people were inserting code like that! I just wanted to know if there was a way to call different functions using the information read in...I have done a good bit of java and dont know of any way I was just wondering if C++ had a way to do that....yeah I know it calls the squareroot function that was there before I extended the calculator I didn't change it as I hadn't another solution yet! I reckon I will just scrap it! Thanks!!