Thread: Program stops working when it gets to function

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    Program stops working when it gets to function

    Code:
    #include <stdio.h>#include <conio.h>
    
    
    void Rent(void);
    
    
    float entry(int);
    float discount(int,float);
    int j=0;
    struct rental
    {
        char firstname[20];
        char lastname[20];
        int number[10];
        char address[40];
        int age;
    }rents[40];
    int main()
    {
      int num1;
    
    
      printf("Welcome to Ollivierre's Car Rental Service!\nDo you want to rent a car or are you returning one?\n1.Renting\n2.Returning\n0.Exit\n");
      scanf("%d",&num1);
    
    
    
    
      while (num1!=0)
      {
          switch (num1)
          {
              case 1: Rent();
                    break;
    
    
          }
      }
      printf("Have a nice day!");
    return 0;
    }
    
    
    void Rent()
    {
        int entrance,num2,num3,num4,car;
        float rate,discop;
        FILE *rentPtr;
        rentPtr=fopen("Rental.txt","w");
    
    
        printf("You have chosen to rent a car\n");
        printf("Please Enter your First name: \n");
        scanf("%s",&rents[j].firstname);
        printf("Please Enter your Last name: \n");
        scanf("%s",&rents[j].lastname);
        printf("Please Enter your Address: \n");
        scanf("%s",&rents[j].address);
        printf("Please Enter your Telephone Number: \n");
        scanf("%d",&rents[j].number);
        printf("Please Enter your Age(Must be at least age 20)): \n");
        scanf("%d",&rents[j].age);
        entrance=rents[j].age;
        if (entrance>20)
        {
            printf("What type of vehicle would you like?\nThese are the vehicles available.\n");
            printf("1.Toyota Camry($30)\n2.Mitsubishi lancer($60)\n3.Bugatti veyron($95)\n4.Lamborghini gallardo ($90)\n5.Nissan Altima($80)\n6.mitsubishi galant ($45)\n");
            printf("Which vehicle would you like to rent?\n");
            scanf("%d",num2);
            car=entry(num2);
            printf("The price of your car per day is %0.2f",car);
            printf("How many days will you be renting this car for?: \n");
            scanf("%d",num4);
            rate=car*num4;
            if (num4>10)
            {
               discop=discount(num4,rate);
               printf("You are renting the car for: %d day(s).\nThe price would be: %0.2f\n",num4,discop);
            }
            else
            {
                if (num4<10)
                {
                printf("You are renting the car for: %d day(s).\nThe price would be %0.2f",num4,rate);
                }
            }
    
    
            fprintf(rentPtr,"Firstname\tLastname\tAddress\tNumber\tAge");
            fprintf(rentPtr,"\n");
            fprintf(rentPtr,"%s\t%s\t%s\t%d\t%d",rents[j].firstname,rents[j].lastname,rents[j].address,rents[j].number,rents[j].age);
            fclose(rentPtr);
    
    
        }
        else
        {
            if (entrance<=19)
            {
            printf("You are too young to rent a car!\n");
            }
        }
    
    
        printf("Have a nice day!");
    }
    
    
    float entry(int a)
    {
        float price;
    
    
        if (a==1)
        {
            price=30;
        }
        else
        {
            if (a==2)
            {
                price=60;
            }
            else
            {
                if (a==3)
                {
                    price=95;
                }
                else
                {
                    if (a==4)
                    {
                        price=90;
                    }
                    else
                    {
                        if (a==5)
                        {
                            price=80;
                        }
                        else
                        {
                            if (a==6)
                            {
                                price=45;
                            }
                        }
                    }
                }
            }
        }
    
    
        return price;
    }
    float discount (int b,float c)
    {
        float disco;
        if ((b>10)&&(b<21))
        {
            disco=c-(c*.02);
        }
        else
        {
            if ((b>20)&&(b<41))
            {
                    disco=c-(c*.05);
            }
            else
                {
                    if (b>40)
                    {
                           disco=c-(c*.10);
                    }
                }
        }
        return disco;
    }
    Screenshot by Lightshot Returning showing there cause i took it out to test this

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Step 1 is learn how to indent code consistently.
    If the code is clearly laid out, it's a lot easier for others to read, and easier for you to spot problems.
    Code:
    #include <stdio.h>
    //#include <conio.h>
    
    void Rent(void);
    float entry(int);
    float discount(int, float);
    
    int j = 0;
    
    struct rental {
      char firstname[20];
      char lastname[20];
      int number[10];
      char address[40];
      int age;
    } rents[40];
    
    int main()
    {
      int num1;
      printf("Welcome to Ollivierre's Car Rental Service!\n"
             "Do you want to rent a car or are you returning one?\n"
             "1.Renting\n"
             "2.Returning\n"
             "0.Exit\n");
      scanf("%d", &num1);
    
      while (num1 != 0) {
        switch (num1) {
        case 1:
          Rent();
          break;
        }
      }
      printf("Have a nice day!");
      return 0;
    }
    
    void Rent()
    {
      int entrance, num2, num3, num4, car;
      float rate, discop;
      FILE *rentPtr;
      rentPtr = fopen("Rental.txt", "w");
    
      printf("You have chosen to rent a car\n");
      printf("Please Enter your First name: \n");
      scanf("%s", &rents[j].firstname);
      printf("Please Enter your Last name: \n");
      scanf("%s", &rents[j].lastname);
      printf("Please Enter your Address: \n");
      scanf("%s", &rents[j].address);
      printf("Please Enter your Telephone Number: \n");
      scanf("%d", &rents[j].number);
      printf("Please Enter your Age(Must be at least age 20)): \n");
      scanf("%d", &rents[j].age);
      entrance = rents[j].age;
    
      if (entrance > 20) {
        printf("What type of vehicle would you like?\n"
               "These are the vehicles available.\n");
        printf("1.Toyota Camry($30)\n"
               "2.Mitsubishi lancer($60)\n"
               "3.Bugatti veyron($95)\n"
               "4.Lamborghini gallardo ($90)\n"
               "5.Nissan Altima($80)\n"
               "6.mitsubishi galant ($45)\n");
        printf("Which vehicle would you like to rent?\n");
        scanf("%d", num2);
        car = entry(num2);
        printf("The price of your car per day is %0.2f", car);
        printf("How many days will you be renting this car for?: \n");
        scanf("%d", num4);
        rate = car * num4;
        if (num4 > 10) {
          discop = discount(num4, rate);
          printf("You are renting the car for: %d day(s).\n"
                 "The price would be: %0.2f\n", num4, discop);
        } else {
          if (num4 < 10) {
            printf("You are renting the car for: %d day(s).\n"
            "The price would be %0.2f", num4, rate);
          }
        }
        fprintf(rentPtr, "Firstname\tLastname\tAddress\tNumber\tAge");
        fprintf(rentPtr, "\n");
        fprintf(rentPtr, "%s\t%s\t%s\t%d\t%d", rents[j].firstname, rents[j].lastname, 
                rents[j].address, rents[j].number, rents[j].age);
        fclose(rentPtr);
      } else {
        if (entrance <= 19) {
          printf("You are too young to rent a car!\n");
        }
      }
      printf("Have a nice day!");
    }
    
    float entry(int a)
    {
      float price;
    
      if (a == 1) {
        price = 30;
      } else {
        if (a == 2) {
          price = 60;
        } else {
          if (a == 3) {
            price = 95;
          } else {
            if (a == 4) {
              price = 90;
            } else {
              if (a == 5) {
                price = 80;
              } else {
                if (a == 6) {
                  price = 45;
                }
              }
            }
          }
        }
      }
    
      return price;
    }
    
    float discount(int b, float c)
    {
      float disco;
      if ((b > 10) && (b < 21)) {
        disco = c - (c * .02);
      } else {
        if ((b > 20) && (b < 41)) {
          disco = c - (c * .05);
        } else {
          if (b > 40) {
            disco = c - (c * .10);
          }
        }
      }
      return disco;
    }
    Next, get a compiler that can tell you a lot more about what is wrong with your program before you try to run it.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘Rent’:
    foo.c:48:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]
       scanf("%s", &rents[j].firstname);
             ^
    foo.c:50:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]
       scanf("%s", &rents[j].lastname);
             ^
    foo.c:52:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[40]’ [-Wformat=]
       scanf("%s", &rents[j].address);
             ^
    foo.c:54:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[10]’ [-Wformat=]
       scanf("%d", &rents[j].number);
             ^
    foo.c:69:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
         scanf("%d", num2);
               ^
    foo.c:71:12: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
         printf("The price of your car per day is %0.2f", car);
                ^
    foo.c:73:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
         scanf("%d", num4);
               ^
    foo.c:87:22: warning: format ‘%d’ expects argument of type ‘int’, but argument 6 has type ‘int *’ [-Wformat=]
         fprintf(rentPtr, "%s\t%s\t%s\t%d\t%d", rents[j].firstname, rents[j].lastname, 
                          ^
    foo.c:41:23: warning: unused variable ‘num3’ [-Wunused-variable]
       int entrance, num2, num3, num4, car;
                           ^
    foo.c:69:5: warning: ‘num2’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         scanf("%d", num2);
         ^
    foo.c:73:5: warning: ‘num4’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         scanf("%d", num4);
         ^
    You have a lot of printf / scanf calls where the format string is inconsistent with the parameter available.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Also, you can lay out your if/else if's in "entry" better:
    Code:
        if (a == 1)
            price = 30;
        else if (a == 2)
            price = 60;
        else if (a == 3)
            price = 95;
        else if (a == 4)
            price = 90;
        else if (a == 5)
            price = 80;
        else if (a == 6)
            price = 45;
        else
            //// handle error
    Or:
    Code:
        switch (a) {
        case 1: price = 30; break;
        case 2: price = 60; break;
        case 3: price = 95; break;
        case 4: price = 90; break;
        case 5: price = 80; break;
        case 6: price = 45; break;
        default: //// handle error
        }
    Or even:
    Code:
        int values[] = {30, 60, 95, 90, 80, 45};
        if (a < 1 || a > 6)
            //// handle error
        else
            price = values[a-1];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console stops working when program is running.
    By ZeroesAndOnes in forum C++ Programming
    Replies: 7
    Last Post: 08-13-2016, 02:58 PM
  2. Replies: 2
    Last Post: 06-01-2015, 04:00 AM
  3. Replies: 6
    Last Post: 09-03-2013, 07:32 AM
  4. Program suddenly stops working.
    By KittenAqua in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 05:45 AM
  5. Program stops working as soon as I input value?..
    By darkmagic in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 02:18 AM

Tags for this Thread