Car Rental Program Help

So my lecturer gave me this assignment:
1. You are employed to develop a program for a car rental company, The program should
keep track of how much money you make per day from rental. Declare a Rental struct
that keeps track of how many cars are rented, the cost per rental, how much you earned
each day, and how many cars are left on the lot (given a total of 100 cars). Read in values
for how many cars are rented, and the cost pre rental. Pass the Rental struct to a function
called printmsg (Rental) that prints each of the values.
given:
money earned = cars rented * amount per rentalÍž
cars on lot = total cars - cars rentedÍž
I'm not sure how to pass the struct to the function correctly because the returning values are totally incorrect. Please if someone could assist me I would be grateful, just starting C++ for the first time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <math.h>

using namespace std;
struct Rental
{
    float rented_cars;
    float rental_cost;
    float daily_earn;
    float lot_cars;
    int total_cars = 100;
};

int main ()
{
    Rental A;
    float printmsg(Rental A);
    cout<<"Enter the number of cars rented"<<endl;
    cin>>A.rented_cars;
    cout<<"What is the cost per rental"<<endl;
    cin>>A.rental_cost;
    cout<<"The money earned each day is: "<<A.daily_earn<<endl;
    cout<<"The number of cars left on the lot is: "<<A.lot_cars<<endl;
}
   float printmsg(Rental B)
    {
     B.daily_earn = B.rented_cars*B.rental_cost;
     B.lot_cars = B.total_cars-B.rented_cars;
     return B.daily_earn;
    }
Hi,

Between line 21 and 22 don't you think you have to call a function or something? How is your daily_earn computed by program? by printmsg() right? call it on your main and see if it solves your problem...
I'm confused, when I put that then I get an error. Where in the main do I put the printmsg function, isn't it already in the main underneath Rental A?
when I put that then I get an error.

how exactly you put that? could you show us your code?

Where in the main do I put the printmsg function, isn't it already in the main underneath Rental A?

yes but the variables are undefined, before line 17 you haven't defined initialized the rented_cars or rental_cost, Its like asking questions on quantum mechanics on your kinder-garden graduation party...
Last edited on
I finally got the code to work but I get a warning at line 11 : non-static data member initializers only available with -std=c++11 or -std=gnu++11 If you could please tell me how to correct it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <math.h>

using namespace std;
struct Rental
{
    float rented_cars;
    float rental_cost;
    float daily_earn;
    int lot_cars;
    int total_cars = 100;

    float printmsg()
    {
    daily_earn = rented_cars*rental_cost;
    lot_cars = total_cars-rented_cars;
    cout<<"The money earned each day is: "<<daily_earn<<endl;
    cout<<"The number of cars left on the lot is: "<<lot_cars<<endl;
    }
};

int main ()
{
   Rental A;
    cout<<"Enter the number of cars rented, then the cost per rental"<<endl;
    cin>>A.rented_cars>>A.rental_cost;
    A.printmsg();
}

Also look at this code it's working and everything, but how do I get it to allow multiple calculations and after the final calculation it would display all the results after entering all of them
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<math.h>

using namespace std;
struct Gradebook
{
    string student_name;
    double grade_1;
    double grade_2;
    double grade_avg;
    string add_ent;

    string gradeclc()
     {
         grade_avg = (grade_1 + grade_2)/2;
         if (grade_avg >=60)
         {
             add_ent = "Pass";
         }
         else if (grade_avg < 60)
         {
             add_ent = "Fail";
         }
     }
    void printmsg()
     {
    gradeclc();
    cout<<"Name\t\tGrade 1\t\tGrade 2\t\tAdd Ent"<<endl;
    cout<<student_name<<"\t\t"<<grade_1<<"\t\t"<<grade_2<<"\t\t"<<add_ent<<endl;
     }
};

int main()
{
    Gradebook A;
    cout<<"Enter the student's name, grade 1 then grade 2\n";
    cin>>A.student_name>>A.grade_1>>A.grade_2;
    A.printmsg();

  }
Last edited on
In member function 'float Rental::printmsg()':
19:5: warning: no return statement in function returning non-void [-Wreturn-type]

This is the warning I am getting when I compile it on cpp.sh with c++11 standard as well as c++14 standard, I am not getting yours


Anyway I believe that your warning comes because you are not using constructor for initializing your values in the struct, use it and see if it still prevails


In your second code

Like in your first code I got the same warning, for line 13 replace this
string gradeclc() with this [code][void gradeclc()/code]

and you get the desired output

Enter the student's name, grade 1 then grade 2
wsdfe
1
3
Name		Grade 1		Grade 2		Add Ent
wsdfe		1		3		Fail
 
Exit code: 0 (normal program termination)

Last edited on
Hey, thanks for the help with the first code. For the second code, it is supposed to ask the user for 3 entries, but I'm not sure how to do that. How do you have multiple inputs for the same name, grade 1 and 2 and let it output all three of each of those all at once in that tabular format? Sorry for pesting but it's my first time doing C++ and it's the only programming I'm doing in my major (Electrical Engineering).
Last edited on
add a double grade_3 to your struct and then cin it in the same place you take the other 2 and also do the obvious changes in your gradecalc() function
I think you misunderstood me, I meant you need 3 entries for each in this case for eg.

