Hello! I've recently started messing around with c++ (been doing it for about 4 weeks) and I'm trying to make a simple calculator with 4 basic "countings". But I've come across a problem and I need some help from someone more knowledgeable.
The problem is that when I run my program and choose for example option 1 I should get to the addition section but instead the program freezes for about 15 seconds and then shut down on me.
Another thing I would like to add to my program is a system("cls") so that the screen is cleared when you come back to the menu but I've had some problems with this and would be happy if I could get some help with that aswell.
Btw, the notes and some other stuff are in swedish but you should be able to read the code anyway :)
#include <iostream>
#include <stdlib.h>
usingnamespace std;
void meny();
void addition();
void subtraktion();
void multiplikation();
void division();
double a, b, c;
int val;
main()
{
meny(); //kallar på meny
}
void meny()
{
int val;
cout <<" 1: Addition n 2: Subtraktion n 3: Multiplikation n 4: Division n 5: Avslutann";
cin >>val;
switch(val)
{
case 1:addition();break;
case 2:subtraktion();break;
case 3:multiplikation();break;
case 4:division();break;
default: cout <<"Tack för att du testade min miniräknare!";
}
}
void addition() //Skapar funktionen addition
{
double a,b,c;
int i;
system("cls");
c=a+b;
cout<<"Ange två tal som du vill addera.";
cin>>a>>b;
cout<<"Summan av dina tal är "<<c<<".";
cout<<"nnTryck 1 för att komma tillbaka till menyn.";
cin>>i;
if(i==1)
meny(); //Går till menyn
system("pause");
}
void subtraktion() //Skapar funktionen subtraktion
{
double a,b,c;
int i;
system("cls");
c=a-b;
cout<<"Ange två tal som du vill subtrahera.";
cin>>a>>b;
cout<<"Differensen av dina tal är "<<c<<".";
cout<<"nnTryck 1 för att komma tillbaka till menyn.";
cin>>i;
if(i==1)
meny(); //Går till menyn
system("pause");
}
void multiplikation() //Skapar funktionen multiplikation
{
double a,b,c;
int i;
system("cls");
c=a*b;
cout<<"Ange två tal som du vill multiplicera.";
cin>>a>>b;
cout<<"Produkten av dina tal är "<<c<<".";
cout<<"nnTryck 1 för att komma tillbaka till menyn.";
cin>>i;
if(i==1)
meny(); //Går till menyn
system("pause");
}
void division() //Skapar funktionen division
{
double a,b,c;
int i;
system("cls");
c=a/b;
cout<<"Ange två tal som du vill subtrahera.";
cin>>a>>b;
cout<<"Kvoten av dina tal är "<<c<<".";
cout<<"nnTryck 1 för att komma tillbaka till menyn.";
cin>>i;
if(i==1)
meny(); //Går till menyn
system("pause");
}
You are calling meny() over and over and over again, from within functions that are within meny(). In C++, when you call a function, a whole new set of memory is taken up, and whole new variables created. This is known as a "stack frame". Calling a function over and over again from inside itself creates an endless run of these stack frames. http://en.wikipedia.org/wiki/Call_stack
This is very very bad. You should not repeat meny by calling it again. You should repeat it using a loop.
That all said, when I run your code, I do not see the problem you describe. Here is some sample output:
1: Addition n 2: Subtraktion n 3: Multiplikation n 4: Division n 5: Avslutann1
Ange tvÕ tal som du vill addera.3
4
Summan av dina tal õr 4.83694e-307.nnTryck 1 f÷r att komma tillbaka till menyn.1
1: Addition n 2: Subtraktion n 3: Multiplikation n 4: Division n 5: Avslutann1
Ange tvÕ tal som du vill addera.4
2
Summan av dina tal õr 4.83694e-307.nnTryck 1 f÷r att komma tillbaka till menyn.
Out of curiosity: would replacing 32-35 with "return addition()" etc technically fix it, or would that still start the new call before ending the old one?
As I said in the first post I've only been doing this for a very short time and I'm not very familiar with creating loops so some help on how I might add it in my code would be nice!
Also, as I understand it I should include conio.h and use clrscr(); instead of system("cls")?
That all said, when I run your code, I do not see the problem you describe. Here is some sample output:
I tried running it again and still have the same problem :/
Thanks again.