Thread: Please Help! I am new at this i need your help

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    1

    Please Help! I am new at this i need your help

    This is my code and i want to repeat my code until the user wants to exit.
    --
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    again();
    }
    
    
    first()
    {
    char arjun;
    
    
    printf("--------------------------------------------------\n");
    printf("\n\tWould you like again to rent one?:\t\n");
    printf("\t\tA for YES\n");
    printf("\t\tB for NO\n");
    printf("Select one:\t");
    scanf("%s", &arjun);
    
    
    
    
    if(arjun == 'B')
    {
    printf("\t\tThank You! Have a Nice Day!");
    printf("--------------------------------------------------\n");
    }
    else if(arjun == 'A')
    {
    printf("\n");
    again();
    }
    
    
    
    
    }
    
    
    again()
    {
    char rent, choose, select;
    float old = 500;
    float medium = 1000;
    float mega = 2000;
    float luxury = 5000;
    float stars = 10000;
    float paid;
    float hours;
    
    
    
    
    
    
    printf("\n\t\t---**Boat For Rent**--\n");
    printf("[1]\tOld Boat\tP500\tper hour\n");
    printf("[2]\tMedium Boat\tP1,000\tper hour\n");
    printf("[3]\tMega Boat\tP2,000\tper hour\n");
    printf("[4]\tLuxury Boat\tP5,000\tper hour\n");
    printf("[5]\t5 Stars Boat\tP10,000\tper hour\n\n");
    
    
    printf("--------------------------------------------------\n");
    printf("Would you like to rent one?:\t\n");
    printf("\t(Y) for YES\n");
    printf("\t(N) for NO\n");
    printf("Choose one:\t");
    scanf("%c", &rent);
    
    
    if(rent == 'Y')
    {
    printf("What particular boat would you like to rent?:\t");
    scanf("%s", &choose);
    
    
    }
    else if(rent == 'N')
    {
    printf("\tThank You! Have a Good Day!");
    }
    switch(choose)
    {
    case '1':
    printf("--------------------------------------------------\n");
    printf("What kind of Transaction?:\t\n");
    printf("[C]cash\n");
    printf("[H]hours\n");
    printf("Choose One please:\t");
    scanf("%s", &select);
    
    
    if(select == 'C')
    {
    printf("--------------------------------------------------\n");
    printf("Enter Your Money:\t");
    scanf("%f", &paid);
    hours = paid / old;
    printf("You only have %.2f hour(s)\n", hours);
    first();
    }
    else if(select == 'H')
    {
    printf("--------------------------------------------------\n");
    printf("For how many hours would you like to use?:\t");
    scanf("%f", &hours);
    paid = hours * old;
    printf("Your Total Payment is %.2f\n", paid);
    first();
    }
    break;
    
    
    case '2':
    printf("--------------------------------------------------\n");
    printf("What kind of Transaction?:\t\n");
    printf("[C]cash\n");
    printf("[H]hours\n");
    printf("Choose One please:\t");
    scanf("%s", &select);
    
    
    if(select == 'C')
    {
    printf("--------------------------------------------------\n");
    printf("Enter Your Money:\t");
    scanf("%f", &paid);
    hours = paid / medium;
    printf("You only have %.2f hour(s)\n", hours);
    first();
    }
    else if(select == 'H')
    {
    printf("--------------------------------------------------\n");
    printf("For how many hours would you like to use?:\t");
    scanf("%f", &hours);
    paid = hours * medium;
    printf("Your Total Payment is %.2f\n", paid);
    first();
    }
    break;
    
    
    case '3':
    printf("--------------------------------------------------\n");
    printf("What kind of Transaction?:\t\n");
    printf("[C]cash\n");
    printf("[H]hours\n");
    printf("Choose One please:\t");
    scanf("%s", &select);
    
    
    if(select == 'C')
    {
    printf("--------------------------------------------------\n");
    printf("Enter Your Money:\t");
    scanf("%f", &paid);
    hours = paid / mega;
    printf("You only have %.2f hour(s)\n", hours);
    first();
    }
    else if(select == 'H')
    {
    printf("--------------------------------------------------\n");
    printf("For how many hours would you like to use?:\t");
    scanf("%f", &hours);
    paid = hours * mega;
    printf("Your Total Payment is %.2f\n", paid);
    first();
    }
    break;
    case '4':
    printf("--------------------------------------------------\n");
    printf("What kind of Transaction?:\t\n");
    printf("[C]cash\n");
    printf("[H]hours\n");
    printf("Choose One please:\t");
    scanf("%s", &select);
    
    
    if(select == 'C')
    {
    printf("--------------------------------------------------\n");
    printf("Enter Your Money:\t");
    scanf("%f", &paid);
    hours = paid / luxury;
    printf("You only have %.2f hour(s)\n", hours);
    first();
    }
    else if(select == 'H')
    {
    printf("--------------------------------------------------\n");
    printf("For how many hours would you like to use?:\t");
    scanf("%f", &hours);
    paid = hours * luxury;
    printf("Your Total Payment is %.2f\n", paid);
    first();
    }
    break;
    
    
    case '5':
    printf("--------------------------------------------------\n");
    printf("What kind of Transaction?:\t\n");
    printf("[C]cash\n");
    printf("[H]hours\n");
    printf("Choose One please:\t");
    scanf("%s", &select);
    
    
    if(select == 'C')
    {
    printf("--------------------------------------------------\n");
    printf("Enter Your Money:\t");
    scanf("%f", &paid);
    hours = paid / stars;
    printf("You only have %.2f hour(s)\n", hours);
    first();
    }
    else if(select == 'H')
    {
    printf("--------------------------------------------------\n");
    printf("For how many hours would you like to use?:\t");
    scanf("%f", &hours);
    paid = hours * stars;
    printf("Your Total Payment is %.2f\n", paid);
    first();
    }
    break;
    }
    
    
    return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Instead of calling again, or first, write your transaction program as if it ran once. Then insert it into a loop such as this.

    Code:
    #include <ctype.h>
    
    char again;
    
    do {
       /*
         single transaction program here
        */
       printf("Do you want to process another transaction? y/N?\n");
       scanf(" %c", &again);
       again = tolower(again);
    } 
    while (again == 'y');

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first step would be to indent your code.
    Indent style - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Tags for this Thread