quick question about c language?

Thread Starter

killer6008

Joined Jan 26, 2010
20
Hi

I am writing a program using visual studio and c and I want to know

that when my program ends and I want to put a for loop to see if Repeat=yes how can i make it start from a certain point again?
its only one simple file no functions or anything.
 

Thread Starter

killer6008

Joined Jan 26, 2010
20
nevermind i found the solution myself.

Just for future reference if anyone else wants to know:

A quick example can be this


#include <stdio.h>

int main(void) {

int i;

i = 1;

again:
printf("%d ", i);
i++;
if(i<10)
goto again;

return 0;
}
 

Papabravo

Joined Feb 24, 2006
21,225
That is actually a poor and undisciplined way to do it. A better expression of that code would be as follows:
Rich (BB code):
int main(void)
{
int  i = 1 ;
 
    do
    {
       printf("%d", i++) ;
    }  while(i < 10)
    return 0 ;
}
This avoids the necessity of creating and keeping track of the label again. It is posited that this form is easier to read, maintain, and understand. That said they do make chocolate ice cream because there is no accounting for taste. Judge for yourself.
 

joemmech

Joined Jan 22, 2010
32
I agree with Papabravo. You can also use do it this way:

int main(void)
{
for(i=1;i<10;i++)
{
printf("%d", i) ;
}
return 0 ;
}
 

Papabravo

Joined Feb 24, 2006
21,225
I agree with Papabravo. You can also use do it this way:

int main(void)
{
for(i=1;i<10;i++)
{
printf("%d", i) ;
}
return 0 ;
}
@joemmech -- Sure can do it that way as well. Try using the code brackets to make code fragments look better.
Rich (BB code):
int main(void)
{
    for(i=1;i<10;i++)
    {
      printf("%d", i) ;
    } 
    return 0 ;
}
 

Thread Starter

killer6008

Joined Jan 26, 2010
20
Hi

But I have a huge file even though its just one and right when it reaches to the end of the file I have to ask the user again if they want to repeat it and then take them back to even before half way of the file and repeat all them steps again and as tat reaches to an end ask the user again.

These ways look a bit complicated to me knowing that I have to fit all that code. Sorry but i am a beginner and this is my first time I am doing an assignment on C.
 
try a structure like this..
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char rsp='N';

do
{
//whatever your program does goes here
printf("Continue -Y/N");
} while( toupper(rsp=getchar() ) == 'Y');

return 0 ;
}
 
Last edited:

Blofeld

Joined Feb 21, 2010
83
Hi Killer,

whether you should follow Papabravo's advice depends on what your ultimate goals are. Do you only need to write a short program occasionally ? Then you can stick with whatever programming style you are most comfortable with. Things get more complicated if you plan to be a professional programmer. If you will work as the only programmer in a small company, your boss might not care too much for your programming style as long as your program delivers the functionality he needs.

If, however, you work in a big company on projects where several programmers have to work together as a team, there is a high probability that you will be forced to use some coding guidelines. Nearly all of these guidelines explicitly forbid the usage of "goto", except for some very special cases. They also limit the maximum size of a file you are allowed to use.

So, if this is what you are planning to do, it might be a good idea to teach yourself a good programming style right from the start. "Unlearning" a bad style might be harder for you later.
 
Top