quick question about c language?

Discussion in 'Programmer's Corner' started by killer6008, Feb 9, 2010.

  1. killer6008

    Thread Starter New Member

    Jan 26, 2010
    20
    0
    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.
     
  2. killer6008

    Thread Starter New Member

    Jan 26, 2010
    20
    0
    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;
    }
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    That is actually a poor and undisciplined way to do it. A better expression of that code would be as follows:
    Code ( (Unknown Language)):
    1.  
    2. int main(void)
    3. {
    4. int  i = 1 ;
    5.  
    6.     do
    7.     {
    8.        printf("%d", i++) ;
    9.     }  while(i < 10)
    10.     return 0 ;
    11. }
    12.  
    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.
     
  4. joemmech

    Member

    Jan 22, 2010
    32
    0
    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 ;
    }
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    @joemmech -- Sure can do it that way as well. Try using the code brackets to make code fragments look better.
    Code ( (Unknown Language)):
    1.  
    2. int main(void)
    3. {
    4.     for(i=1;i<10;i++)
    5.     {
    6.       printf("%d", i) ;
    7.     }
    8.     return 0 ;
    9. }
    10.  
     
  6. killer6008

    Thread Starter New Member

    Jan 26, 2010
    20
    0
    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.
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Do it any way you like as long as it works the way you want it to.
     
  8. Rusty_Shackleford

    New Member

    Feb 22, 2010
    1
    0
    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: Feb 23, 2010
  9. symqwerty

    Member

    Feb 22, 2010
    31
    0
    I agree with you..thumbs up
     
  10. Blofeld

    Active Member

    Feb 21, 2010
    82
    18
    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.
     
Loading...