Trying to cause this program to execute continously

I have completed this program outlining mostl of the required specifications I need. However, I need to be able to run continously until the user chooses to quit. The requirements is to make this happen without using for, while, or d0 while. Any pointers?







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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*NAME: 
DATE: December 01, 2008
FILENAME: Program6.cpp
PURPOSE: T0 calculate simple math
COMPILER: Dev C++
LIMITATIONS: Add, Subtract, Multiply, Divide, and Average only 2 numbers (1 digit each), 
             Only whole numbers for inputs, no fractions or decimals
*/

#include <iostream>
#include <cstdlib>
#include <string>
#include <math.h>
#include <windows.h>
 
using namespace std;
 
int num1, num2;
float sum;
float sub;
float multi;
float divi;
float average;
 
void getInput( );             
void calculateSum ( );
void writeSum ( );
void calculateSub ( );
void writeSub ( );
void calculateMulti ( );
void writeMulti ( );
void calculateDivi ( );
void writeDivi ( );
void calculateAverage( ); 
void writeAverage( ); 
int main()
 
{
 
 
 
system("title Simple Math Calculation");
	 
 
 
 
    getInput( );            
    calculateSum ( );
    writeSum ( );
    calculateSub ( );
    writeSub ( );
    calculateMulti ( );
    writeMulti ( );
    calculateDivi ( );
    writeDivi ( );
    calculateAverage( );
    writeAverage( );
     
     
    system("pause");
    return (0);
 
}
 
 
    void getInput()
    {
        cout <<  "Welcome to Math Made Simple!" << endl;
        cout <<"                               \n";
        cout <<"                               \n";
         
         
         
        cout << "Type 2 numbers to add, space between the numbers, then Press Enter: ";
        cin >> num1 >> num2;
    }
    void calculateSum ( )
    {
        sum = num1 + num2;
    }
    void writeSum( )
    {
        cout << "The sum is " << sum << endl;
        cout <<"                               \n";
         
         
        cout << "Type 2 numbers to subtract, space between the numbers, then Press Enter: ";
        cin >> num1 >> num2;
    }
    void calculateSub ( )
    {
        sub = num1 - num2;
    }
    void writeSub ( )
    {
        cout << "The difference is " << sub << endl;
        cout <<"                               \n";
         
        cout << "Type 1 multiplicand and 1 multiplicator to multiply, space between the numbers, then Press Enter: ";
        cin >> num1 >> num2;
    }
    void calculateMulti ( )
    {
        multi = num1 * num2;
    }
    void writeMulti ( )
    {
        cout << "The product is " << multi << endl;
        cout <<"                               \n";
         
        cout << "Type 1 dividend and 1 divisor to divide, space between the numbers, then Press Enter: ";
        cin >> num1 >> num2;
    }
    void calculateDivi ( )
    {
        divi = num1 / num2;
    }
    void writeDivi ( )
    {
        cout << "The quotient is " << divi << endl;
        cout <<"                               \n";
        
        cout <<  "Type 2 numbers to average, space between the numbers, then Press Enter: ";
        cin  >>  num1 >> num2;
    }
    void calculateAverage( )
    {
        average = (num1 + num2)/2.0;  
    }
    void writeAverage( )
    {
        cout << "The average is " << average << endl;
        cout <<"                               \n";
        

     


}
Last edited on
The only solution seems to be a goto loop.
What about recursion?
Bazzy,
Your solution really works well.
I learned a lot from this.
Many thanks!
Yeah Bazzy has an interesting idea...

You would need to add something to the getInput function that gives the user an option to quit.. like if they type "quit"

then just set quit to true and ur good:


Do something like this...

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  bool quit=false; //define a boolean quit variable
  getInput();
  if (!quit) //only execute all that junk if the user hasn't chosen to quit
  {
    //...
    main();  //recall the main function.. essentially starting over.
  }
  else  //if the user did choose to quit
    return 0;  //end the program
}


Last edited on
Calling main()==BAD idea
Using recursion (even if you use another function other than main) can also raise a couple of other problems.

1. A function running continuously and recursing continuously can create a stack overflow.
Although this may need several thousand recursions to occur.
Most likely no user will have the patience to do that many calculations in one go - but the potential for stack overflow still exists.

2. It can also mean that with the backlog of function calls to complete, when the user presses Q to exit, all it will do is return to the previous copy of
the function, where the user will have to press Q again, and again, and again, depending
on how may times the function recursed. (Recurse 10 times and user may have to
press Q 10 times) - but it depends on your exit strategy.
Agreeing with helios for sure! Calling main would be like killing a unicorn.
I didn't mean to call main() for recursion. The way functions in the original post were implemented seemed to me that each could call the next and for exiting I would use a global variable.
Fine... why would you ever not be able to use loops anyway....
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
bool quit=false;
void everything()
{
  if (quit)
    return;

  getInput();
  calculateSum();
  writeSum();
  calculateSub();
  writeSub();
  calculateMulti();
  writeMulti();
  calculateDivi();
  writeDivi();
  calculateAverage();
  writeAverage();

  everything();
}

int main()
{
  everything();
  return 0;
}


Topic archived. No new replies allowed.