Repeating entire program

I would like to have my entire program to repeat a single time without user input. How could I do this with both main and foo? I know how to repeat a program when there is just main but not when there are two functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int foo()
{
	int a,
		b,
		c,
		d;
	cout << "Please enter three numbers: " << endl;
	cin >> a;
	cin >> b;
	cin >> c;
	d = a + b + c;
	return d;
}
int main()
{
	int Sum = foo();

	cout << "Your sum of these numbers is: " << Sum << "\n" << endl;

	return 0;
}
based on your description this answers your question, but I am not sure if this solves your problem.

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

#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int foo()
{
    int a,
    b,
    c,
    d;
    cout << "Please enter three numbers: " << endl;
    cin >> a;
    cin >> b;
    cin >> c;
    d = a + b + c;
    return d;
}
int main()
{
    bool first_run = true;
    while (first_run) {
        int Sum = foo();
        
        cout << "Your sum of these numbers is: " << Sum << "\n" << endl;
        first_run = false;
    }
    
    int Sum = foo();
    
    cout << "Your sum of these numbers is: " << Sum << "\n" << endl;
    
    
    return 0;
}
Its exactly what I needed. However I dont understand how this works. Do you mind explaining to me whats going on in line 25 - 29? How does it know to go back up and run foo again?
1
2
3
4
5
6
 while (first_run) {
        int Sum = foo();
        
        cout << "Your sum of these numbers is: " << Sum << "\n" << endl;
        first_run = false;
    }


in other words


1
2
3
4
5
6
this = true;
while (this = true)
{
run this code.
then set this = false.
}



so it sets "first_run" to true.
while loop works while "this" is true
then once while loops executes it sets "this" to false...
then it does not run again.

hope that makes sense.
Last edited on
TieRein line 24 he creates a bool named first_run and sets it to true.

Line 25: While loop. Stating that WHILE the bool you created is "true" what ever follows will keep happening until it's turned to false.

Line 26: Is stating that the integer named "Sum" is equal to integer named "foo()"

Line 28: The CMD will say "Your sum of these numbers is: "your sum"

Line 29:: Turns your bool first_run to false, shutting down the loop.
What causes foo to run again? It it when it calls on "int Sum = foo();"?

What I dont understand is how its not just taking the return number from foo twice. How is foo running twice?
Its exactly what I needed. However I dont understand how this works. Do you mind explaining to me whats going on in line 25 - 29? How does it know to go back up and run foo again?


The code in question is a loop which only runs once. It is a bit of obfuscation.

The main is equivalent to:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int Sum = foo();
        
    cout << "Your sum of these numbers is: " << Sum << "\n" << endl;
    
    int Sum = foo();
    
    cout << "Your sum of these numbers is: " << Sum << "\n" << endl;
    
 }


Something more useful might be:

1
2
3
4
5
6
7
8
9
int main()
{
    const unsigned times_to_execute = 2 ;
    for (unsigned i=0; i<times_to_execute; ++i)
    {
        int sum = foo();
        cout << "Your sum is: " << sum << '\n';
    }
 }


Last edited on
Topic archived. No new replies allowed.