#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int firstadd,secondadd,initialsubamount, deductor, firstmult, secondmult, quotient, sum, product, difference;
double dividend, divisor;
string answer3;
int exit2;
int loop;
int exit()
{
cout<< "would you like to end the program?\n";
cin>> answer3;
if (answer3=="yes" || answer3=="yep" || answer3=="okay")
{
cout<< "Okay Goodbye";
}
else
{
cout<< "okay program will not terminate\n";
loop=main(); // ERROR IS RIGHT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
return 0;
}
int main()
{
string answer;
string answer2;
string and;
cout<<"This is a calculator to add, subtract, multiply, or divide two numbers. Would you like to add subtract multiply or divide?\n";
cin>> answer;
if (answer=="Subtract" || answer=="subtract")
{
cout<<"Okay you would like to " << answer << "\n" << "Enter the inital amount then the amount you want to subtract from the initial amount\n";
cin>> initialsubamount>> and >>deductor;
int difference=initialsubamount - deductor;
cout<< difference <<endl;
}
else if (answer=="multiply"||answer=="Multiply")
{
cout<<"okay you would like to " << answer <<"\n" << "enter a number then the number you would like to multiply it by\n";
cin>> firstmult >> and >>secondmult;
signed product=firstmult * secondmult;
cout<< product <<endl;
}
else if (answer=="divide" || answer=="Divide")
{
cout<<"okay you would like to " << answer <<"\n" << "enter the inital value then the amount you want to divide from it\n";
cin>> dividend>>and>>divisor;
double quotient = (dividend / divisor);
cout<< quotient <<endl;
}
else if (answer=="add" || answer=="Add")
{
cout<<"okay you would like to add\n" << "enter two numbers that you want to add\n";
cin>> firstadd >> and >> secondadd;
signed sum=firstadd + secondadd;
cout<< sum << " is your answer"<<endl;
}
else
{
exit2=exit();
return 0;
}
cout<<"would you like to do another calculation?\n";
cin>>answer2;
if (answer2=="yes")
{
cout<<"okay the calculator will rerun\n";
loop=main();
}
else
{
exit2=exit();
return 0;
}
}
Holy code tags batman. Be surprised that someone actually responded, because looking at something that didn't use code tags is brutal.
The primary mistake you are making is that you cannot call main(). main() is called once and only once by the [operating system?] as the entry point to your program.
The second mistake is that on line 25, you call a function without declaring it above the first usage in the file. You'd need to declare that function beforehand. It's main() so this wouldn't work anyways, but in general that's a problem.
Your final mistake is on line 77 where you try and call main from main() giving it a recursive flavor. main() cannot be called. Use an infinite loop loop instead and break if a condition is true.
#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
usingnamespace std;
int firstadd,secondadd,initialsubamount, deductor, firstmult, secondmult, quotient, sum, product, difference;
double dividend, divisor;
string answer3;
int exit2;
int loop;
int exit()
{
cout<< "would you like to end the program?\n";
cin>> answer3;
if (answer3=="yes" || answer3=="yep" || answer3=="okay")
{
cout<< "Okay Goodbye";
}
else
{
cout<< "okay program will not terminate\n";
loop=main(); // ERROR IS RIGHT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
return 0;
}
int main()
{
string answer;
string answer2;
string and;
cout<<"This is a calculator to add, subtract, multiply, or divide two numbers. Would you like to add subtract multiply or divide?\n";
cin>> answer;
if (answer=="Subtract" || answer=="subtract")
{
cout<<"Okay you would like to " << answer << "\n" << "Enter the inital amount then the amount you want to subtract from the initial amount\n";
cin>> initialsubamount>> and >>deductor;
int difference=initialsubamount - deductor;
cout<< difference <<endl;
}
elseif (answer=="multiply"||answer=="Multiply")
{
cout<<"okay you would like to " << answer <<"\n" << "enter a number then the number you would like to multiply it by\n";
cin>> firstmult >> and >>secondmult;
signed product=firstmult * secondmult;
cout<< product <<endl;
}
elseif (answer=="divide" || answer=="Divide")
{
cout<<"okay you would like to " << answer <<"\n" << "enter the inital value then the amount you want to divide from it\n";
cin>> dividend>>and>>divisor;
double quotient = (dividend / divisor);
cout<< quotient <<endl;
}
elseif (answer=="add" || answer=="Add")
{
cout<<"okay you would like to add\n" << "enter two numbers that you want to add\n";
cin>> firstadd >> and >> secondadd;
signed sum=firstadd + secondadd;
cout<< sum << " is your answer"<<endl;
}
else
{
exit2=exit();
return 0;
}
cout<<"would you like to do another calculation?\n";
cin>>answer2;
if (answer2=="yes")
{
cout<<"okay the calculator will rerun\n";
loop=main();
}
else
{
exit2=exit();
return 0;
}
}
You can't call main. Main is the entry point. Only the OS can call main. If you need to loop to the beginning, then enclose the body within a for/next, while or do/while.
okay I have no Idea what nooblet said I dont even know what void is. and yeah im new to calling functions. you guys said you cant call main, but it works when I call it at the end.
It's horribly broken. It doesn't go back to the beginning - it creates a whole new stack frame (which you can think of as a section of memory that "is" the function you've just called) for the new function, main, and then when it gets to the end again and you call main again, it does it again, so now you've got THREE main functions set up in memory, each one with their own values, and then again and again and again and again..... until eventually you run out of space on the stack and the program crashes.
Of course, let's say that at the end of one of those main functions, you don't repeat calling you main and the function does actually return. Does the program end? No! It goes back to the previous main, and carries on from there, and then has to unwind that, and then the next one, and the next and the next and the next... oh, what a mess :( All for want of a proper loop.