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.
#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);
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.
#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));
}
}