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, what I need is to print the structs inside the vector. I made the following function:
void PrintVehicle(vector<Vehicle> vtnewV){
{
vector<Vehicle> ::iterator i;
for (i = vtnewV.begin(); i != vtnewV.end(); i++)
{
cout << "License plate: " << i->License << endl;
cout << "Ubication: " << i->Place << endl;
cout << "Capacity: " << i->Capacity << endl;
cout << "Driver's name: " << i->dude.Name << endl;
cout << "Driver's code: " << i->dude.Code << endl;
cout << "Id: " << i->dude.Id << endl;
cout << " " << endl;
}
}
}
But it only prints out the elements of the first struct, printing out random numbers where the driver's info should be. Can you tell me where's my mistake? Everything else prints fine, except for the nested struct.
Vehicle::Driver dude;
You're declaring another variable here, which has nothing to do with the dude inside the newV (Vehicle).
Change the code to:
void AddVehicle(vector<Vehicle> &vtnewV){
Vehicle newV;
//Vehicle::Driver dude; // delete it here
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.dude.Name;
cout << "Enter the driver's code: " << endl;
cin >> newV.dude.Code;
cout << "Enter the driver's identification number: " << endl;
cin >> newV.dude.Id;
vtnewV.push_back(newV);
};
Related
I have written this code using array of structures in C++ and the output was correct, but when I included the pointers, the output is not correct and missing a lot of, what is the reason of this?
This is the code using pointers:
#include <iostream>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
Course info;
};
void menu(void) {
cout << "=================================" << endl;
cout << " MENU " << endl;
cout << "=================================" << endl;
cout << " 1- Add new students" << endl;
cout << " 2- Display student list" << endl;
cout << " 3- Add new course" << endl;
cout << " 4- Display Course Offered" << endl;
cout << " 5- Exit Program" << endl;
cout << "=================================" << endl << endl;
}
int main()
{
Student *ptr[2] ;
Student details[2];
ptr[2] = &details[2];
int m, option, courNo;
menu();
cout << "Choose an option from the menu: ";
cin >> option;
while (option != 5)
{
if (option == 1) {
// cout << "\nHow many students do you want to enter? ";
// cin >> stuNo;
for (m = 0; m < 2; m++) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> ptr[m]->StudentID;
cout << "Name: ";
cin >> ptr[m]->StudentName;
cout << "Nickname: ";
cin >> ptr[m]->Nickname;
}
}
else if (option == 2) {
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < 2; m++) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << ptr[m]->StudentID << endl;
cout << "Name: " << ptr[m]->StudentName << endl;
cout << "Nickname: " << ptr[m]->Nickname << endl;
}
}
else if (option == 3) {
cout << "\nHow many courses do you want to enter? ";
cin >> courNo;
for (m = 0; m < courNo; m++) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> ptr[m]->info.CourseCode;
cout << "Course Name: ";
cin >> ptr[m]->info.CourseName;
cout << "Lecturer Name: ";
cin >> ptr[m]->info.LecturerName;
}
}
else if (option == 4) {
cout << "\nThe courses details you entered are:" << endl;
for (m = 0; m < 2; m++) {
cout << "\nCourse " << m + 1 << ": " << endl;
cout << "Course Code: " << ptr[m]->info.CourseCode << endl;
cout << "Course Name: " << ptr[m]->info.CourseName << endl;
cout << "Lecturer Name: " << ptr[m]->info.LecturerName << endl;
}
}
else
cout << "Invalid number! Please re-enter a choice from the menu." << endl;
cout << "=====================================================" << endl;
cout << "\nChoose an option from the menu: ";
cin >> option;
}
if (option == 5)
cout << "End of program!" << endl;
}
This is the output of the program, after I enter the code it was terminated:
=================================
MENU
=================================
1- Add new students
2- Display student list
3- Add new course
4- Display Course Offered
5- Exit Program
=================================
Choose an option from the menu: 3
How many courses do you want to enter? 2
Enter course 1 details:
Course Code: 123
This is the output without using pointers, it was like this which is the output that I'm looking for:
=================================
MENU
=================================
1- Add new students
2- Display student list
3- Add new course
4- Display Course Offered
5- Exit Program
=================================
Choose an option from the menu: 3
How many courses do you want to enter? 2
Enter course 1 details:
Course Code: 0000
Course Name: course1
Lecturer Name: lecturer1
Enter course 2 details:
Course Code: 1111
Course Name: course2
Lecturer Name: lecturer2
=====================================================
Choose an option from the menu: 5
End of program!
This is part of my project codes.
About the struct customer that I did, from welcomeScreen function I call the getInfo function for user to input the details (as you can see) and then return back the value to welcomeScreen function for output.
I can compile the codes, but the problem is there is no output for all the details after I input it (just blank)? Sorry if this is a dumb question cause im still a student.
struct customer
{
string name;
string email;
int number;
};
void welcomeScreen(); //prototype
void getInfo(struct customer cust); //prototype
void welcomeScreen()
{
struct customer cust; // struct declaration
const int SIZE=5;
system("CLS");
cout << setfill ('-') << setw (55) << "-" << endl;
cout << "\tWelcome to Computer Hardware Shop" << endl;
cout << setfill ('-') << setw (55) << "-" << endl;
cout << endl << "Type of hardwares that we sell:" << endl;
string item[SIZE]={"Monitor","CPU","RAM","Solid-State Drive","Graphic Card"};
for(int i=0;i<SIZE;i++)
cout << "\t" << i+1 << ". " << item[i] << endl;
getInfo(cust);
cout << endl;
cout << fixed << showpoint << setprecision (2);
cout << "Name: "<< cust.name << endl; // struct output
cout << "Email: "<< cust.email << endl;
cout << "Phone Number: " << cust.number << endl;
cout << endl;
}
void getInfo(struct customer cust)
{
cout << endl << "Enter name: ";
cin >> cust.name;
cout << "Enter email: ";
cin >> cust.email;
cout << "Enter phone number: ";
cin >> cust.number;
}
You probably want to pass a pointer or a reference, in this case recommend a reference because it means fewer changes to your code:
void getInfo(struct customer &cust); //prototype
Remember to change your function parameter as well.
I am trying to make a program that will take a users input to make multiple forms. I am stuck on trying to get the vector (that will be filled with objects of the form class that the user creates) to be use-able in other functions. When I use the address-of operator (&) it gives me this error when the program gets to letting the user input the data to the objects.
This is the screen capture of the program and the error.
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Form {
public:
string Fname;
string Lname;
string City;
string Street;
string State;
string ZipCode;
};
void menuMain();
void menu1st(vector<Form> &Fvect);
void menu1st(vector<Form> &Fvect)
{
int MainM;
int n;
cout << "NEW FORM(s)" << endl;
cout << "Enter the number of forms you would like to make (Maximum of 5): "; cin >> n; cout << endl;
for (int i = 0; i < n; i++)
{
cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
cout << "City: "; cin >> Fvect[i].City; cout << endl;
cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
cout << "State: "; cin >> Fvect[i].State; cout << endl;
cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
}
cout << "Enter 1 to go back to main: "; cin >> MainM;
if (MainM == 1)
{
menuMain();
}
else
{
cout << "Error not a correct input." << endl;
}
}
void menu2nd()
{
int MainM;
//int Fnum;
vector<Form> Fvect;
cout << "EDIT A FORM" << endl;
cout << Fvect[1].Fname;
cout << "Enter the ";
cout << "Enter 1 to go back to main: "; cin >> MainM;
if (MainM == 1)
{
menuMain();
}
else
{
cout << "Error not a correct input." << endl;
}
}
void menuMain()
{
int Pnum;
cout << "INFORMATION FORMATTING PROGRAM" << endl;
cout << "1. Create new form's." << endl;
cout << "2. Edit a form." << endl;
cout << "3. Print forms." << endl;
cout << "4. Erase a form." << endl;
cout << "5. Exit Program." << endl;
cout << "Enter the action you want to take (1-5): "; cin >> Pnum;
vector<Form> Fvect;
if (Pnum == 1)
{
menu1st(Fvect);
}
if (Pnum == 2)
{
menu2nd();
}
else
{
cout << "Error not a correct input." << endl;
}
}
int main()
{
menuMain();
}
You are accessing Fvect using an invalid index in the following lines:
cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
cout << "City: "; cin >> Fvect[i].City; cout << endl;
cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
cout << "State: "; cin >> Fvect[i].State; cout << endl;
cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
Consequently, your program has undefined behavior.
You need to have items in a std::vector before you can access an item from it using the array syntax. What you need to do is:
Read the data to an object of type Form.
Add the object to the std::vector.
Replace those lines with:
Form form;
cout << "First Name: "; cin >> form.Fname; cout << endl;
cout << "Last Name: "; cin >> form.Lname; cout << endl;
cout << "City: "; cin >> form.City; cout << endl;
cout << "Street: "; cin >> form.Street; cout << endl;
cout << "State: "; cin >> form.State; cout << endl;
cout << "Zip Code: "; cin >> form.ZipCode; cout << endl;
Fvect.push_back(form);
PS
I am not sure why you have the cout << endl; in those lines. You don't need them. It will be sufficient to use:
cout << "First Name: "; cin >> form.Fname;
cout << "Last Name: "; cin >> form.Lname;
cout << "City: "; cin >> form.City;
cout << "Street: "; cin >> form.Street;
cout << "State: "; cin >> form.State;
cout << "Zip Code: "; cin >> form.ZipCode;
i'm trying to load the file into an array when the program starts so i can
modify or search in it i don't know if my code works or not ( it's not reading the file )i have the file
and there's two books in
i have tried to debug it but couldn't find the problem the code works
but it think there's a problem with the load() function i don't know what
my code :
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct books{
//identfying books with all needed things
int id, status;
string title, p_name, p_address;
string date;
string aut_name, aut_nationality;
}newbook[10000], aut[10000];
int i = 0;
void load(){
ifstream myfile("books.txt", ios::in);
while (myfile >> newbook[i].id >> newbook[i].title >> newbook[i].p_name >> newbook[i].p_address >> aut[i].aut_name >> aut[i].aut_nationality >> newbook[i].date >> newbook[i].status)
i++;
}
void search_for_book(){
int temp = 0;
int idx;
cout << "enter the ID of the book you're looking for : ";
cin >> idx;
for (int srh = 0; srh < i; srh++){
if (newbook[srh].id == idx){
cout << setw(10) << "book found :" << endl;
cout << "title :" << newbook[srh].title << endl;
cout << "publisher name : " << newbook[srh].p_name << endl;
cout << "publisher address" << newbook[srh].p_address << endl;
cout << "author name :" << aut[srh].aut_name << endl;
cout << "author Nationality :" << aut[srh].aut_nationality << endl;
cout << "publish Date :" << newbook[srh].date << endl;
cout << "status :" << newbook[srh].status << endl;
temp++;
break;
}
else
srh++;
}
if (temp == 0){
cout << "couldn't find book" << endl << endl;
}
}
int main(){
load();
char choice;
cout << "enter your choice (3)";
cin >> choice;
if (choice == '3'){
search_for_book();
}
}
note :*( there are other functions like adding new book but not necessary to write )
*(i'm new to c++ don't really know how to read file into memory but i'm trying)
this is the code to save data into file :
void add_new_book_5(){
int booksnumber;
books newbook[1000], aut[100];
cout << "how many books you want to add ? ";
cin >> booksnumber;
cout << "what books you want to add :" << endl;
d_base.open(path, ios::out | ios::app);
for (int i = 0; i < booksnumber; i++){
cout << "id please : "; cin >> newbook[i].id;
cout << "title : "; cin.ignore(); getline(cin, newbook[i].title);
cout << "publisher name :"; getline(cin, newbook[i].p_name);
cout << "publisher address : "; getline(cin, newbook[i].p_address);
cout << "author" << " name : "; cin.ignore(); getline(cin, newbook[i].aut_name);
cout << "Nationality : "; getline(cin, newbook[i].aut_nationality);
cout << "Publish date :"; getline(cin, newbook[i].date);
cout << "How many copies of " << newbook[i].title << " "; cin >> newbook[i].status;
system("cls");
d_base << newbook[i].id << "\ " << newbook[i].title << "\ ";
d_base << newbook[i].p_name << "\ " << newbook[i].p_address << "\ ";
d_base << newbook[i].aut_name << "\ " << newbook[i].aut_nationality << "\ ";
d_base << newbook[i].date << "\ " << newbook[i].status << endl;
}
d_base.close();
cout << setw(76) << "Books Have Been Saved Sucessfully" << endl;
}
my program seems to want to enter two inputs for name variable instead of just entering one thing and moving on to phone number?
i'm sure its simple but can someone help me fix this please? is it something it do with the getline?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//define Car struct
struct Speaker
{
string name;
string phoneNumber;
string emailAddress;
string theme;
double fee;
};
Speaker *getSpeaker();
int main()
{
Speaker thespeaker;
thespeaker = *getSpeaker();
cout << "The speaker entered is!" << endl;
cout << thespeaker.name << endl;
cout << "phone number: " << thespeaker.phoneNumber << endl;
cout << "email: " << thespeaker.emailAddress << endl;
cout << "theme: " << thespeaker.theme << endl;
cout << "fees: " << thespeaker.fee << endl;
}
Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cin.ignore(100, '\n');
cin.clear();
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;
return theSpeaker;
}
There's no need for cin.ignore();
Simply write it as:
Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;
return theSpeaker;
}