Thread: Looping a code

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    8

    Question Looping a code

    hi, i want to loop my code, where if the choice is 1, it will print the results then i want to ask the user if he/she wants to run the code again. if answered yes, the code will run again from the start. thank you

    Code:
    void main()
    {
     char  emp[30], id[10];
     int   days, ot;
     float rate, bp, oot, gp, np, sss, phil, pag;
     int   x, y, choice;
    
     clrscr();
    
     g(5,15);  p("Employee's name: ");
     g(5,16);  p("ID number: ");
     g(5,17);  p("Rate per day: ");
     g(50,15); p("Days: ");
     g(50,16); p("Overtime: ");
    
     g(22,15); gets(emp);
     g(22,16); gets(id);
     g(22,17); s("%f", &rate);
     g(60,15); s("%d", &days);
     g(60,16); s("%d", &ot);
    
      gp  = grosspay(rate, days, ot);
      sss = ssscom(gp);
      pag = pagcom(gp);
      phil= philcom(gp);
      np  = netpay(gp, sss, phil, pag);
    
     g(5,20);  p("Your net pay is: %.3f ", np);
     g(5,22);  p("Do you want to see the breakdown?  [1] YES  [2] NO : ");
     g(58,22); s("%d", &choice);
    
    
     if(choice==1)
     {
    
           bp = rate*days;
           oot= (1.25*(rate/8))*ot;
    
    
           g(28,27); p("EMPLOYEE'S WAGE INFORMATION");
           g(5,29);  p("Employee:      %s", emp);
           g(5,30);  p("ID number:     %s", id);
           g(5,31);  p("Rate per day:  P %.2f", rate);
           g(50,29); p("Days:     %d", days);
           g(50,30); p("Overtime: %d", ot);
    
           g(20,35); p("-EARNINGS-");
           g(5,37);  p("Basic pay: P %.3f", bp);
           g(5,38);  p("Overtime:  P %.3f", oot);
           g(5,41);  p("Grosspay:  P %.3f", gp);
    
           g(50,35); p("-DEDUCTIONS-");
           g(38,37); p("SSS:        P %.3f", sss);
           g(38,38); p("PAGIBIG:    P %.3f", pag);
           g(38,39); p("PhilHealth: P %.3f", phil);
           g(38,41); p("Netpay:     P %.3f", np);
    
          This is where i want to ask if a user wants to run this block of code again, if answered YES.
    
     }
    
    
     else if(choice==2)
     {
    
           clrscr();
           for(x=31;x<=51;x++)
           {
    	g(x,23); p("Í");
    	g(x,25); p("Í");
           }
    
           g(31,24); p("º");
           g(51,24); p("º");
           g(31,23); p("É");
           g(51,23); p("»");
           g(31,25); p("È");
           g(51,25); p("¼");
           g(32,24); p("Thank you, goodbye!");
     }
    
     else
     {
           clrscr();
           g(33,24); p("Invalid Input!");
    
       Here also
     }
    
    
     getch();
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    please help, i need this tomorrow. thank you

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know what language this is, but in C, you need to declare your variables. You have not declared g, p, or s, in your program.

    It won't link.

    If you need it by tomorrow, you should have posted it before 10:51 pm the night before.
    Last edited by Adak; 08-22-2010 at 01:15 AM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    this is c, i just not included the #includes and #defines, i just posted the main() because i just need to loop the code. p is printf, s is scanf and g is gotoxy, and it is only 6:47pm here in the Philippines =)

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Add a do/while loop, a new variable to hold the user's answer, and a printf, scanf and compare of the user's answer to your question and you'll be good to go.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    this is what i did:

    Code:
    main()
    {
    
    clrscr();
    
    do {
    
    if(conditions)
    {
      some prints
    
     printf("Again?");
     scanf("%d", &z);
    }
    
    else if(condition)
    {
     some prints
    }
    
    else
    {
      some prints
    
     ANOTHER ASKING IF WANT TO RE-RUN THE WHOLE CODE
    }
    
    } while(z==1);
    
    getch();
    }

    am i doing this correctly? but the problem is, when i ended up in else and asks again to run the code, i cannot input anything and the program ends. also another thing, when it loops, i cannot get back to input on Employee's name, it skips to 2nd line where i need to input the ID. thanks you

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    hi, i want to loop my code, where if the choice is 1, it will print the results then i want to ask the user if he/she wants to run the code again. if answered yes, the code will run again from the start. thank you
    The key here is a structured approach.

    Step 1. Dissecting your requirements:

    * Apparently your code does something
    * You want to print results when user enters a particular symbol
    * You want to loop when user enters a particular symbol

    Step 2. Translate to flow diagram:

    Code:
    +--------------+
    | Do something |<-------------------------------+
    +--------------+                                |
            |                                       |
            v                                       |
    +----------------+                              |
    | Get user input |                              |
    +----------------+                              |
            |                                       |
            v                                       |
            *                                       |
           / \                                      |
          /   \                                     |
         /     \                                    |
        /       \                                   |
       /         \                                  |
      /           \   YES   +---------------+       |
     * input = 1?  * -----> | Print results |       |
      \           /         +---------------+       |
       \         /                   |              |
        \       /                    |              |
         \     /                     |              |
          \   /                      |              |
           \ /                       |              |
            *                        |              |
            |                        |              |
         NO +------------------------+              |
            |                                       |
            v                                       |
            *                                       |
           / \                                      |
          /   \                                     |
         /     \                                    |
        /       \                                   |
       /         \                                  |
      /           \   YES                           |
     * input = y?  * -------------------------------+
      \           /         
       \         /
        \       /
         \     /
          \   /
           \ / 
            *
         NO |
            v
         +------+
         | Done |
         +------+
    Notice how, up until this point, it does not matter whether you speak C, Python, Perl, or whatever. This is simply a description of how your program is supposed to work. Once you have this drawn up, the actual implementation shouldn't be too hard now.

    Step 3. Translate to pseudo-code, or a skeleton framework:

    Code:
    while(!done)
    {
        result = do_something();
    
        key = get_user_input();
        if(key == 1)
            print(results);
    
        key = get_user_input();
        if(key != y)
            done = true;
    }
    Step 4. Translate to C:

    Up to you.

    Stick to this general approach for all your projects, and you will succeed.

    Additional note: USE PROPER INDENTATION. There is absolutely no excuse for not doing so.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    wow thanks for that, ok i will. thanks for the hard work =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Help With C code looping
    By maker10 in forum C Programming
    Replies: 11
    Last Post: 10-23-2008, 10:57 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM