Thread: When I run my program and reach to my function It doesn't work

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

    When I run my program and reach to my function It doesn't work

    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;
    }
    This is my code.. in the rent function when it gets to the type of car and i choose a number.. it's suppose to go into the entry function and do what it's suppose to do but instead it just says that the program has stopped working.. can anyone help? Screenshot by Lightshot The returning is at the start because i took out the function for it to test the rental alone

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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

Similar Threads

  1. Fseek doesn't reach the end of compiled file
    By alexnyde in forum C Programming
    Replies: 10
    Last Post: 12-01-2014, 09:07 AM
  2. My program doesn't work as it should?
    By BatchProgrammer in forum C Programming
    Replies: 3
    Last Post: 04-15-2013, 05:03 AM
  3. why this program doesn't work!!
    By AlSadiq in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2011, 09:00 AM
  4. My first program and it doesn't work.
    By Magneto in forum C++ Programming
    Replies: 16
    Last Post: 07-16-2005, 02:13 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread