problem 22 Problem 22 I am working on this problem 22 from b
problem 2.2
Problem 2.2
I am working on this problem 2.2 from below. When it goes to the console and aske me for the user inputs it is stuck in a loop at here:
OUTPUT BELOW:
Enter reportingMark (5 or less upper case characters): SP
Enter the carNumber: 34567
Enter the kind: box
Enter the load (only true or false): true
Enter the load (only true or false): true
Enter the load (only true or false): true
Enter the load (only true or false):
When i enter true it should prompt me for the destination or if i enter false it should print NONE the console.
Introduction to structures
Structures to classes
Introduction to classes
Object oriented design
Use the same format for problem and function headings as lab 1.
Problem 2.1
Declare a structure with a type name: Car
containing:
reportingMark a string of 5 or less upper case characters
carNumber an int
kind could be box tank flat or other
loaded a bool
destination a string with a destination or the word NONE
Note: A destination is required if the car is loaded. If it is not
loaded the destination may be either a destination or the word NONE.
Create the following functions:
main
* Uses new to obtain space for the data structure
* Calls the other two functions
* Deletes the space obtained using new
input
* Read all the data from the user
* After all the data has been read, put all this data into the structure
output
* Print the data in a neat format
Put the main function first.
Use the function names and field names specified above.
Arrange the functions in the order listed above.
Test your program with the following data:
reportingMark SP
carNumber 34567
kind box
loaded true
destination Salt Lake City
------------------------------------------------------------------------------------------------------------------------------------------
Problem 2.2
Repeat problem 2.1 with the following changes:
Use class rather than struct with the type name: Car
Make the data in the class Car public.
Revise the input so the input function will only read the data from the
user, and then it will call an additional function named setUpCar which
will put the data in the object.
THIS IS WHAT I HAVE BELOW:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class Car
{
public:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
Car ()
{
reportingMark = \"none\";
carNumber = 0;
kind = \"none\";
loaded = false;
destination = \"none\";
}
Car (string r, int c , string k, bool l, string d)
{
reportingMark = r;
carNumber = c;
kind = k;
loaded = l;
destination = d;
}
~Car()
{};
void setreportingMark (string r)
{
reportingMark = r;
}
void setCarNumber (int c)
{
carNumber = c ;
}
void setkind (string k)
{
kind = k;
}
void setloaded (bool l)
{
loaded = l;
}
void setdestination (string d)
{
destination = d;
}
string getreportingMark () const
{
return reportingMark;
}
int getCarNumber () const
{
return carNumber;
}
string getkind () const
{
return kind;
}
bool getloaded () const
{
return loaded;
}
string getdestination () const
{
return destination;
}
/* ******************** setUpCar ********************
creates an object using a constructor that takes the reference parameters
pointer to the object c1
*/
Car *setUpCar (string r, int c, string k, bool l, string d)
{
Car * c1 = new Car (r, c, k, l, d);
return c1;
}
};
void input ();
void output (Car* setUpCar);
Car *cPtr;
//main
int main ()
{
input ();
output (cPtr);
delete cPtr;
return 0;
}
/* ********** Input ************
Reads the data from the user, than calls an additional function named SetUpCar which
will put the data in the object.
*/
void input()
{
string r;
int c;
string k;
bool l;
string d;
int len;
string choice;
cout<< \"Enter reportingMark (5 or less upper case characters): \";
getline(cin, r);
len= r.length();
while (len > 5)
{
cout<< \"INVALID! Enter reportingMark with 5 or less upper case characters: \";
getline(cin, r);
len= r.length();
}
cout<< \"Enter the carNumber: \";
cin>> c;
cout << \"Enter the kind: \";
cin>> k;
do
{
cout<<\"Enter the load (only true or false): \";
cin>>choice;
if (choice == \"true\")
{
l = true;
}
else if (choice == \"false\")
{
l = false;
}
else
{
cout<<\"error!\"<<endl;
}
}while (choice == \"true\" || choice == \"false\");
if(l == true)
{
cout<<\"Enter the destination: \";
cin.ignore();
getline(cin,d);
}
if(l == false)
{
d = \"NONE\";
}
cPtr = cPtr -> setUpCar (r, c, k, l, d);
}
/* ********** Output ************
Prints Data from the car object in a neat format.
*/
void output(Car* setUpCar)
{
cout<<\"\ reportingMark:\\t\\t\" <<setUpCar->reportingMark;
cout<<\"\ carNumber:\\t\\t\"<<setUpCar->carNumber;
cout<<\"\ Kind:\\t\\t\\t\"<<setUpCar->kind;
cout<<\"\ Loaded:\\t\\t\\t\"<<setUpCar->loaded ? \"true\" : \"false\";
cout<<\"\ Destination:\\t\\t\"<<setUpCar->destination <<endl;
}
Solution
I just ran through your provided code.
I think the problem is the the do-while loop that yoy have used.
do
{
cout<<\"Enter the load (only true or false): \";
cin>>choice;
if (choice == \"true\")
{
l = true;
}
else if (choice == \"false\")
{
l = false;
}
else
{
cout<<\"error!\"<<endl;
}
}while (choice == \"true\" || choice == \"false\");
if(l == true)
{
cout<<\"Enter the destination: \";
cin.ignore();
getline(cin,d);
}
if(l == false)
{
d = \"NONE\";
}
Now what is happening over here is, since it is do-while, the do block will get executed without checking the while condition.After do block is done then in checks the while condition. So if the while condition is true then do block continues to execute.
Now looking at your while condition,it says if the value is true or false,continue the loop,until you give other than \"true\" or \"false\". See here the user input has to be \"true\" or \"false\",if it is \"True\" or \"False\" while condition will terminate.
So,when user is giving true or false as input, the while condition satisfies, and continues the loop.Thats the issue over here.
Solution for this is to use If-else decision making loop.
// take the input from thr user
cout<<\"Enter the load (only true or false): \";
cin>>choice;
// here if the input is true or false then only continue.
if (choice == \"true\" || choice == \"false\")
{
if (choice == \"true\")
{
l = true;
}
else if (choice == \"false\")
{
l = false;
}
else
{
cout<<\"error!\"<<endl;
}
}
else
{
cout<<\"error!\"<<endl;
}
Here the whole code,only that portion i have changed. I have executed it, it is runnig now, i am giving the output too at the end of this.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class Car
{
public:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
Car ()
{
reportingMark = \"none\";
carNumber = 0;
kind = \"none\";
loaded = false;
destination = \"none\";
}
Car (string r, int c , string k, bool l, string d)
{
reportingMark = r;
carNumber = c;
kind = k;
loaded = l;
destination = d;
}
~Car()
{};
void setreportingMark (string r)
{
reportingMark = r;
}
void setCarNumber (int c)
{
carNumber = c ;
}
void setkind (string k)
{
kind = k;
}
void setloaded (bool l)
{
loaded = l;
}
void setdestination (string d)
{
destination = d;
}
string getreportingMark () const
{
return reportingMark;
}
int getCarNumber () const
{
return carNumber;
}
string getkind () const
{
return kind;
}
bool getloaded () const
{
return loaded;
}
string getdestination () const
{
return destination;
}
/* ******************** setUpCar ********************
creates an object using a constructor that takes the reference parameters
pointer to the object c1
*/
Car *setUpCar (string r, int c, string k, bool l, string d)
{
Car * c1 = new Car (r, c, k, l, d);
return c1;
}
};
void input ();
void output (Car* setUpCar);
Car *cPtr;
//main
int main ()
{
input ();
output (cPtr);
delete cPtr;
return 0;
}
/* ********** Input ************
Reads the data from the user, than calls an additional function named SetUpCar which
will put the data in the object.
*/
void input()
{
string r;
int c;
string k;
bool l;
string d;
int len;
string choice;
cout<< \"Enter reportingMark (5 or less upper case characters): \";
getline(cin, r);
len= r.length();
while (len > 5)
{
cout<< \"INVALID! Enter reportingMark with 5 or less upper case characters: \";
getline(cin, r);
len= r.length();
}
cout<< \"Enter the carNumber: \";
cin>> c;
cout << \"Enter the kind: \";
cin>> k;
cout<<\"Enter the load (only true or false): \";
cin>>choice;
if(choice == \"true\" || choice == \"false\")
{
if (choice == \"true\")
{
l = true;
}
else if (choice == \"false\")
{
l = false;
}
else
{
cout<<\"error!\"<<endl;
}
}
else
{
cout<<\"Invalid input(enter true or false) only\"<<endl;
}
if(l == true)
{
cout<<\"Enter the destination: \";
cin.ignore();
getline(cin,d);
}
if(l == false)
{
d = \"NONE\";
}
cPtr = cPtr -> setUpCar (r, c, k, l, d);
}
/* ********** Output ************
Prints Data from the car object in a neat format.
*/
void output(Car* setUpCar)
{
cout<<\"\ reportingMark:\\t\\t\" <<setUpCar->reportingMark;
cout<<\"\ carNumber:\\t\\t\"<<setUpCar->carNumber;
cout<<\"\ Kind:\\t\\t\\t\"<<setUpCar->kind;
cout<<\"\ Loaded:\\t\\t\\t\"<<setUpCar->loaded ? \"true\" : \"false\";
cout<<\"\ Destination:\\t\\t\"<<setUpCar->destination <<endl;
}
Output:
Enter reportingMark (5 or less upper case characters):SP
Enter the carNumber: 34567
Enter the kind: box
Enter the load (only true or false): true
Enter the destination:
reportingMark: SP
carNumber: 34567
Kind: box
Loaded: 1
Destination:
Hope you understood.








