C++ program that Create a struct called car that has the following data members (variables): - Color //color of the car - Model //model name of the car - Year //year the car was made - isElectric //whether the car is electric (true) or not (false) - topSpeed //top speed of the car, can be a decimal.
code i have done struct not working properly.
#include <iostream>
using namespace std;
struct Car
{
string color;
string model_number;
int year_model;
bool cariselectric = true;
bool carisnotelectric = false;
}car_info;
int main()
{
cout << "please enter your cars color: " << car_info.color << ;
getline(cin,color);
cout << "please enter your model number: " << car_info.model_number << ;
getline(cin,.model_number);
cout << "please enter your year model: " << car_info.year_model << ;
getline(cin,year_model);
cout << "the car color is " << endl;
cout << "the model number of the car is: " << endl;
cout << "the year number of the car is: " << endl;
cout << "it is electric" << endl;
cout << "the top speed of the car is: ";
}
Code:
#include <iostream>
using namespace std;
//structure declaration
struct Car
{
string Color;
string Model;
int Year;
bool isElectric;
float topSpeed;
}car_info;
int main()
{
//data input from the user
cout <<"Please enter car color: ";
getline(cin,car_info.Color);
cout <<"Please enter model name: ";
getline(cin,car_info.Model);
cout <<"Please enter year model: ";
cin>>car_info.Year;
cout<<"Is the car electric? Enter 1 for 'Yes' and 0 for 'N0':";
cin>>car_info.isElectric;
cout<<"Please enter the top speed of the car: ";
cin>>car_info.topSpeed;
//printing the data
cout<<endl<<"The details of the car are the following:"<<endl;
cout<< "The car color is: " <<car_info.Color<<endl;
cout << "The model number of the car is: " << car_info.Model<<endl;
cout << "The year number of the car is: "<<car_info.Year<<endl;
if(car_info.isElectric==true)
cout << "It is electric" << endl;
else
cout<<"It is not electric"<<endl;
cout << "The top speed of the car is: "<<car_info.topSpeed;
}
Code Snapshots:
Code Output:
:
Get Answers For Free
Most questions answered within 1 hours.