Name Grade_1 Grade_2 ADD_ENT
John 10 40 Fail
James 20 50 Fail
Joseph 30 60 Fail
Last edited on
Oh, for that you just need to make the array of struct like Gradebook A[3], and input the data using the lovely for loop
Sorry, again I'm confused, we haven't learnt arrays yet, but I'm assuming we have to use them in this. If you could show me how to add it because when I put Gradebook A[3] there it gives me an error about request for member which is of non class type. Also with arrays can they be used based on the user input or is it you who enter the values and assign them to the array? I want to be able to allow the user to input 3 different names, grades 1 and 2, then allow the function to output the three different pass or fail result.The for loop is also confusing wouldn't it just loop the user to keep entering values rather than outputting all 3 of the values the user enters? Like so:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    Gradebook A;
    int x = 0;
    for(x=0;x<3;x++)
    {
    cout<<"Enter the student's name, grade 1 then grade 2\n";
    cin>>A.student_name>>A.grade_1>>A.grade_2;
    A.printmsg();
    }

  }


If you could please help me correct it or show me where to make adjustments in the code that would be great. I've tried searching online for hours but still can't find anything similar to what I'm asked to do since I must use struct.
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    Gradebook A[3];
    int x = 0;
    for(x=0;x<3;x++)
    {
    cout<<"Enter the student's name, grade 1 then grade 2\n";
    cin>>A[x].student_name>>A[x].grade_1>>A[x].grade_2;
    A[x].printmsg();
    }

  }


further reading: http://www.cplusplus.com/doc/tutorial/arrays/
I don't see how above is any different from the loop I used. I need a way to input the three different sets of variables but store them in the same place and output them together,as in right under each other (not after prompt messages) in that tab form. I could use 9 variables instead of the three but it's tedious so I'm asking for a simpler way if you get me.
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    Gradebook A[3];
    int x = 0;
    for(x=0;x<3;x++)
    {
    cout<<"Enter the student's name, grade 1 then grade 2\n";
    cin>>A[x].student_name>>A[x].grade_1>>A[x].grade_2;
    A[x].printmsg();
    }

  }


I hope you get it now, please read the link I have provided before to completely understand
I'm still not getting what you're driving at. I understood the tutorial on arrays, but what I'm asking for wouldn't it be in the form of say
1
2
3
4
5
6
 
string student_name[3];
x = student_name[0];
cin>>x;
cout<<x;

etc. etc. like I want to be able to output each of the elements in the array of say student_name, grade_1, grade_2 and even add_ent, but it won't allow me to do that, I would have to attach a variable (or a value) in this case to the array element so I can output it. But, the thing is you're asking for the user's input of the array, so the user's 3 inputs for the certain element should be entered in the program. That's the only way I see it happening where I can output all three at once. The code you wrote literally has the same output as my previous one, it still loops it till the third time it has prompt the user. How do I get it to prompt the user three times, but the last time and the last time only it outputs all three of the inputs for each entry?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>//including input output functions of library


using namespace std;//avoid naming conflicts
struct Gradebook //struct;
{//start of struct
    string student_name;// initialising variables
    int grade_1, grade_2;
    double grade_avg;
    string add_ent;

    void gradeclc()//gradeclc function
     {//start of gradeclc
         grade_avg = (grade_1 + grade_2)/2; //grade average calculations
         if (grade_avg>=60)//if statement
         {
             add_ent = "Pass"; // assigning a string to the add_ent
         }
         else if (grade_avg < 60)
         {
             add_ent = "Fail";
         }
     }//end of gradeclc function
    void printmsg()//printmsg function
     {//start of printmsg
    gradeclc();//calling gradeclc function
     //output headings
    cout<<student_name<<"\t\t\t"<<grade_1<<"\t\t"<<grade_2<<"\t\t"<<add_ent<<endl;//output value
     }//end of printmsg function
};//end of struct

int main()
{
    Gradebook A[3];
    cout<<"Enter the student's name, grade 1 then grade 2\n";
    cin>>A[0].student_name>>A[0].grade_1>>A[0].grade_2;
    cout<<"Enter the student's name, grade 1 then grade 2\n";
    cin>>A[1].student_name>>A[1].grade_1>>A[1].grade_2;
    cout<<"Enter the student's name, grade 1 then grade 2\n";
    cin>>A[2].student_name>>A[2].grade_1>>A[2].grade_2;
    cout<<"Student_Name\t\tGrade_1\t\tGrade_2\t\tADD_ENT"<<endl;
    A[0].printmsg();
    A[1].printmsg();
    A[2].printmsg();
}

Idk something similar to this but the loop would end and the headings wouldn't stay there idk if I'm confusing you but you've been helpful so far.

Edit: I got it to work how I'd want it to look, but How do I do that without it continuously looping is my question.
Second edit: I just removed the loop...does the code look fine to you? On a side note...I'm sorry if I've been a slow learner, I have 3 more assignments to do and a major project, is it possible to pm you during the week of my assignments and seek assistance or no?
Last edited on
The code is fine... but think if there are like 70 students in the class then will you do this?
1
2
3
4
5
6
7
cin>>A[0];
cin>>A[1];
cin>>A[2];
cin>>A[3];
....
//goes upto 100
cin>>A[100];


or this

1
2
for (int i=0;i<100;i++)
cin>>A[i];


Both the program do the same thing but the second one is shorter and non-repetitive....

Further Reading: http://www.cplusplus.com/doc/tutorial/control/
check the loops section to understand

PS: As a coder you need to learn to be lazy ;)

PPS: About the 3 more assignments, you may start it on your own and if you get some difficulty we all are here to help, just post your problem and the community will teach you the rest
Many thanks! I don't want to seem needy where I would make 3 different posts and overburden this forum especially if it's just a simple tweak to my code that I needed. But I will take your advice and if I do have any troubles I'll make sure to make a post, hopefully all goes well. Again, you've been a real help.
PS: As a coder you need to learn to be lazy ;)
Truer words have never been spoken.
welcome :)
Topic archived. No new replies allowed.