-
Application Repeatition
How does one code an application to repeat? My application functions fine but I need to have the program start from the beginning after it concludes. Their must be a command when I am by (return).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//Function Declaration
int main(int argc, char *argv[])
{
//Local Declarations
int a= 1; //First Integer
int b= 0; //Least Allowed Number
int x; //User inputted Number
int sum; //Sum of numbers multiplied
//Statements
printf("Input a Number:"); //User Instructions
scanf("%d", &x); //User inputs number
if (x >= b) //If statement
{
sum = x * a; //Number Multiplied by 1 and results
a = a+1;
printf("%d*1=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 2 and results
a = a+1;
printf("%d*2=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 3 and results
a = a+1;
printf("%d*3=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 4 and results
a = a+1;
printf("%d*4=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 5 and results
a = a+1;
printf("%d*5=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 6 and results
a = a+1;
printf("%d*6=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 7 and results
a = a+1;
printf("%d*7=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 8 and results
a = a+1;
printf("%d*8=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 9 and results
a = a+1;
printf("%d*9=%d\n\n", x, sum);
sum = x * a; //Number Multiplied by 10 and results
a = a+1;
printf("%d*10=%d\n\n", x, sum);
}
else //Closes program with Negative Integer
{
return 0;
}
system("PAUSE");
return 0;
}
-
if you post your code you will get better help.
You could always just enclose your whole program in a while loop and check the condition after each iteration of your program.
-
Usually, that is done using loops of some sort. C supports three loop constructs:
- for
Normally for when you have a fixed [1] number of repetitions to do. Not necessarily fixed at the time of writint the code, but there is a distinct "we start at here, and go until we get to this point" type thing. - while
For when you have a situation where repetition at all may happen - do .. while
When you have a situation that always at least ONE repetition should happen.
There is two other ways to cause repetition in C:
- Recursion.
You call function f() inside function f(). Note that this is illegal for main in C++, but technicaly legal in C. Generally, this is not what you should do for repetition unless you have a particular type of problem that lends itself to recursive solution. You should not do this if you don't know how many loops it may be - do too many loops, and the program crashes. - Goto [and setjmp/longjmp]
You can make loops with goto, but goto is not recommended unless you have explored all other solutions and come up with worse solutions in all cases. I use maybe one goto every 4-5 months in my code - generally in cases where I'm in deep nested loops and need to get out quick because things are horribly wrong (e.g. can't allocate memory and need to clean something up before returning with an error code). Do not use this method for simply repeating a piece of code again.
--
Mats
-
Perhaps you want a while loop which will repeat some code until the user requests a quit?
For example,
Code:
char item;
do {
print_menu();
item = getchar();
while(getchar() != '\n') {} /* remove characters until newline */
handle_item(item);
} while(item != 'q');
[edit] Don't forget that [/list], matsp. :) [/edit]