C++ beginner help!

May 9, 2012 at 12:20pm
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 :)

So here's the code!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <stdlib.h>
 
using namespace 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");
     }


Thanks in advance!
May 9, 2012 at 12:29pm
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.

system("cls") is bad. Here is something better:
http://www.cplusplus.com/articles/4z18T05o/

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.
Last edited on May 9, 2012 at 12:32pm
May 9, 2012 at 12:34pm
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?
May 9, 2012 at 12:37pm
Thanks for a quick reply!

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.
Last edited on May 9, 2012 at 12:43pm
May 9, 2012 at 2:21pm
Your loop could look something like this:

1
2
3
4
5
6
7
int i;
do {
   meny();

   cout << "Continue? 1 for yes, 0 for no!\n";
   cin >> i;
} while (i == 1);

That way, the menu will be called up as long as the user keeps inputting '1' when the question appears.
May 9, 2012 at 2:24pm
Your Int main should use a while loop and a bool like so
1
2
3
4
5
Do{
Meny()
Cin >> y/n
\\set game to true is yes, and no if n is entered 
}while(game)


This way menu will always repeat as long as you'd like

Sorry if the code is written weird, wrote this on my phone!
Last edited on May 9, 2012 at 2:25pm
May 9, 2012 at 5:02pm
Thanks alot, seems that after I changed it to a loop the freezing issue went away as well :)
Topic archived. No new replies allowed.