I am working on a program that keeps inventory of vehicles, so I created a struct for it. It also needs to keep a list of the drivers, so I created a nested struct for that. Here's the code:
struct Vehicle {
string License;
string Place;
int Capacity;
struct Driver {
string Name;
int Code;
int Id;
} dude;
};
I ask for user input and then put the structs in a vector using this function:
void AddVehicle(vector<Vehicle> &vtnewV) {
Vehicle newV;
Vehicle::Driver dude;
cout << "Enter license plate number: " << endl;
cin >> newV.License;
cout << "Enter the vehicle's ubication: " << endl;
cin >> newV.Place;
cout << "Enter the vehicle's capacity: " << endl;
cin >> newV.Capacity;
cout << "Enter the driver's name: " << endl;
cin >> dude.Name;
cout << "Enter the driver's code: " << endl;
cin >> dude.Code;
cout << "Enter the driver's identification number: " << endl;
cin >> dude.Id;
vtnewV.push_back(newV);
};
Now, I need to know if there's a way to add the driver on another function, like, you ask for the vehicle info on one function and then ask for the driver's info on another. The user enters the license plate, for example, and the driver is added in the struct that has that license plate. I don't know if I'm explaining myself. So, that's it, if you can help me, I would really appreciate it.
Example of your own code:
#include <iostream>
#include <string>
#include <vector>
#include <limits>
struct Vehicle {
std::string License;
std::string Place;
int Capacity;
struct Driver {
std::string Name;
int Code;
int Id;
}dude;
};
void AddVehicle(std::vector<Vehicle> &vtnewV)
{
Vehicle newV;
std::cout << "Enter license plate number: " << std::endl;
std::cin >> newV.License;
std::cout << "Enter the vehicle's ubication: " << std::endl;
std::cin >> newV.Place;
std::cout << "Enter the vehicle's capacity: " << std::endl;
std::cin >> newV.Capacity;
std::cout << "Enter the driver's name: " << std::endl;
std::cin >> newV.dude.Name;
std::cout << "Enter the driver's code: " << std::endl;
std::cin >> newV.dude.Code;
std::cout << "Enter the driver's identification number: " << std::endl;
std::cin >> newV.dude.Id;
vtnewV.push_back(newV);
};
int main()
{
std::vector<Vehicle> listVehicle;
AddVehicle(listVehicle);
AddVehicle(listVehicle);
for (auto& i : listVehicle)
{
std::cout << i.dude.Name << " got crabs" << std::endl;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
Now, I need to know if there's a way to add the driver on another
function, like, you ask for the vehicle info on one function and then
ask for the driver's info on another.
I don't know if this is what you are after but there is a way better way to solve this than doing it this way, but without changing too much of your code, this will give you a hint:
#include <iostream>
#include <string>
#include <vector>
#include <limits>
struct Vehicle {
std::string License;
std::string Place;
int Capacity;
struct Driver {
std::string Name;
int Code;
int Id;
}driver;
};
Vehicle CreateVehicle()
{
Vehicle vehicle;
std::cout << "Enter license plate number: " << std::endl;
std::cin >> vehicle.License;
std::cout << "Enter the vehicle's ubication: " << std::endl;
std::cin >> vehicle.Place;
std::cout << "Enter the vehicle's capacity: " << std::endl;
std::cin >> vehicle.Capacity;
return vehicle;
};
Vehicle::Driver CreateDriver()
{
Vehicle::Driver driver;
std::cout << "Enter the driver's name: " << std::endl;
std::cin >> driver.Name;
std::cout << "Enter the driver's code: " << std::endl;
std::cin >> driver.Code;
std::cout << "Enter the driver's identification number: " << std::endl;
std::cin >> driver.Id;
return driver;
}
int main()
{
std::vector<Vehicle> listVehicle;
auto vehicle = CreateVehicle();
auto driver = CreateDriver();
vehicle.driver = driver;
listVehicle.push_back(vehicle);
for (auto& i : listVehicle)
{
std::cout << i.driver.Name << " got crabs" << std::endl;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
How to modify a nested struct on a vector?
To set the driver of the N-th item from the vector of Vehicles, you'd use:
Vehicle::Driver driver_dude;
...
...
vtnewV[N-1].dude = driver_dude;
#include <iostream>
#include <vector>
using namespace std;
struct Driver
{
string Name;
int Code;
int Id;
};
struct Vehicle
{
string License;
string Place;
int Capacity;
///from my opinion driver should be declare like this
Driver driver;
};
vector <Vehicle> vtnewV ;
/// the vector need to declare outside the void
/// else it will keep recreate a new vector
void AddVehicle()
{
Vehicle newV;
cout << "Enter license plate number: " << endl;
cin >> newV.License;
cout << "Enter the vehicle's ubication: " << endl;
cin >> newV.Place;
cout << "Enter the vehicle's capacity: " << endl;
cin >> newV.Capacity;
cout << "Enter the driver's name: " << endl;
cin >> newV.driver.Name;
cout << "Enter the driver's code: " << endl;
cin >> newV.driver.Code;
cout << "Enter the driver's identification number: " << endl;
cin >> newV.driver.Id;
vtnewV.push_back(newV);
};
void ShowInfo()
{
for(int i= 0; i<vtnewV.size();i++)
{
cout<<vtnewV[].License<<newV.driver.Id;
///you need to use for loop to cout all that exist in vector info
}
}
int main()
{
AddVehicle();
ShowInfo();
return 0;
}
I have modify and added some comment to your code base by my own opinion
hope that will solve you problem
Related
#include <iostream>
using namespace std;
struct teacher
{
int id;
char name[50];
int salary;
void input(teacher a)
{
cout << "Enter Name : ";
cin >> a.name;
cout << "Enter ID : ";
cin >> a.id;
cout << "Enter Salary : ";
cin >> a.salary;
}
void output(teacher b)
{
cout << "Your Name Is : " << b.name << endl;
cout << "Your ID Is : " << b.id << endl;
cout << "Your Salary Is : " << b.salary;
}
};
int main()
{
teacher t;
t.input(t);
t.output(t);
return 0;
}
Is there any problem? The output is random numbers, don't know what is it.
I tried writing the output function separately, but still same results.
This seems like a weird design, why don't your class methods directly operate on this?
struct teacher
{
int id;
std::string name;
int salary;
void input()
{
cout << "Enter Name : ";
cin >> name;
cout << "Enter ID : ";
cin >> id;
cout << "Enter Salary : ";
cin >> salary;
}
void output() const
{
cout << "Your Name Is : " << name << endl;
cout << "Your ID Is : " << id << endl;
cout << "Your Salary Is : " << salary;
}
};
Then main would look like
int main()
{
teacher t;
t.input();
t.output();
return 0;
}
Also I'd prefer to use std::string instead of char[] when possible.
In input() you modify parameter a which goes out of scope when it reaches the end of the function.
Instead you should modify the class members variables themselves.
void input()
{
cout << "Enter Name : ";
cin >> name;
cout << "Enter ID : ";
cin >> id;
cout << "Enter Salary : ";
cin >> salary;
}
I have a school assignment in which I have to create a Wine Inventory System where the user can add multiple different wines without a finite number I assume.
I need to create vectors but I'm not sure. I don't know what to try.
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
} wine;
void printwine(Wine1 wine);
int main()
{
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
cout << endl;
printwine2(wine2);
cout << endl;
printwine3(wine3);
}
void printwine(Wine1 wine)
{ //data the user typed as output
cout << "Wine1" << endl;
cout << "the name is: " << wine.name << endl;
cout << "it's year is: " << wine.year << endl;;
cout << "its country of creation is: " << wine.place << endl;;
cout << "it's price is: " << wine.price << endl;
}
It should output the name of the wine, the year, it's country, and it's price, for each wine which was added.
A good starting should be using vector of Wine1.
std::vector<Wine1> wineVec;
wineVec.reserve(/*size*/) // reserve the memory if you know the number of wines beforehand
The printwine function should now take std::vector<Wine1>(preferably const-reference as the data is read-only) and iterate through the vector to print the attributes of the Wine1.
Something like:
#include <string>
#include <iostream>
#include <vector>
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
// do printing each: wine.name, wine.year,... so on
}
}
int main()
{
std::vector<Wine1> vecWine;
int wineNumber = 2;
vecWine.reserve(wineNumber);
std::string name, year, place, price;
for (int i = 0; i < wineNumber; ++i)
{
// get the user input for name, year, place, and price
std::cin >> name >> year >> place >> price;
vecWine.emplace_back(Wine1{ name, year, place, price });
}
printwine(vecWine);
}
That said, you should read more about std::vector
to get to know more, how it works.
Also, good to read about, how to overload operator>> and operator<<, so that you could even write code, much simpler.
Following is an incomplete code, which I leave you to complete after covering the topics which I mentioned.
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
std::cout << wine << '\n';
}
}
int main()
{
std::vector<Wine1> vecWine(wineNumber);
for (Wine1& wine : vecWine)
{
std::cin >> wine;
}
printwine(vecWine);
}
I believe there is a misunderstanding: you do not intend your struct Wine1 to contain a vector but instead, you want a vector of Wine1's.
I suggest a data structure similar to the following:
struct Wine {
string name;
string year;
string place;
string price;
};
void printwinelist(vector<Wine>& list){
for(Wine& w : list){
printwine(w);
}
}
vector<Wine> winelist;
The main method has to be rewritten accordingly, to append additional objects to the vector.
While I could rewrite your code accordingly I suspect, that a better next step for you would be to read up on some of the concepts used, such as vectors.
You probably want something like this:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
};
void printwine(Wine1 wine);
int main()
{
vector<Wine1> wineinventory;
// read 3 wines
for (int i = 3; i < 10; i++)
{
Wine1 wine;
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
wineinventory.push_back(wine); // store in vectore
}
// print all wines in the vector
for (int i = 0; i < wineinventory.size(); i++)
{
cout << "Wine " << i << ":" endl;
printwine(wineinventory[i]);
}
}
Disclaimer: this is untested code, I'm not even sure if it compiles, but you should get the idea.
There is still much room for improvement.
Basically trying to just run this program for extra learning, Xcode won't understand that I have written a class, and wont implement it. Really confused and need some guidance.
When I run the code only the main method is implemented, nothing else works...
#include <iostream>
using namespace std;
class students {
int id;
char name[20];
int s1;
int s2;
int s3;
public:
void getData() {
cout << "Enter the ID " << endl;
cin >> id;
cout << "Enter the name " << endl;
cin >> name;
cout << "Enter the grade in subject 1 " << endl;
cin >> s1;
cout << "Enter the grade in subject 2 " << endl;
cin >> s2;
cout << "Enter the grade in subject 3 " << endl;
cin >> s3;
}
void putData() {
cout << id << " " << name << " " << s1 << " " << s2 << " " << s3 << endl;
}
};
int main () {
students s[20];
int i, n; //i is for the for loop, n for number of students
cout << "Enter the number of students " << endl;
cin >> n;
for (i=0;i>n;i++)
{
s[i].getData();
}
for (i=0;i>n;i++)
{
s[i].putData();
}
return 0;
}
sorry I am relatively new to c++ and am currently stuck. The point of the application is to have the user enter the number of employees they have and then information about their employees including the hours they worked and their pay rate. After that that application to print out all the information and then give them each employees gross pay. I thought I had everything set up correctly but am getting an error on line 26 it is saying "expression must have constant value". Any tips or advice would be appreciated.
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
struct Employee
{
int id;
string fName;
string lName;
int pay;
int hours;
};
int main() {
int i, n;
cout << "Enter number of employees";
cin >> n;
Employee Emp[n];
for (i = 0; i < n; i++) {
cout << "Enter Employee ID: ";
cin >> Emp[i].id;
cout << "Enter First Name: ";
cin >> Emp[i].fName;
cout << "Enter Last Name: ";
cin >> Emp[i].lName;
cout << "Enter in Pay Rate: ";
cin >> Emp[i].pay;
cout << "Enter in Hours: ";
cin >> Emp[i].hours;
}
cout << "\n*** Employee Details ***";
cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << setw(10) << "Pay" << setw(10) << "Hours" << setw(10) << "Gross Pay";
for (i = 0; i < n; i++) {
cout << "\n" << Emp[i].id << setw(15) << Emp[i].fName << setw(10) << Emp[i].lName << setw(10) << Emp[i].pay << setw(10) << Emp[i].hours << setw(10) << Emp[i].pay*Emp[i].hours;
}
_getch();
return 0;
}
Employee Emp[n];
In C/C++ you can't declare dynamic-size arrays like this.
See this question - How to create a dynamic array of integers
Or better, use an std::vector instead.
C++ standard requires you to provide an array size known at compile time.
Therefore to acquire what you want you need to use dynamic memory allocation i.e. allocate an array on heap depending upon the n being entered by the user. The following demonstrates this method.
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
struct Employee
{
int id;
string fName;
string lName;
int pay;
int hours;
};
int main() {
int i, n;
cout << "Enter number of employees";
cin >> n;
auto *Emp = new Employee[n];
for (i = 0; i < n; i++) {
cout << "Enter Employee ID: ";
cin >> Emp[i].id;
cout << "Enter First Name: ";
cin >> Emp[i].fName;
cout << "Enter Last Name: ";
cin >> Emp[i].lName;
cout << "Enter in Pay Rate: ";
cin >> Emp[i].pay;
cout << "Enter in Hours: ";
cin >> Emp[i].hours;
}
cout << "\n*** Employee Details ***";
cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << setw(10) << "Pay" << setw(10) << "Hours" << setw(10) << "Gross Pay";
for (i = 0; i < n; i++) {
cout << "\n" << Emp[i].id << setw(15) << Emp[i].fName << setw(10) << Emp[i].lName << setw(10) << Emp[i].pay << setw(10) << Emp[i].hours << setw(10) << Emp[i].pay*Emp[i].hours;
}
delete [] Emp;
return 0;
}
I need to store current date and time on a string to store it in a struct. I don't even know if that's possible, but I need to do it. I will try to further explain it:
I have this struct:
struct Apartment{
int number;
string owner;
string condition;
}ap
And this code to add a new object of the same struct:
cout << "Enter the apartment number: " << endl;
cin >> ap.number;
cout << "Enter the name of the owner: " << endl;
cin >> ap.owner;
cout << "Enter the condition: " << endl;
cin >> ap.condition;
And I need a variable for date and time. I need it to save the time and date the object was created. I don't know if I can do it with string or any other thing. I need it to be printable, too. I would be really thankful if you could help me.
You can use std::time_t, std::time() and std::ctime() like this:
#include <ctime>
#include <string>
#include <iostream>
struct Apartment
{
int number;
std::string owner;
std::string condition;
std::time_t when;
};
int main()
{
Apartment ap;
std::cout << "Enter the apartment number: " << std::endl;
std::cin >> ap.number;
std::cout << "Enter the name of the owner: " << std::endl;
std::cin >> ap.owner;
std::cout << "Enter the condition: " << std::endl;
std::cin >> ap.condition;
ap.when = std::time(0);// set the time to now
std::cout << "Record created on: " << std::ctime(&ap.when) << std::endl;
}