Thread: Terminating Program

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    Terminating Program

    Second post in one day I'm on a role.

    My question is quite simple I hope, but basically I have an assignment which involves creating a program which simulates an electronic ordering system. I have already completed the bulk of the code and it seems to work fine, my only queery is terminating the program when an invalid input is entered.

    The instuctions state this

    "Even though the program may need to be terminated at several stages, the logic of the main() function shall be such that only one return statement is used to terminate the program in all cases"

    I've managed to terminate it succsesfully using the exit() command however I dont think this is an acceptable way to acheive what I'm aiming for.

    This is an example of one of the functions I have written.

    Code:
     int getQuantity(int Q, int RQ)
    {
    if (Q > 0)
    RQ = Q;
    
    else
    { printf("Wrong Quantity\n");
    exit(0);
    }
    return (RQ);
    }
    Any suggestions would be greatly appreciated.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    So basically you want to get rid of exit(0) right? just replace that by RQ = 0, assuming RQ is an integer.

    EDIT: Also note that exit is a function that terminates your whole program. I am not sure what you are trying to do here but it doesnt look right.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Yeah basically just terminate the program without the need for exit().

    I'll put up the design specifications (just a note I have already finished it, this is just one problem I'm not sure how to fix).

    Design Specifications
    Design and implement in C a program that simulates an electronic component ordering system. The program must be well structured and built based upon the concept of modular design. It shall meet the following specifications:

    a) Prompt the user to input the component type: RJ12, RJ45, FW40 or DB24. Display a message 'Wrong component type if the input is other than specified types of electrical connectors and terminate the program then

    b) Prompt the user to input the ordered quantity. The quantity is supposed to be a positive integer number greater that 0. The program should validate the input value. Display a message 'Wrong quantity if the entered value is invalid and terminate the program then

    c) Calculates the price of ordered components based upon the following retail prices: RJ12 - $ 7.70, RJ45 - $8.20, FW40 - $8.50, DB24 - $9.00
    However, if the quantity is more than 9 items, 5% discount is applied ( to all items ordered ). If the quantity is more than 99 items, 10% discount is applied ( to all items ordered ).

    d) Calculate the delivery fee: same day delivery - $ 30.00, next day delivery - $15.00. Terminate the program if an invalid option is entered

    e) Calculate the total charges as a sum of the component

    Basically I have to break it up into different functions and terminate the entire program if an invalid option is entered. So basically I'v used 3 exit() functions however I'm not sure this is what I'm meant to use.

    Thx again for the response it is greatly appreciated.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, can you post your whole code please?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could use a do while loop, then if the called functions get bad input and the program needs to terminate, you have them return say, a -1. When your do while loop in main gets this -1 returned from any function, then it quits the loop, and the program terminates with a standard return 0 statement at the end.
    Code:
    int main(void) {
      do {
        result = call your input function
         if result == -1
            continue;       //skips over remaining function calls- break would do the same thing
        result = call your cost computation function
        if result == -1
          continue;
        etc.
      }while (result != -1);
    
      return 0;
    } //end of main
    That kind of logic will work.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Don't know why one return statement has to be used. Without that knowledge you can just do workarounds. The simplest way is to do something like:

    Code:
    int main()
    {
       if (first())
          if (second())
              if (third())
                 return OK;
       return ERROR;
    }
    where ERROR and OK are defined integers (probably 0 for OK). If you don't care what main returns just don't use a return statement at all. Of course the function has to return 0 if it succeeds or something else if there was an error.

    Other solution involves goto, which I don't believe you want.

    If the above is ugly you could improvise by making the functions accept a parameter which will indicate if there was an error. If there was then they can just return w/o doing anything.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The return requirement was part of the assignment he was given. I'm sure the teacher wants the students to avoid the "exit doors all over the place", kind of logic.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    I'll post the code I have, it works fine but I did have to cheat to get around the terminate debarkle. Please dont laugh :P.

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    
    
    #define NINE_COE 9
    #define NINTYNINE_COE 99
    #define RJ12 7.70
    #define RJ45 8.20
    #define FW40 8.50
    #define DB24 9.00
    #define FIVE_PERCENT 19/20
    #define TEN_PERCENT 9/10
    #define NEXT_DAY 15
    #define SAME_DAY 30
    
    
    float getComponentType(char T, char Ta, char Tb, char Tc, float P);
    int getQuantity(int Q, int RQ);
    float calcPrice(float Price, int ReturnQ, float DP, float TP);
    float getDeliveryFee(int O, float F);
    float calcTotalCharges(float Fee, float DiscountP);
    
    int main(void)
    
    {
    char T, Ta, Tb, Tc;
    int Q, O, RQ;
    float TC, Total, F, DP, Price, P, DiscountP, TP, Fee;
    int ReturnQ;
    
    
    printf("Enter the component type (RJ12, RJ45, FW40 or DB24):  ");
    scanf("%c%c%c%c", &T, &Ta, &Tb, &Tc);
    
    Price = getComponentType( T, Ta, Tb, Tc, P);
    
    printf("Enter the quantity:  ");
    scanf("%d", &Q);
    
    ReturnQ = getQuantity( Q, RQ);
    DiscountP = calcPrice(Price, ReturnQ, DP, TP);
    
    printf("Component Price $%.2f\n", DiscountP);
    
    printf("Select a delivery option (1 - same day, or 2 - next day):  ");
    scanf("%d", &O);
    
    
    
    
    Fee = getDeliveryFee(O, F);
    Total = calcTotalCharges( Fee, DiscountP);
    
    
    printf("Total Charges: $%.2f\n", Total);
    
    return (0);
    }
    
    float getComponentType(char T, char Ta, char Tb, char Tc, float P)
         {
    if (T == 'R' && Ta == 'J' && Tb == '1' && Tc == '2' || T == 'r' || Ta == 'j')
    
        P = RJ12;
    else if (T == 'R' && Ta == 'J' && Tb == '4' && Tc == '5' || T == 'r' || Ta == 'j')
    
        P = RJ45;
    else if (T == 'F' && Ta == 'W' && Tb == '4' && Tc == '0' || T == 'f' || Ta == 'w')
    
        P = FW40;
    else if (T == 'D' && Ta == 'B' && Tb == '2' && Tc == '4' || T == 'd' || T == 'b')
    
        P = DB24;
    else
       {  printf("Wrong Component Type\n");
          exit(0);
       }
    
    return (P);
         }
    
    
    
    int getQuantity(int Q, int RQ)
    {
    if (Q > 0)
    RQ = Q;
    
    else
    { printf("Wrong Quantity\n");
    exit(0);
    }
    return (RQ);
    }
    
    float calcPrice(float Price, int ReturnQ, float DP, float TP)
    {
    TP = Price*ReturnQ;
    
    if (ReturnQ > NINE_COE && ReturnQ <= NINTYNINE_COE)
        DP = TP*FIVE_PERCENT;
    else if (ReturnQ > NINTYNINE_COE)
        DP = TP*TEN_PERCENT;
    else
        DP = TP;
    
    
    return (DP);
    }
    
    float getDeliveryFee(int O, float F)
    {
    if (O == 1)
        F = SAME_DAY;
    else if (O == 2)
        F = NEXT_DAY;
    else
    {  printf("Invalid Option\n");
       exit(0); }
    
    return (F);
    }
    
    float calcTotalCharges(float DiscountP, float Fee)
    {
    return (DiscountP+Fee);
    }

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Quote Originally Posted by Adak View Post
    You could use a do while loop, then if the called functions get bad input and the program needs to terminate, you have them return say, a -1. When your do while loop in main gets this -1 returned from any function, then it quits the loop, and the program terminates with a standard return 0 statement at the end.
    Code:
    int main(void) {
      do {
        result = call your input function
         if result == -1
            continue;       //skips over remaining function calls- break would do the same thing
        result = call your cost computation function
        if result == -1
          continue;
        etc.
      }while (result != -1);
    
      return 0;
    } //end of main
    That kind of logic will work.
    Thx for the reply, I would use your example however we are only allowed to use the material given to us up to a certain point and that dosnt include loop functions how annoying.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Another way (partly mentioned on my post above is)
    Code:
    int byebye;
    int main()
    {
        first();
        second();
        third();
    }
    
    void first()
    {
        ....
       byebye = 1; //if there is an error
    }
    
    void second()
    {
        if (!byebye) return; //so it will execute only if there wasn't an error
       ...
    
    ...
    you will have the scanfs and all the other logic inside functions.
    }

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh my, I just love beginner C code . In all honesty though milsy2000 you are putting a great effort here you're doing good. (no joke)! Keep it up!

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Quote Originally Posted by claudiu View Post
    Oh my, I just love beginner C code . In all honesty though milsy2000 you are putting a great effort here you're doing good. (no joke)! Keep it up!
    Haha thank you for the compliment and also not laughing (too much). This is only my second assignment and i'm sure they will get trickier. I don't need telling that there are a few bugs in there but i'll ask my lecturer for help with those.

    Thx all for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Terminating a GLUT program?
    By glo in forum Game Programming
    Replies: 4
    Last Post: 07-05-2006, 02:37 AM
  4. Terminating a program
    By whisper11 in forum C++ Programming
    Replies: 1
    Last Post: 02-03-2005, 12:59 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM