Using a struct to hold information entered by the user C++ - c++

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.

Related

How to introduce values in an array of structs? (c++)

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! ;-)

My program is not correctly outputting the amount of students in my struct

i am making a program which is supposed to collect input from the user by asking for id, name, units attempted, units earned and the gpa of a student and store that into a struct. Currently, when i input more than one student into the struct, it only outputs the last student in the array. What can i do to fix this?
Here is my code
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student {
string ID; //an 8-digit student ID
string name; //name of the student
unsigned unitsAttempted;//number of units attempted by the student
unsigned unitsEarned; //number of units earned by the student
double gpa; //the grade point average for the student
};
int main()
{
Student students[10];//array for 10 Student Structs
char answer = 'y';
int number = 0;
while ((answer == 'y') || (answer == 'Y')) {
//prompt the user to enter the data
{
answer = ' ';
int i = 0;
cout << "Enter the students ID: \n" ;
getline(cin, students[i].ID);
cout << "\nEnter the name:\n";
getline(cin, students[i].name);
cout << "\nEnter the units attempted:\n";
cin >> students[i].unitsAttempted;
cin.ignore();
cout << "\nEnter the units earned:\n";
cin >> students[i].unitsEarned;
cin.ignore();
cout << "\nEnter the GPA: \n";
cin >> students[i].gpa;
cin.ignore();
cout << "\nWould you like to add another student? y or n\n";
cin >> answer;
cin.ignore();
i++;
number++;
}
}
cout << left << setw(10) << "ID " << setw(20) <<"Name" << setw(20) <<"Units Attempted" << setw(20) <<"Units Earned" << setw(10) <<"GPA";
cout << "\n===============================================================\n";
for (int i = 0; i < number; i++)//display the students
{
cout << left << setw(10) << students[i].ID << setw(20) << students[i].name << setw(20) <<students[i].unitsAttempted << setw(20) <<students[i].unitsEarned << setw(10) <<students[i].gpa << endl << endl;
}
return 0;
}
Im still learning so please go easy on me, thanks.
Currently, when i input more than one student into the struct, it only outputs the last student in the array.
It looks most likely that the scope of your i is incorrect:
int i = 0; //<-- this should be declared outside the `while` loop
As it stands, your i is scoped inside while loop, so it will be reset every time the while loop is entered. In other word, that i never increased.

fix repeating "enter names of teacher" after I finish the input?

this code should record school data names and address but after input it repeats "enter names of teachers:" line several times before the output of data entered successfully how can i fix this problem? any tweaks in the code to achieve this? it should be school projects in C++ but I can't fix this problem at all I would apprecciate any help
#include <iostream>
#include <stack>
using namespace std;
int main()
{
string sname, saddress, sname_teacher;
int snumber_classrooms;
double ratio, snumber_students, snumber_teachers;
//Entering data of school
cout << "Enter school data: \n";
cout << "Enter name of school : ";
cin >> sname;
cout << "Enter an address of school : ";
cin >> saddress;
cout << "Enter number of classrooms of the school : ";
cin >> snumber_classrooms;
cout << "Enter number of students of the school : ";
cin >> snumber_students;
cout << "Enter number of teachers of the school : ";
cin >> snumber_teachers;
//check if number of teachers < 20 or not
if (snumber_teachers > 20) {
while (snumber_teachers > 20) {
cout << " The number of teachers should not be more than 20 teachers. \n";
cout << "Enter number of teachers of the school : ";
cin >> snumber_teachers;
}
}
//check if number of teachers : number of students < (1/50=0.02)
ratio = snumber_teachers / snumber_students;
while (ratio < 0.02) {
cout << " The number of teachers should not be less than 1/50 number of students. \n";
cout << "Enter number of teachers of the school : ";
cin >> snumber_teachers;
ratio = snumber_teachers / snumber_students;
}
// Entering names of teachers
int snumber_teachers2 = snumber_teachers;
stack<string> names;
for (int i = 0; i < snumber_teachers2; i++) {
cout << "Enter names of teachers : ";
cin >> sname_teacher;
names.push(sname_teacher);
}
cout << " \n The Data of shool has entered succedly :)\n \n";
// Printing data of school
cout << " _______________________The Data of school are_______________________ \n";
cout << " name : " + sname << endl;
cout << " address " + saddress << endl;
cout << " classrooms : " << snumber_classrooms << endl;
cout << " students : " << snumber_students << endl;
cout << " teachers : " << snumber_teachers << endl;
cout << " names of teachers : \n";
// Printing content of stack
while (!names.empty()) {
cout << ' ' << names.top();
names.pop();
cout << "\n";
}
return 0;
}
You want to receive several inputs for teacher names, but only prompt "Enter names of teachers : " once.
So change
for (int i = 0; i < snumber_teachers2; i++) {
cout << "Enter names of teachers : ";
cin >> sname_teacher;
names.push(sname_teacher);
}
to
cout << "Enter names of teachers : ";
for (int i = 0; i < snumber_teachers2; i++) {
cin >> sname_teacher;
names.push(sname_teacher);
}
I.e. keep what you want to repeat inside the loop,
but move what you only want once outside.

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

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