Access the loop result to instance of class for output - c++

Alright, so I have an class-inventory assignment for my C++ class. The thing I'm struggling with right now is the part between the loop and object creation.
string description = "";
int id_number{0};
int quantity_number{0};
double price_value{0};
for (int count{1}; count <= inventory_num; count++)
{
cout << endl;
cout << "Item #" << count++ << endl;
cout << "Enter the id number: ";
cin >> id_number;
cout << "Descriptiom: ";
cin.get();
getline(cin, description);
cout << "Quantity on hand: ";
cin >> quantity_number;
cout << "Unit price: ";
cin >> price_value;
cout << endl;
}
InventoryItem item1(id_number, description, quantity_number, price_value);
InventoryItem item2(id_number, description, quantity_number, price_value);
InventoryItem item3(id_number, description, quantity_number, price_value);
InventoryItem item4(id_number, description, quantity_number, price_value);
item1.display(); cout << endl;
item2.display(); cout << endl;
item3.display(); cout << endl;
item4.display(); cout << endl;
return EXIT_SUCCESS;
}
So the problem is, for example, looping the input for 4 times, but the output only shows the the data from the LAST INPUT OF THE LOOP for ALL OF THE OUTPUT(item1,item2,item3,item4). How do fix this lads?

You overwrite the values in each loop iteration. After the loop the values of the last iteration are stored in your variables. You could fix it with a std::vector
#include "InventoryItem.hpp"
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::getline;
int main() {
string description = "";
int id_number{0};
int quantity_number{0};
double price_value{0};
std::vector<InventoryItem> items;
for (int count{1}; count <= inventory_num; count++)
{
cout << endl;
cout << "Item #" << count++ << endl;
cout << "Enter the id number: ";
cin >> id_number;
cout << "Descriptiom: ";
cin.get();
getline(cin, description);
cout << "Quantity on hand: ";
cin >> quantity_number;
cout << "Unit price: ";
cin >> price_value;
cout << endl;
items.emplace_back(id_number, description, quantity_number, price_value);
}
items[0].display(); cout << endl;
items[1].display(); cout << endl;
items[2].display(); cout << endl;
items[3].display(); cout << endl;
return EXIT_SUCCESS;
}

Related

C++ Using Struct for employee record/Gross pay calculator

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;
}

Referencing vector causes error when running program

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;

Loop over list of objects skipped

I'm making a list of Student Objects, and want to iterate over them and output the values they have. I'm not sure why the for loop is being skipped over. Any help/guidance would be appreciated. Here is the loop:
void studentInfo(list<Student> stuList) {
cout << "in studentinfo" << endl;
for (list<Student>::iterator it = stuList.begin(); it != stuList.end(); ++it) {
cout << "in student info loop" << endl;
cout << it->toString();
}
cout << "after loop" << endl;
}
I get the cout messages that are right before and after the loop. Here is the toString() method if you need.
string Student::toString() {
stringstream outString;
outString << "Name: " << name << "\nID: " << id << "\nAge: " << age << endl;
return outString.str();
}
This is how the list is being populated, I'm adding 2 student objects usually when testing:
void addToList(list<Student> stuList) {
string tempName;
int tempID;
int tempAge;
int numStudents;
cout << "How many students will you be entering?\n";
cin >> numStudents;
for (int i = 0; i < numStudents; i++) {
cout << "Enter student name: \n";
cin >> tempName;
cout << "Enter student id: \n";
cin >> tempID;
cout << "Enter student age: \n";
cin >> tempAge;
stuList.push_back(Student(tempName, tempID, tempAge));
}
}

c++ file reading into memory

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;
}

when running program it has me enter two lines after name? help please

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;
}