Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// Project36,a car variable as userdefined datatype.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include<iostream> 

#include<string>
#include<iomanip>
using namespace std;



	char  structurecar()
	{


		char a;
		int b;
		char c;
		int d;
		int e;
		char f;

		cout << "the stored vehicle information is" << '\n';
		cout << "Enter CarName" << '\n';
		cin >> a;
		cout << endl;

		cout << "Enter CarModel " << '\n';
		cin >> b;
		cout << endl;

		cout << "Enter CarVendor " << "\n";
		cin >> c;
		cout << endl;

		cout << "Enter CarHorsePower " << '\n';
		cin >> d;
		cout << endl;

		cout << "Enter CarPrize " << '\n';
		cin >> e;
		cout << endl;

		cout << "Enter CarOwnerId " << '\n';
		cin >> f;
		cout << endl;

		cout << "Values are stored in car variable. " << '\n';
		return true;
	}
#include <iostream>
	 int main() {
		structurecar();
		 return 0;
	 }


What I have tried:

// Project36,a car variable as userdefined datatype.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include<iostream> 

#include<string>
#include<iomanip>
using namespace std;



	char  structurecar()
	{


		char a;
		int b;
		char c;
		int d;
		int e;
		char f;

		cout << "the stored vehicle information is" << '\n';
		cout << "Enter CarName" << '\n';
		cin >> a;
		cout << endl;

		cout << "Enter CarModel " << '\n';
		cin >> b;
		cout << endl;

		cout << "Enter CarVendor " << "\n";
		cin >> c;
		cout << endl;

		cout << "Enter CarHorsePower " << '\n';
		cin >> d;
		cout << endl;

		cout << "Enter CarPrize " << '\n';
		cin >> e;
		cout << endl;

		cout << "Enter CarOwnerId " << '\n';
		cin >> f;
		cout << endl;

		cout << "Values are stored in car variable. " << '\n';
		return true;
	}
#include <iostream>
	 int main() {
		structurecar();
		 return 0;
	 }
Posted
Updated 26-Aug-22 2:59am

Simple: you are reading a single character into a and assuming that it will hold everything. It won't, its a char not a string, so it holds just one character: 'A', 'z', or ';'. If you want more data, you need to allocate space for it!
Try changing your app to use std::string variables instead, or char[100] if you haven't reached those yet.

And it doesn't output anything other than "Values are stored in car variable. " because that is all you have told it to print! And even that isn't true: it's stored in a selection of variables, not a single struct!
 
Share this answer
 
Your code doesn't resemble C++ 'typical' one.
Try
C++
#include<iostream>
using namespace std;


class Car
{
private:
  string m_name, m_model;
  int  m_horsepower;


public:
  Car( string name, string model, /* ... */ int horsepower) : m_name{name}, m_model{model}, m_horsepower{horsepower}
  {
  }

  string details()
  {
    return m_name  + ", " + m_model + ", " + to_string(m_horsepower);
  }
};

int main()
{
  string name, model;
  int horsepower;
  cout << "please enter car details, ";
  cout << "name:";
  cin >> name;
  cout << "model: ";
  cin >> model;
  cout << "horsepower: ";
  cin >> horsepower;
  Car car(name, model, horsepower);

  cout << "car stored details: " << car.details() << "\n";
}
 
Share this answer
 

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



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