USING C++ : JUST SOLVE PROBLEM (B) ..Based on problem (A): ---------------------
ID: 3823745 • Letter: U
Question
USING C++ : JUST SOLVE PROBLEM (B) ..Based on problem (A):
----------------------------------------------------------------------------------------------------------------------------------------------------------------
USING C++: SOLVE PROBLEM (B).. this problem is based on problem A.
Problem A: People and vehicles (20 points) For this part you will be keeping information on people and what cars they are driving. Your program should have the following options: p create a new person in the database (you may assume this person's name is only one word) c create a new car in the database (you may assume the car's name will be only one word) f find and select a person in the database (if there is no such person, show "no one") unregistered to any current cars) You may assume there will be no more than 100 cars in the database and no more than 100 people. If you try and find (and select) a person that doesn't exist, show "no one" for both the person and car. If a person is not registered to a car, show "no one" for the car. You may assume all people have different names and all cars have different names. However, a car and person might have the same name (see example 2) Each person can only be registered to a single car, but a car can have multiple people registered to it simultaneously (see example 30 Hint 1: use classes (and don't dump everything in a single function) Hint 2: use pointers (really!) or you might regret it when part B comes around Example 1 (user input underlined): Currently looking at Person No one In car No one Do you want to (p) make a new person, (c) make a new car, (f) find a person, (r) register a person to a car or (q) quit What is this person name? insh Currently looking at: Person No one In car No one Do you want to (p) make a new person (c) make a new car, (f) find a person, (r) register a person to a car or (q) quit What is this car s name? putput Currently looking at: Person No one In Car No one Do you want to (p) make a new person, (c) make a new car (f) find a person, (r) register a person to a car or (q) quit Which person do you want to find? inshuaExplanation / Answer
Part A:
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
//Class PersonCar definition
class PersonCar
{
//Data member to store Persons
string Person[100];
//Data member to store Cars
string Car[100];
//Data member to store Registered Cars
int RegisterCar[100];
public:
//Member functions
//Constructor
PersonCar();
//To Return menu option choosen
char Menu();
//Create a person
void CreatePerson();
//Create a car
void CreateCar();
//Register a car
void Register(int);
//Displays current status
void CurrentStatus(int);
//Find a person
int Find();
};//End of class
//Create a person
void PersonCar::CreatePerson()
{
string name;
int flag = 0;
//Accept name of the person
cout<<" What is this Person's name? ";
cin>>name;
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If person x position is not null
if(Person[x] != "")
{
//Checks duplicate name
//If the person x position name is equal to the name entered
if(Person[x] == name)
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of inner if
}//End of outer if
}//End of for loop
//If flag is zero no duplicate name
if(flag == 0)
{
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If person x position is null
if(Person[x] == "")
{
//Store name in person's x position
Person[x] = name;
break;
}//End of inner if
}//End of for loop
}//End of outer if
//Error message
else
cout<<" Person name already exist!";
}//End of function
//Creates a car
void PersonCar::CreateCar()
{
string name;
int flag = 0;
//Accepts car name
cout<<" What is this Cars's name? ";
cin>>name;
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If car x position is not null
if(Car[x] != "")
{
//Checks duplicate car name
//If the car x position name is equal to the car name entered
if(Car[x] == name)
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of inner if
}//End of outer if
}//End of if
//NO duplicate
if(flag == 0)
{
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If car x position is null
if(Car[x] == "")
{
//Store name in cars's x position
Car[x] = name;
break;
}//End of inner if
}//End of for loop
}//End of outer if
//Displays error
else
cout<<" Car name already exist!";
}//End of function
//Register a car based on the person index
void PersonCar::Register(int na)
{
string car;
int flag = 0, carInd = -1, x;
//Accepts car name
cout<<" Which car do you wish to register this person to? ";
cin>>car;
//Loops 100 times
for(x = 0; x < 100; x++)
{
//If entered car name is equal to Car x position name
if(car == Car[x])
{
//Set the carInd to x value
carInd = x;
//Come out of the loop
break;
}//End of if
}//End of for loop
//Store the carInd value in the RegisterCar na position
//na is the person index position
RegisterCar[na] = carInd;
}//End of function
//Find a person and return the index
int PersonCar::Find()
{
string name;
int flag = 0, x;
//Enter the person name
cout<<" Which person do you want to find?";
cin>>name;
//Loops 100 times
for(x = 0; x < 100; x++)
{
//If the person's x position name is equal to entered name
if(Person[x] == name)
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of for loop
//If flag is 1 then name found
if(flag == 1)
//Return the index position
return x;
//Otherwise return -1
else
return -1;
}//End of function
//Displays the current status
void PersonCar::CurrentStatus(int x)
{
//If x value is -1 no person found
if(x == -1)
{
cout<<" Person: No one";
cout<<" In car: No one";
}//End of if
//Otherwise person found
else
{
//Display the person name
cout<<" Person: "<<Person[x];
//If register car x position is not -1 then car is registered
if(RegisterCar[x] != -1)
//Display the car name
cout<<" In car: "<<Car[RegisterCar[x]];
//Otherwise car is not registered
else
cout<<" In car: No one";
}//End of else
}//End of function
//Displays menu and returns the selection
char PersonCar::Menu()
{
char choice;
int index;
cout<<" Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit";
cin>>choice;
return choice;
}//End of function
//Constructor to initialize the data members
PersonCar :: PersonCar()
{
//Loops 100 times
for(int x = 0; x < 100; x++)
{
Person[x] = "";
Car[x] = "";
RegisterCar[x]= -1;
}
}//End of Constructor
//Main function
int main()
{
//Creates an object
PersonCar pc;
int index = -1;
//Displays the current status
pc.CurrentStatus(index);
//Loops till user choice
do
{
//Calls the menu
switch(pc.Menu())
{
case 'P': case 'p':
pc.CreatePerson();
pc.CurrentStatus(index);
break;
case 'C': case 'c':
pc.CreateCar();
pc.CurrentStatus(index);
break;
case 'F': case 'f':
index = pc.Find();
cout<<" index = "<<index;
pc.CurrentStatus(index);
break;
case 'R': case 'r':
pc.Register(index);
pc.CurrentStatus(index);
break;
case 'Q': case 'q':
exit(0);
default:
cout<<" Invalid choice";
}//End of switch
}while(1);//End of do - while
}//End of function
Output:
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit p
What is this Person's name? aa
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit p
What is this Person's name? ss
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit c
What is this Cars's name? qq
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit c
What is this Cars's name? ww
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit f
Which person do you want to find?ss
Person: ss
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit r
Which car do you wish to register this person to? qq
Person: ss
In car: qq
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit r
Which car do you wish to register this person to? ss
Person: ss
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit r
Which car do you wish to register this person to? ww
Person: ss
In car: ww
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit f
Which person do you want to find? aa
Person: aa
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit r
Which car do you wish to register this person to? qq
Person: aa
In car: qq
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit q
Part B:
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
//Class PersonCar definition
class PersonCar
{
//Data member to store Persons
string Person[100];
//Data member to store Cars
string Car[100];
//Data member to store Registered Cars
int RegisterCar[100];
//Person Link
int PersonLink[100];
//Person index to link
int secondInd, firstInd;
public:
//Member functions
//Constructor
PersonCar();
//To Return menu option chosen
char Menu();
//Create a person
void CreatePerson();
//Create a car
void CreateCar();
//Register a car
void Register(int);
//Displays current status
void CurrentStatus(int);
//Find a person
int Find();
//Rename a car
void RenameCar();
//Link two persons
void linkPerson();
};//End of class
//Link two persons
void PersonCar::linkPerson()
{
//To store first person name and second person name
string first, second;
//Accept the first person name
cout<<" Enter the who you want to tag along: ";
cin>>first;
//Accept the second person name
cout<<" Who do you want this person to follow? ";
cin>>second;
//Initialize to -1
secondInd = firstInd = -1;
//Loops 100 times
for(int x = 0; x < 100; x++)
{
//Search for second person name
if(Person[x] == second)
{
//Stores its index position
secondInd = x;
}//End of if
//Search for second person name
if(Person[x] == first)
{
//Stores its index position
firstInd = x;
}//End of if
}//End of loop
}//End of function
//Function to rename a car
void PersonCar::RenameCar()
{
//To store old car name and new car name
string oldName, newName;
//Accepts the old car name
cout<<" What car do you wish to rename?";
cin>>oldName;
//Accepts the new car name
cout<<" Enter the new name: ";
cin>>newName;
//Loops till 100
for(int x = 0; x < 100; x++)
{
//Search for the old car name
if(Car[x] == oldName)
{
//Rename it with the new car name
Car[x] = newName;
break;
}//End of if
}//End of for loop
}//End of function
//Create a person
void PersonCar::CreatePerson()
{
//To store person name entered by the user
string name;
int flag = 0;
//Accept name of the person
cout<<" What is this Person's name? ";
cin>>name;
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If person x position is not null
if(Person[x] != "")
{
//Checks duplicate name
//If the person x position name is equal to the name entered
if(Person[x] == name)
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of inner if
}//End of outer if
}//End of for loop
//If flag is zero no duplicate name
if(flag == 0)
{
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If person x position is null
if(Person[x] == "")
{
//Store name in person's x position
Person[x] = name;
break;
}//End of inner if
}//End of for loop
}//End of outer if
//Error message
else
cout<<" Person name already exist!";
}//End of function
//Creates a car
void PersonCar::CreateCar()
{
string name;
int flag = 0;
//Accepts car name
cout<<" What is this Cars's name? ";
cin>>name;
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If car x position is not null
if(Car[x] != "")
{
//Checks duplicate car name
//If the car x position name is equal to the car name entered
if(Car[x] == name)
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of inner if
}//End of outer if
}//End of if
//NO duplicate
if(flag == 0)
{
//Loops till 100
for(int x = 0; x < 100; x++)
{
//If car x position is null
if(Car[x] == "")
{
//Store name in cars's x position
Car[x] = name;
//Come out of the loop
break;
}//End of inner if
}//End of for loop
}//End of outer if
//Displays error
else
cout<<" Car name already exist!";
}//End of function
//Register a car based on the person index
void PersonCar::Register(int na)
{
//To store car name
string car;
int flag = 0, carInd = -1, x;
//Accepts car name
cout<<" Which car do you wish to register this person to? ";
cin>>car;
//Loops 100 times
for(x = 0; x < 100; x++)
{
//If entered car name is equal to Car x position name
if(car == Car[x])
{
//Set the carInd to x value
carInd = x;
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of for loop
//Store the carInd value in the RegisterCar na position
//na is the person index position
//If flag flag is one then car found
if(flag == 1)
RegisterCar[na] = carInd;
//If firstInd and secondInd is not -1 then they are linked assign same car to both of then
if(firstInd != -1 && secondInd != -1)
{
//Assign same car index to both the persons
//firstInd and secondInd are person index
RegisterCar[firstInd] = carInd;
RegisterCar[secondInd] = carInd;
}//End of if
//Reset to -1
firstInd = secondInd = -1;
}//End of function
//Find a person and return the index
int PersonCar::Find()
{
string name;
int flag = 0, x;
//Enter the person name
cout<<" Which person do you want to find?";
cin>>name;
//Loops 100 times
for(x = 0; x < 100; x++)
{
//If the person's x position name is equal to entered name
if(Person[x] == name)
{
//Set the flag to 1
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of for loop
//If flag is 1 then name found
if(flag == 1)
//Return the index position
return x;
//Otherwise return -1
else
return -1;
}//End of function
//Displays the current status
void PersonCar::CurrentStatus(int x)
{
//If x value is -1 no person found
if(x == -1)
{
cout<<" Person: No one";
cout<<" In car: No one";
}//End of if
//Otherwise person found
else
{
//Display the person name
cout<<" Person: "<<Person[x];
//If register car x position is not -1 then car is registered
if(RegisterCar[x] != -1)
//Display the car name
cout<<" In car: "<<Car[RegisterCar[x]];
//Otherwise car is not registered
else
cout<<" In car: No one";
}//End of else
}//End of function
//Displays menu and returns the selection
char PersonCar::Menu()
{
char choice;
int index;
cout<<" Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit"
<<"... you can also: (E) Rename a car or (L) Link one person's car to another. ";
cin>>choice;
return choice;
}//End of function
//Constructor to initialize the data members
PersonCar :: PersonCar()
{
//Loops 100 times
for(int x = 0; x < 100; x++)
{
Person[x] = "";
Car[x] = "";
RegisterCar[x]= -1;
firstInd = secondInd = -1;
}
}//End of Constructor
//Main function
int main()
{
//Creates an object
PersonCar pc;
int index = -1;
//Displays the current status
pc.CurrentStatus(index);
//Loops till user choice
do
{
//Calls the menu
switch(pc.Menu())
{
case 'P': case 'p':
pc.CreatePerson();
pc.CurrentStatus(index);
break;
case 'C': case 'c':
pc.CreateCar();
pc.CurrentStatus(index);
break;
case 'F': case 'f':
index = pc.Find();
pc.CurrentStatus(index);
break;
case 'R': case 'r':
pc.Register(index);
pc.CurrentStatus(index);
break;
case 'E': case 'e':
pc.RenameCar();
pc.CurrentStatus(index);
break;
case 'L': case 'l':
pc.linkPerson();
pc.CurrentStatus(index);
break;
case 'Q': case 'q':
exit(0);
default:
cout<<" Invalid choice";
}//End of switch
}while(1);//End of do - while
}//End of function
Output:
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. p
What is this Person's name? aaa
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. P
What is this Person's name? sss
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. p
What is this Person's name? ddd
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. c
What is this Cars's name? zzz
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. c
What is this Cars's name? xxx
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. L
Enter the who you want to tag along: aaa
Who do you want this person to follow? sss
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. R
Which car do you wish to register this person to? zzz
first = 0 second = 1
Person: No one
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. F
Which person do you want to find?aaa
Person: aaa
In car: zzz
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. f
Which person do you want to find?sss
Person: sss
In car: zzz
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. f
Which person do you want to find?ddd
Person: ddd
In car: No one
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. R
Which car do you wish to register this person to? xxx
Person: ddd
In car: xxx
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. f
Which person do you want to find?aaa
Person: aaa
In car: zzz
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. F
Which person do you want to find?sss
Person: sss
In car: zzz
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. e
What car do you wish to rename?xxx
Enter the new name: ttt
Person: sss
In car: zzz
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. f
Which person do you want to find?ddd
Person: ddd
In car: ttt
Do you want to (P) Make a new Person, (C) Make a new Car, (F) Find a person, (R) Register a Person to a car, or (Q) quit... you can also: (E) Rename a car or (L) Link one person's car to another. Q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.