How to repeat a simple program

Hi guys.

I'm still new with C++. I'm trying to repeat a simple program. I'm not getting any errors in Dev c++ but when i tried to repeat the program, with do while loop, the program won't respond. When i say it won't respond, it would run but when i press 1 to repeat the program, it won't proceed. Below is my code. I'm hoping you could help me out here.

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


#include <stdlib.h>
#include <stdio.h>

int main()
{   
char Repeat;
int n;
int LoopControl=1;

do
{
          
    printf("Please insert a number : \n");
    scanf("%d",&n);
    printf("The sum of squares for %d is %d\n", n,sumoftheSquares(n));
	printf("Press [1] to repeat program and Press [2] to exit: ");
    scanf("%d", LoopControl);
     
}
while (LoopControl != 1 );
printf("Goodbye!");
system ("pause");
return 0; 

}
int sumoftheSquares(int n)
{

	if (n == 0)
		return (0);
	else
	{
		return (sumoftheSquares(n-1) + (n*n));
	}
}

your loop repeats until ( LoopControl != 1 ) so pressing 1, and hence setting LoopControl to 1, will break out of the loop. Perhaps you should change the while condition to while(LoopControl == 1);
Thanks for the quick reply MintBerryCrunch however i tried that and it didn't work.
What do you mean it didn't work? You need to be more specific
What i meant was, when i press 1, a pop up will appear saying that "the program stop working".
scanf("%d", &LoopControl);
Compare that to your line 19.
Also, you should either declare a prototype of your function, or define the function, before main.
Last edited on
Fixed as below:

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
#include <stdlib.h>
#include <stdio.h>

int sumoftheSquares(int n);

int main()
{   
char Repeat;
int n;
int LoopControl=1;

do
{
          
    printf("Please insert a number : \n");
    scanf("%d",&n);
    printf("The sum of squares for %d is %d\n", n,sumoftheSquares(n));
	printf("Press [1] to repeat program and Press [2] to exit: ");
    scanf("%d", &LoopControl);
     
}
while (LoopControl != 2 );
printf("Goodbye!");
system ("pause");
return 0; 

}
int sumoftheSquares(int n)
{

	if (n == 0)
		return (0);
	else
	{
		return (sumoftheSquares(n-1) + (n*n));
	}
}
Topic archived. No new replies allowed.