I have a project where I need to create a shopping list with checkout functionality. I am trying to create an array using a users input. They supply how many products they are purchasing and I need to use that to define the size of the array.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct shopList {
double pluCode;
string product;
int saleType; // 0 = per unit, 1 = per pound
double price;
double inventory;
};
int main(){
char line[255];
const int items = 0;
int n;
ofstream outfile;
outfile.open("products.txt");
cout << "How many items in your checkout: ";
cin >> items;
shopList shop[items];
for (n = 0; n < items; n++) {
cout << "Enter the PLU code: ";
cin >> shop.pluCode;
outfile << shop.pluCode << " ";
cout << "Enter product name: ";
cin >> shop.product;
outfile << shop.product << " ";
cout << "Enter the price type (0 for per unit, 1 for per pound): ";
cin >> shop.saleType;
outfile << shop.saleType << " ";
cout << "Enter the price of the product: ";
cin >> shop.price;
outfile << shop.price << " ";
cout << "How much will you purchase: ";
cin >> shop.inventory;
outfile << shop.inventory << " " << "\n";
}
outfile.close();
ifstream infile;
infile.open("products.txt");
infile.getline(line, 255);
cout << line << endl;
}
It's possible , you just have to change your declaration like that;
int items = 0 ;
cin >> items;
shopList *shop = new shopList [items];
Related
I am looking to remove the space from the name part of my assignment which allows users to put any name with or without space in my product list. for example "TV stand", I assume getline function will help me with it but I couldn't add the getline function into my main. can anyone help me with it?
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
struct product {
char product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
cin >> Products[i].product_name;
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
cin >> Products[i].no_of_sales;
To be honest I don't understand your question but this might be helpful for you
I changed the product_name data type from char to string because the char data type is not making any sense
#include <iostream>
#include <string>
using namespace std;
struct product {
string product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cin.ignore();
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
getline(cin, Products[i].product_name);
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
getline(cin, Products[i].no_of_sales);
cin.ignore();
}
return 0;
}
I'm working on a program in C++ to make schedules for me, since I have a hard time focusing on school work. The code will make priorities of assignments based on the due dates, duration to do the assignments, etc.
Anyway, I've run into a massive problem. I'm trying to assign a class variable's name to a variable! For example:
string a = "assignmentName" Class a(); //a is a variable and it's supposed to put the class's name as assignmentName
If you don't think this way will work, please tell me other solutions!
Also, I'm still a beginner, so please try to explain a little simple!
Here's the code:
#include <string>
#include <iostream>
using namespace std;
class Assignment
{
private:
int duration = 30; //Amount of time per assignment (in minutes)
int due = 1; //Amount of days till its due, if it's one then the assignment is due in 1 days
string name; //Name of assignment
public:
Assignment(int, int, string);
~Assignment();
void setDuration(int duration) {this -> duration = duration;}
void setDue(int due) {this -> due = due;}
void setDue(int name) {this -> name = name;}
int getDuration() { return duration; }
int getDue() { return due; }
string getName() { return name; }
};
Assignment::Assignment (int duration, int due, string name) //Constructor Method
{
this -> duration = duration;
this -> due = due;
this -> name = name;
}
Assignment::~Assignment() //Destructor Method
{
cout << "Assignment Object Destroyed" << endl;
}
int main()
{
int assignmentAmount;
int i;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
for (i = 0; i < assignmentAmount; i++){
cout << "Enter assignment" << i <<"'s " << "duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment" << i << "'s " << "due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment" << i <<"'s " << "name: ";
cin >> userInput3 >> endl;
Assignment i(userInput1, userInput2, userInput3); //I is the class's number and it's supposed to be a variable
}
return 0;
}
What you are looking for is an array of Assignment objects, eg:
int main()
{
Assignment *assignments;
int assignmentAmount;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
assignments = new Assignment[assignmentAmount];
for (int i = 0; i < assignmentAmount; i++){
cout << "Enter assignment " << i << "'s duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment " << i << "'s due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment " << i <<"'s name: ";
cin >> userInput3 >> endl;
// either:
assignments[i] = Assignment(userInput1, userInput2, userInput3);
// or:
assignments[i].setDuration(userInput1);
assignments[i].setDue(userInput2);
assignments[i].setName(userInput3);
}
// use assignments and assignmentAmount as needed...
delete[] assignments;
return 0;
}
Though, you should use std::vector instead of new[] directly:
#include <vector>
...
int main()
{
std::vector<Assignment> assignment;
int assignmentAmount;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
for (int i = 0; i < assignmentAmount; i++){
cout << "Enter assignment " << i << "'s duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment " << i << "'s due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment " << i << "'s name: ";
cin >> userInput3 >> endl;
// either:
assignments.push_back(Assignment(userInput1, userInput2, userInput3));
// or:
assignments.emplace_back(userInput1, userInput2, userInput3);
}
// use assignments as needed...
return 0;
}
Or:
#include <vector>
...
int main()
{
std::vector<Assignment> assignment;
int assignmentAmount;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
assignments.resize(assignmentAmount);
for (int i = 0; i < assignmentAmount; i++){
cout << "Enter assignment " << i << "'s duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment " << i << "'s due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment " << i << "'s name: ";
cin >> userInput3 >> endl;
// either:
assignments[i] = Assignment(userInput1, userInput2, userInput3);
// or:
assignments[i].setDuration(userInput1);
assignments[i].setDue(userInput2);
assignments[i].setName(userInput3);
}
// use assignments as needed...
return 0;
}
You can't get variable names as a variable, since a variable is basically like an alias to the data, when your program gets compiled all these names, don't exist anymore.
The compiler does not emit symbol names to the executable (the debug version may have symbol names).
If you want to associate or map symbol names to variables, you will need to declare the variable global and place the address into some kind of table.
Example:
int my_global_int_a;
std::map<std::string, int *> variable_dictionary;
variable_dictionary["my_global_int_a"] = &my_global_int_a;
// Here's how to get the variable
const std::string variable_name = "my_global_int_a";
int * p_variable = nullptr;
p_variable = variable_dictionary[variable_name];
std::cout << *p_variable << std::endl;
You may want to give deep thought to the need of retaining variable names.
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;
}
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;
}
Okay, so I am writing a C++ program to declare a struct data type that holds the following information on an employee (First Name, Last Name, ID, Pay Rate, and Hours). My problem is that the user can only enter in the ID and First Name, then the whole program runs without letting the user enter the rest of the data.
Heres my code:
#include <iostream>
#include <iomanip>
using namespace std;
struct Employee
{
int employeeID;
char firstName;
char lastName;
float payRate;
int hours;
};
int main()
{
int i, j;
cout << "How Many Employees Do You Wish To Enter?:\n\n";
cin >> j;
Employee info;
for (i = 0; i < j; i++)
{
cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << setw(5) << "\n Please Enter The Employee ID Number: ";
cin >> info.employeeID;
cout << setw(5) << "\n Please Enter Employees First Name: ";
cin >> info.firstName;
cout << setw(5) << "\n Please Enter Employees Last Name: ";
cin >> info.lastName;
cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
cin >> info.payRate;
cout << setw(5) << "\n Please Enter The Hours The Employee Worked:
";
cin >> info.hours;
}
cout << "\n\n \n";
cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" <<
setw(10) << "Pay Rate" << setw(10) << "Hours";
cout << endl;
for (i = 0; i < j; i++)
{
cout << "\n" << info.employeeID << setw(15) << info.firstName << setw(10) << info.lastName << setw(10) << info.payRate << setw(10) << info.hours;
}
cout << "\n\n \n";
system("pause");
return 0;
};
#include <iostream>
#include <iomanip>
#include <string> //Allows you to use strings, which are way more handy for text manipulation
#include <vector> //Allows you to use vector which are meant to be rezied dynamicaly, which is your case
using namespace std;
struct Employee
{
int employeeID;
string firstName; //HERE : use string instead of char (string are array of char)
string lastName; //HERE : use string instead of char
float payRate;
int hours;
};
int main()
{
int j;
cout << "How Many Employees Do You Wish To Enter?:\n\n";
cin >> j;
vector<struct Employee> info; //creation of the vector (dynamic array) to store the employee info the user is going to give you
for (int i = 0; i < j; i++) //declare your looping iterator "i" here, you will avoid many error
{
struct Employee employee_i; // create an employee at each iteration to associate the current info
cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << "\n Please Enter The Employee ID Number: ";
cin >> employee_i.employeeID;
cout << "\n Please Enter Employees First Name: ";
cin >> employee_i.firstName;
cout << "\n Please Enter Employees Last Name: ";
cin >> employee_i.lastName;
cout << "\n Please Enter Employees Pay Rate: ";
cin >> employee_i.payRate;
cout << "\n Please Enter The Hours The Employee Worked: ";
cin >> employee_i.hours;
info.push_back(employee_i); //store that employee info into your vector. Push_back() methods expands the vector size by 1 each time, to be able to put your item in it
} // because you employee variable was create IN the loop, he will be destruct here, but not the vector which was created outside
cout << "\n\n \n";
for (int i = 0; i < j; i++) //the loop to get back all the info from the vector
{
cout << "ID :" << info[i].employeeID << " First Name :" << info[i].firstName << " Last Name :" <<
info[i].lastName << " Pay Rate :" << info[i].payRate << " Hours :"<< info[i].hours;
cout << endl;
//notice the info[i], which leads you to the employee you need and the ".hours" which leads to the hours info of that specific employee
}
system("pause");
return 0;
}
First, please read Tips and tricks for using C++ I/O (input/output). It might be helpful to understand C++ I/O.
Here are some comments on your code:
First
Use string instead of char.
struct Employee
{
int employeeID;
string firstName;
string lastName;
float payRate;
int hours;
};
Second
Use array of Employee object to store multiple employees.
Employee info[100];
Third
Use cin carefully depending data types. In your case, it would be something like this:
cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << setw(5) << "\n Please Enter The Employee ID Number: ";
cin >> info[i].employeeID;
cin.ignore(); //It is placed to ignore new line character.
cout << setw(5) << "\n Please Enter Employees First Name: ";
getline (cin, info[i].firstName);
cout << setw(5) << "\n Please Enter Employees Last Name: ";
getline (cin, info[i].lastName);
cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
cin >> info[i].payRate;
cout << setw(5) << "\n Please Enter The Hours The Employee Worked: ";
cin >> info[i].hours;
Fourth
std::getline() can run into problems when used before std::cin >> var. So, std::cin.ignore() can be used in this case to solve the problem.
I hope it helps.