Click here to Skip to main content
15,176,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to build a car renting system using a classes and functions in c++,
but i have completed almost half of the things here when i am trying to run my object with the logic who is setting the basevalue of car i am getting an error
C++
<pre>#include<iostream>
#include<string>  //using string for name and models
using namespace std;

class rent  //rent class
{
 private:
  string car;        //name of car  
  int age;           //age of the owner
  float basevalue;   //basevalue of car
  string owner;      //name of the owner
  int otp;           //otp for password 
  string modelno;    //modelno of car
  int timing;        //timing limit for car (in hrs)

public:
 void list();  //list of cars
 int otpgenerator();
 void setvar();
 void takedata(); //taking name and other data of owner 
 void logic(int a,string c,string mn);//proccessing the rent price according to car and time limit

 int display(); //displaying  the further results or end
};

void rent ::list(){
  cout<<"\t\t\t`````````````` LIST OF CARS``````````````````"<<endl;
  cout<<"\t\t\t---------------------------------------------"<<endl;
  cout<<"\t\t\t\t|S.No|  NAME   | MODEL.NO | RATING |"<<endl;
  cout<<"\t\t\t--------------------------------------"<<endl;
  cout<<"\t\t\t\t| 1  | Alto    |  6787ba  | 4.5 |"<<endl;
  cout<<"\t\t\t\t| 2  | Alto    |  8797bk  | 3.6 |"<<endl;
  cout<<"\t\t\t\t| 3  | Tata    |  5665av  | 2.9 |"<<endl;
  cout<<"\t\t\t\t| 4  | Ferrari |  1298kb  | 4.5 |"<<endl;
  cout<<"\t\t\t\t| 5  | Ferrari |  8944kj  | 4.3 |"<<endl;
  cout<<"\t\t\t\t| 6  | Ferrari |  9056gt  | 3.4 |"<<endl;
  cout<<"\t\t\t\t| 7  |  Toyota |  7855kh  | 4.3 |"<<endl;
  cout<<"\t\t\t\t| 8  | Ferrari |  9084jk  | 1.4 |"<<endl;
  cout<<"\t\t\t\t| 9  |   BMW   |  6712az  | 4.4 |"<<endl;
  cout<<"\t\t\t\t| 10 |   BMW   |  6751bh  | 1.1 |"<<endl;
  cout<<"\t\t\t\t| 11 |  Hundai |  1871vk  | 2.5 |"<<endl; 
  cout<<"\t\t\t\t| 12 |  Hundai |  5643kl  | 1.4 |"<<endl;
}

void rent:: takedata(){
   cout<<"\t\t\t\t-----------For Renting Car---------"<<endl;
   cout<<"\t\t\tEnter your Name:"<<endl;
   cin>>owner;
   cout<<"\t\t\tEnter your Age:"<<endl;
   cin>>age;
   cout<<"\t\t\tEnter Car Name:"<<endl;
   cin>>car;
   cout<<"\t\t\tEnter Car Modelno:"<<endl;
   cin>>modelno;
   cout<<"\t\t\tEnter Timelimit for Rent(in hrs):"<<endl;
   cin>>timing;

}

void rent::logic(int a,string c,string mn){
   
   if (a<18)   //checking age of owner
   {
     cout<<"\t\tSorry but you are not mature!!!"<<endl;
     exit(0);
   }
   else  //checking car name and model-no
   {
    if (c=="Alto" && mn=="6787ba")
    {
     basevalue=25000;
    }
    else if(c=="Alto"&& mn=="8797bk"){
    basevalue=20000;
    }
    else if(c=="Ferrari" && mn=="1298kb"){
    basevalue==50000;
    }
    else if(c=="Ferrari" && mn=="8944kj"){
    basevalue==55000;
    }
    else if(c=="Ferrari" && mn=="9056gt"){
    basevalue==40000;
    }
    else if(c=="Ferrari" && mn=="9084jk"){
    basevalue==90000;
    }
    else if(c=="Tata"){
    basevalue==16000;
    }
    else if(c=="Toyota") {
    basevalue==25000;
    }
    else if(c=="BMW"&& mn=="6712az"){
    basevalue==100000;
    }
    else if(c=="BMW"&& mn=="6751bh"){
    basevalue==100000;
    }
    else if(c=="Hundai"&& mn=="1871vk"){
    basevalue==300000;
    }
    else if(c=="Hundai"&& mn=="1871vk"){
    basevalue==90000;
    }
    else 
    {
    cout<<"\t\t The name or the model.no You have put is wrong !!"<<endl;
    exit(0);
    }
    
   }




}
int main()  //main start of program
{
  rent custom;  //customer object

  custom.list();
  label:
  custom.takedata();
  custom.logic(age,car,modelno);


  return 0;
}



i am getting an error of variable age modelno and car was not declared in this scope

What I have tried:

i have tried to set another values but this is not working
Posted
Updated 14-Aug-21 21:28pm

in your main() function this call:

C
custom.logic(/*undeclared*/age, /*undeclared*/car, /*undeclared*/modelno);


Probably should be something like:

C++
custom.logic(21, "Ferrari", "8944kj");
   
v2
To add to what steveb has said, you can think of a function as "teaching the computer to do something" and the parameters as the "bits it needs to do it to".
If you think about a cooking recipe it has two parts: teh ingredients and the method.
The ingredients tells you how much ot each item to use, the method tells you what to do with it.
So if I create a recipe for a Gin and Tonic:
Ingredients:
50ml     Gin
200ml    Tonic water
2 cubes  Ice
1 slice  Lemon

Method:
Put the Gin in the glass.
Add the ice cubes.
Add the Tonic water.
Add the lemon.
You can vary that by making enough for two:
Ingredients:
100ml    Gin
400ml    Tonic water
4 cubes  Ice
2 slices Lemon

Method:
Put the Gin in the glass.
Add the ice cubes.
Add the Tonic water.
Add the lemon.
Or "weaken it":
Ingredients:
25ml     Gin
200ml    Tonic water
2 cubes  Ice
1 slice  Lemon

Method:
Put the Gin in the glass.
Add the ice cubes.
Add the Tonic water.
Add the lemon.
Or make a "VirGin & Tonic":
Ingredients:
0ml      Gin
200ml    Tonic water
2 cubes  Ice
1 slice  Lemon

Method:
Put the Gin in the glass.
Add the ice cubes.
Add the Tonic water.
Add the lemon.
The method doesn't change at all, just the ingredients.
In computing, the "Method" part of the recipe is the function, the Ingredients part is the function parameters:
C++
int MakeGnT(int gin, int tonic, int cubes, int slices)
   {
   return gin + tonic + cubes + slices;
   }
And you create a "Normal" G&T by calling it with the first set of ingredients:
C++
int drink = MakeGnT(50, 200, 2, 1);
Or a weaker one by passing different numbers:
C++
int drink = MakeGnT(25, 200, 2, 1);

Or a V&T:
C++
int drink = MakeGnT(0, 200, 2, 1);

The function itself doesn't change, but the resulting drink does.

You have created your function, but you need to pass it the appropriate values for it to do anything useful.

I'd also suggest that you should change your names: rent should probably be RentalAgreement, logic should probably be something like GetPrice and so on - make the names reflect what they are there for, what they do instead of "generic" names! Similarly, the parameter names need to reflect what they are there for: a is age, c is Manufacturer, mn is ModelNo - that way when you look at the function you know how to use it.

And the whole class should be independent: move the takedata code into the main function so that the class isn't dependant on the environment in which your code is running: main "knows" this is a console app, but the rental agreement doesn't so main can use cin and cout but your new class shouldn't need to know that at all!
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900