Advertisement

Continual Loop

Started by May 10, 2000 02:32 PM
6 comments, last by Tiso 24 years, 7 months ago
I'm still learning C and I'm trying to make a basic program that works off of certain numbers typed in. But, I've only learned up to the point where once the program does it's thing it shuts off. How do I keep this from happening so users can access another part of my program without restarting it? Any help is greatly appreciated. Edited by - Tiso on 5/10/00 2:38:35 PM
quote: ...that works off of certain numbers typed in.


What do you mean? As in a loop? We need more information on what your trying to do, although it sounds as if you mean a loop.

If it is a loop, then you need two of them: One to control the entire program, and one to do the "stuff" loop . The main control loop keeps the program going until the USER wants to leave, and the "stuff" loop does "it's thing". Like so:

void main(){  int done = 0;  int selection;  clrscr();  while(!done)  {    printf("\n\nEnter a number: ");    scanf("%d", &selection);    if(selection == 0)    {      done=1;    }    for(int loop=0; loop < selection; loop++)    {      printf("\nIteration %d", selection);    }  }}  


OK?

Edited by - Zipster on May 10, 2000 4:30:23 PM
Advertisement
So I just add the code I want into that right?

What I meant was like when u enter 2 it does something and if you type in 1 it does something else, get it?
well, id code it:

main()
{
int input;
int done = 0;
clrscr();
/*display options and then allow input
im trusting the last guy''s input command*/
scanf("%d", &input);
while(!done)
{
switch(input)
{
case 0:
done = 1;
break;
case 1:
/*do something*/
break;
/*etc.*/
default:
/*error message*/
}
clrscr();
}
return 0;
}
-PoesRaven
Rats, I don''t get a thing that''s going on. I''ll wait I guess. Man! This is going to slow my program down quite a bit.
Ohhh, so thats what you meant .
A switch statement like the one above will work. I''ll repost that code with some formatting :
main(){    int input;    int done = 0;    clrscr();    /*display options and then allow input    im trusting the last guy''s input command*/    scanf("%d", &input);    while(!done)    {        switch(input)        {            case 0:                done = 1;                break;            case 1:                /*do something*/                break;            /*etc.*/            default:                /*error message*/        }        clrscr();    }    return 0;} 
Advertisement
Okay, I got that. Do I just add my source code to that?
quote: Okay, I got that. Do I just add my source code to that?


Well, actually, that WOULD be your source code (along with including headers, etc.). YOU just fill in the blanks!

This topic is closed to new replies.

Advertisement