I want to make a program that lets the user input the brand, price and color of a car for an unknown amount of cars, and cant figure out how to do that or what i have to search for in order to understand.
Example of what i want it to do: i want 20 cars, and i want to input values for each one of them and at the end have the program say which brand is the most expensive.
#include <iostream>
using namespace std;
struct car{
char brand[50];
char color[60];
unsigned short int price;
};
void compare(car a, car b){
if(a.price > b.price)
cout << "Most expensive: " << a.brand;
else
cout << "Most expensive: " << b.brand;
}
int main()
{
car m1, m2;
cout << "Brand of first car: "; cin >> m1.brand; cout << endl;
cout << "Color of first car: "; cin >> m1.color; cout << endl;
cout << "Price of first car: "; cin >> m1.price; cout << endl << endl;
cout << "Brand of second car: "; cin >> m2.brand; cout << endl;
cout << "Color of second car: "; cin >> m2.color; cout << endl;
cout << "Price of second car: "; cin >> m2.price; cout << endl << endl;
compare(m1, m2);
return 0;
}
Your first step will be:
int main()
{
car m[20]; // todo better: std::vector
for(int i=0; i < 20; i++)
{
cout << "Brand of first car: "; cin >> m[i].brand; cout << endl;
cout << "Color of first car: "; cin >> m[i].color; cout << endl;
cout << "Price of first car: "; cin >> m[i].price; cout << endl << endl;
}
}
The next step will be to do something with m[].
If you create your object (of type car) named m1, m2 and then you would like to add some values to its' fields, you have to create the object dynamically, so it will be:
car *m1 = new car;
cin >> m1->brand;
I'm more into Java right now, so I'm not 100% sure about the syntax, but the concept seems right. Hope it works! ;-)
Related
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;
}
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;
}
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.