how can i put string after int in c++ - c++

i want to create a c++ program like that
Design and implement using C++, a class "Student" that stores the student id, name,
40
address and marks in five different subjects (in an array having five elements). Assume
that marks are out of 100. The class should have a constructor, a member function for
input of marks and an additional member functions which prints the student details
and marks, along with total marks and percentage. Write the main function which
creates two such student objects and displays their details
i want to insert student name and marks in this program but i am getting error why ...
this is my program..
please provide solution with example
#include <iostream>
#include<stdio.h>
using namespace std;
class student{
private:
int id;
char *name;
int marks[5];
char *address;
public:
student();
void input_detail();
void display_detail();
double total_marks();
double percentage();
};
student::student(){
id=0;
name='\0';
marks[5]=NULL;
address='\0';
}
void student::input_detail(){
int i=1,j=0;
cout<<"please enter student id: "<<endl;
cin>>id;
cout<<"please enter student name: "<<endl;
gets(name);
for(i=1,j=0;i<6;i++,j++){
cout<<"enter marks "<<i<<" subject : "<<endl;
cin>>marks[j];
}
cout<<"please enter student address : "<<endl;
gets(address);
}
double student::total_marks(){
double total_marks;
int i=0;
for(i=0;i<5;i++){
total_marks=total_marks+marks[i];
}
return total_marks;
}
double student::percentage(){
double percentage;
percentage=total_marks()/500*100;
return percentage;
}
void student::display_detail(){
int i=1,j=0;
cout<<"student id: "<<id<<endl;
cout<<"student id: "<<name<<endl;
for(i=1,j=0;i<6;i++,j++){
cout<<"marks "<<i<<" subject : "<<marks[j]<<endl;
}
cout<<"student address : "<<address<<endl;
cout<<"student total marks : "<<total_marks()<<endl;
cout<<"student percentage : "<<percentage()<<endl;
}
int main()
{
student s1;
s1.input_detail();
s1.display_detail();
return 0;
}
i am getting this output
please enter student id:
anil
please enter student name:
please enter student address :
annn
student id: 0
student id: anil
student address : annn
student total marks : 1.1331e-317
student percentage : 0
Process returned 0 (0x0) execution time : 19.901 s
Press any key to continue.
but where is the student marks it is not showing in program

A quick check shows me two errors:
The for condition is the opposite of what you think: when it's true the loop continues, when it's false the loop quits. A loop like for(int i=0; i>6; i++){...} will never run because the condition is false when starting (0 is not bigger than 6).
When you declare double a variable in C++ you need to initialize it; this error is present when computing the sum (start with double total_marks=0).
There are indeed many other errors and questionable approaches, but those are probably better discussed in class...

Related

why is the file not being created by f stream or even if it is the output is just garbage values?

I had to design a basic hospital management system by using Turbo C++ . everything seems to be working fine including the add record function but when i choose to view the records that were entered (through a main menu that was created using the same program) the function just starts displaying an infinite loop of random characters. moreover i can't seem to find the file that was created in C:\TurboC++\Disk\TurboC3\BIN.
I think that perhaps the problem arises because the file was not created and there may be another file with the same name as of the class.
p.s. i know that i am using an ancient relic so you don't need to mention that and its my first semester so i am still not aware with all the technical stuff so would very much appreciate if you kept the language as simple as possible to help me and others like me.
class hospital {
int bill,
ref,
age;
char disease[30],
name[20],
doctor[20],
address[50],
sex;
public : void getdata() {
cout<<"ENTER THE NAME OF THE PATIENT"<<endl;
gets(name);
cout<<endl<<"ENTER THE ADDRESS OF THE PATIENT"<<endl;
gets(address);
cout<<endl<<"ENTER THE NAME OF THE DIESEASE(S) OF THE PATIENT"<<endl;
gets(disease);
cout<<endl<<"ENTER THE SEX OF THE PATIENT(f/m)"<<endl;
cin>>sex;
cout<<endl<<"ENTER THE NAME OF THE DOCTOR"<<endl;
gets(doctor);
cout<<endl<<"ENTER THE PRESCRIPTION/REFERENCE no."<<endl;
cin>>ref;
cout<<endl<<"ENTER THE AGE OF THE PATIENT"<<endl;
cin>>age;
cout<<endl<<"ENTER THE BILL OF THE PATIENT"<<endl;
cin>>bill;
}
void display() {
cout<<endl<<"THE DETAILS OF THE PATIENT ARE AS FOLLOWS:"<<endl;
cout<<"AGE:"<<age<<"\tREFERENCE/PRESCRIPTION no.:"<<ref;
cout<<"\tTHE NAME OF PATIENT:";
puts(name);
cout<<endl<<"\tTHE NAME OF THE DOCTOR:";
puts(doctor);
cout<<endl<<"\tTHE DIAGNOSED/TESTED FOR DISEASE(S) IS/ARE :"<<endl;
puts(disease);
cout<<endl<<"THE SEX OF THE PATIENT IS "<<sex<<endl;
cout<<endl<<"THE BILL OF THE PATIENT IS "<<bill<<endl;
cout<<endl<<"THE ADDRESS OF THE PATIENT IS: "<<endl;
puts(address);
}
int retref() {
return ref;
}
}
;
void main() {
clrscr();
hospital h;
cout<<endl<<"\n\n\t\t\tLOADING...\n\t\tPLEASE WAIT..\n";
system("pause");
clrscr();
cout<<endl<<"\tWELCOME TO THE HOSPITAL MANAGEMENT SYSTEM"<<endl;
displayrec();
getch();
}
this is the function to view all the records that were saved
void displayrec()
{
hospital h;
ifstream fin;
fin.open("hospitalrecords.dat", ios: :binary);
while(!fin.eof()) {
h.display();
if(fin.eof()) break;
}
}
You don't read anything from the file, so eof() will always be false, leading to an infinite loop.
You need to actually read something in the loop, and initialize h with the data you read.

Traversing a linked list with a structure inside

So I a linked list of 4 students and inside each node for the linked list is a structure that holds some data about the students. I want to traverse this linked list and print the data inside each of the structures. I can traverse the linked list expect all the data print is 0. Any help would be much appreciated.
#include<stdlib.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
using namespace std;
void displayGrades( struct Outer *O);
void calculateGrades(struct Outer *O);
void readGrades( struct Outer *O);
struct Inner{
int id;
string name;
double midterm1;
double midterm2;
double midtermTotal;
double lab_H;
double finalExam;
double total;
};
Inner i1;
struct Outer{
Inner data;
Outer *next;
};
struct Outer o1, o2, o3, o4;
int main()
{
readGrades(&o1);
calculateGrades(&o1);
displayGrades(&o1);
//o1.next = &o2;
/*
readGrades(&o2);
calculateGrades(&o2);
displayGrades(&o2);
//o2.next =&o3;
readGrades(&o3);
calculateGrades(&o3);
displayGrades(&o3);
//o3.next =&o4;
readGrades(&o4);
calculateGrades(&o4);
displayGrades(&o4);
//o4.next =NULL;
*/
Outer *ptro1;
ptro1 = new Outer;
Outer *ptro2;
ptro2 = new Outer;
Outer *ptro3;
ptro3 = new Outer;
Outer *ptro4;
ptro4 = new Outer;
Outer *head=ptro1;
ptro1->next = ptro2;
ptro2->next = ptro3;
ptro3->next = ptro4;
ptro4->next = NULL;
while(head!=NULL) // && i<=2)
{
cout<<"Student ID: "<<head->data.id<<endl;
cout<<"Student Midterm1: "<<head->data.midterm1<<endl;
cout<<"Student Midterm2: "<<head->data.midterm2<<endl;
cout<<"Student Labs and Homework: "<<head->data.lab_H<<endl;
cout<<"Student Final Exam: "<<head->data.finalExam<<endl;
head = head->next;
}
return 0;
}
void readGrades(struct Outer *O){
cout<<"Enter the student's id: "<<endl;
cin>>o1.data.id;
cout<<"Enter the student's midterm #1 grade: ";
cin>>o1.data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";
cin>>o1.data.midterm2;
cout<<"Enter the student's lab and homework grade: ";
cin>>o1.data.lab_H;
cout<<"Enter the student's final exam grade: ";
cin>>o1.data.finalExam;
}
void displayGrades(struct Outer *O){
cout<<"The students final grade is: ";
if(O->data.total>=90)
{
cout<<"A"<<endl;
}
else if(O->data.total<=89 && O->data.total>=80)
{
cout<<"B"<<endl;
}
else if(O->data.total<=79 && O->data.total>=70)
{
cout<<"C"<<endl;
}
else if(O->data.total<=69 && O->data.total<60)
{
cout<<"F"<<endl;
}
}
void calculateGrades(struct Outer *O){
O->data.midtermTotal=(((O->data.midterm1/50)+(O- >data.midterm2/50))/2)*35;
O->data.lab_H=(O->data.lab_H/20)*25;
O->data.finalExam=(O->data.finalExam/100)*40;
O->data.total=O->data.midtermTotal+O->data.lab_H+O->data.finalExam;
//displayGrades();
}
In the below function you need to use O as you are passing it but you are using
o1 which will overwrite every time
void readGrades(struct Outer *O)
{
cout<<"Enter the student's id: "<<endl;
cin>>O->data.id;
cout<<"Enter the student's midterm #1 grade: ";
cin>>O->data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";
cin>>O->data.midterm2;
cout<<"Enter the student's lab and homework grade: ";
cin>>O->data.lab_H;
cout<<"Enter the student's final exam grade: ";
cin>>O->data.finalExam;
}
You should create a constructor that initializes the member variables to 0. Most of compilers do not do that by default.
struct Inner{
Inner() : id(0),midterm1(0),midtermTotal(0), lab_H(0), finalExam(0), total(0)
{
}
int id;
string name;
double midterm1;
double midterm2;
double midtermTotal;
double lab_H;
double finalExam;
double total;
};

How to extract the details of the highest paid employee in files?

I am having problem in extracting the highest paid employee from empdetails.txt and finally displaying it to the user. i have completed getting the details from the user and merging the two files but for displaying highest paid using functions, i have no idea about it.
here is my code till now:
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class emp
{
int num,age;
char name[20],dep[5];
public:
void getdata()
{
cout<<"\n\n Name = ";
cin>>name;
cout<<"\n Emp Num = ";
cin>>num;
cout<<"\n Department= ";
cin>>dep;
cout<<"\n Age = ";
cin>>age;
}
void display1()
{
cout<<"\n"<<name<<"\t"<<num<<"\t"<<dep<<"\t\t"<<age;
}
};
class sal
{
float gs,ns;
public:
void getsal()
{
cout<<"\n Gross sal = ";
cin>>gs;
cout<<"\n Net sal = ";
cin>>ns;
}
void display2()
{
cout<<"\t"<<gs<<"\t"<<ns;
}
};
void display()
{
emp e;sal s;
ifstream fil1;
fil1.open("empdetails.txt",ios::in);
cout<<"\n\n Name \t Emp Num \t Dep \t Age \t Gross Sal \t Net Sal \n";
while(!fil1.eof())
{
fil1.read((char*)&e,sizeof(e));
e.display1();
fil1.read((char*)&s,sizeof(s));
s.display2();
}
}
int main()
{
int n;
emp e1;sal s1;
ofstream fil1,fil2,fil3;
fil1.open("emp.txt",ios::out);
fil2.open("sal.txt",ios::out);
fil3.open("empdetails.txt",ios::out);
cout<<"\n How many employee details do you want to enter = ";
cin>>n;
cout<<"\n Enter the deatils one by one \n";
for(int i=0;i<n;i++)
{
e1.getdata();
fil1.write((char*)&e1,sizeof(e1));
s1.getsal();
fil2.write((char*)&s1,sizeof(s1));
fil3.write((char*)&e1,sizeof(e1));
fil3.write((char*)&s1,sizeof(s1));
}
fil1.close();
fil2.close();
fil3.close();
cout<<"\n\n\t\t Merged file contents \n\n\t\t";
display();
getch();
return 0;
}
how can i make a function and what conditions to use?
You don't need a function, there's already one: std::max_element. It can figure out that you're working on class emp (the first two arguments to std::max_element. It can't figure out that you want employees sorted by salary, so that is the third argument that you have to provide: a function that takes two employees and which returns true if the first employee earns less than the second. (Sounds weird, but this allows you to use the same function for std::min_element)

C++ Classes. Values inside class don't get updated

for a class assignment I have to write a class definition. Its called Employee class, something really basic for my first c++ class.
My problem is in the first forloop, when I try to adjust the salary based on the new percentage.
The variables inside the class don't change after that. I don't know what could be wrong anymore.
The code is:
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
int emplSalary;
string employeeName;
public:
Employee();
Employee(string name,int salary);
string getName();
int getSalary();
void newSalary(int percent);
void input();
void output();
};
Employee::Employee()
{
emplSalary=0;
employeeName="";
}
Employee::Employee(string name,int sal)
{
employeeName=name;
emplSalary =sal;
}
string Employee::getName()
{
return employeeName;
}
int Employee::getSalary()
{
return emplSalary;
}
void Employee::newSalary(int percent)
{
emplSalary= emplSalary *(1+(percent/100));
cout<<"I calculated"<<endl;
/*
if(percent < 0)
{
cout<<"Invalid Percentage";
cout<<"I calculated"<<endl;
}
else
{
emplSalary= emplSalary *(1+(percent/100));
cout<<"I calculated"<<endl;
}
*/
}
void Employee::input()
{
cout << "Enter Name: ";
cin>> employeeName;
cout<<"\n";
cout<<"Enter Salary: " ;
cin>>emplSalary;
cout<<"\n";
}
void Employee::output()
{
cout << "Name: " << employeeName <<" : "<< "Salary: " << emplSalary << endl;
cout<<"\n";
}
int main()
{
const int NUMBER_EMPLOYEE =1;
Employee employees[NUMBER_EMPLOYEE];
int percent;
cout<<"Welcome to Employee program. Enter Name and Salary when prompted."<<endl;
cout<<"\n";
cout<<"\n";
for (int i=0; i<NUMBER_EMPLOYEE; i++)
{
employees[i]=Employee();
employees[i].input();
cout<<"What percentage to raise the salary: ";
cin>>percent;
employees[i].newSalary(percent);
}
for (int i=0; i<NUMBER_EMPLOYEE; i++)
{
employees[i].output();
}
return EXIT_SUCCESS;
}
and the output is:
Welcome to Employee program. Enter Name and Salary when prompted.
Enter Name:
Enter Salary:
What percentage to raise the salary: I calculated
Name: : Salary: 0
The problem is in this line:
emplSalary= emplSalary *(1+(percent/100));
Because percent is an int, you are doing all integer math.
Suppose percent was 50.
The innermost part of your expression ends up being 50/100, which is 0 in integer math.
(You intended for the result to be 0.50).
To fix this, change the type of percent to be double.
Alternately, you can change 100 to be 100.0 (making it a double):
emplSalary= emplSalary *(1+(percent/100.0));
emplSalary= emplSalary *(1+(percent/100));
This line, if your percent is less than 99, percent/100 will be zero, that's why it has no affect to your result. You may want to use double type for your emplSalary and percent.
emplSalary= emplSalary *(1+(percent/100));
You're performing integer arithmetic there (emplSalaray and percent are both of type int). This means that percent / 100 will (unless percent is greater 99) always evaluate to 0. So the equation ends up being emplSalary = emplSalary * 1.
First, note that when the original salary is zero (which is what happens when you write employees[i]=Employee() and the default constructor sets the salary to 0), raises of any percentage will always remain zero,
Second, note that dividing an int by another int will perform integer arithmetic, so the quotient gets truncated. Raises between 0 and 100 percent will therefore be rounded to 0%. Divide by 100.0 instead of 100 to resolve that problem.

Reading into dynamically created pointer to array of structs

This is a class assignment that must be done using a dynamically created array of Course. I am trying to read into each member variable inside of course inside of my for loop but I'm not really sure how to do it. I did it with my student struct but the difference in this being an array is messing me up because I'm not sure how to proceed with it.
My problem is in the readCourseArray function when trying to read in struct members. If anyone could tell me how I do that I'd be appreciative. I know using the new operator isn't ideal along with many of the pointers being unnecessary but it is just how my instructor requires the assignment to be turned in.
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string firstName, lastName, aNumber;
double GPA;
};
struct Course
{
int courseNumber, creditHours;
string courseName;
char grade;
};
Student* readStudent();
Course* readCourseArray(int);
int main()
{
int courses = 0;
Student *studentPTR = readStudent();
Course *coursePTR = readCourseArray(courses);
delete studentPTR;
delete coursePTR;
system ("pause");
return 0;
}
Student* readStudent()
{ Student* student = new Student;
cout<<"\nEnter students first name\n";
cin>>student->firstName;
cout<<"\nEnter students last name\n";
cin>>student->lastName;
cout<<"\nEnter students A-Number\n";
cin>>student->aNumber;
return student;
}
Course* readCourseArray(int courses)
{
cout<<"\nHow many courses is the student taking?\n";
cin>>courses;
const int *sizePTR = &courses;
Course *coursePTR = new Course[*sizePTR];
for(int count = 0; count < *sizePTR; count++) //Enter course information
{
cout<<"\nEnter student "<<count<<"'s course name\n";
cin>>coursePTR[count]->courseName>>endl;
cout<<"\nEnter student "<<count<<"'s course number\n";
cin>>coursePTR[count]->courseNumber;
cout<<"\nEnter student "<<count<<"'s credit hours\n";
cin>>coursePTR[count]->creditHours;
cout<<"\nEnter student "<<count<<"'s grade\n";
cin>>coursePTR[count]->grade>>endl;
}
return coursePTR;
}
Array subscript operator return an element of the array.
coursePTR[count] is equivalent to incrementing the pointer to the start of array and dereferencing the result, like: *(coursePTR + count). What you get is an object (or a reference to one) of type Course. So you'll need to use 'dot' operator, not 'arrow' operator to access the elements:
cin >> coursePTR[count].creditHours;
You've got another error:
cin >> coursePTR[count].courseName >> endl;
^^^^
This won't compile. endl can only be used on output streams.
Course* readCourseArray(int &courses); // Update the definition to pass "courses" by reference.
Course* readCourseArray(int &courses) // Pass the courses by reference so that your main() has the value updated.
{
cout<<"\nHow many courses is the student taking?\n";
cin>>courses;
/*
You don't need this line.
*/
// const int *sizePTR = &courses;
/*
You've allocated space for "courses" no. of "Course" objects.
Since this is essentially an array of "Course" object, you
just have to use the "." notation to access them.
*/
Course *coursePTR = new Course[courses];
/*
"endl" cannot be used for input stream.
*/
for(int count = 0; count < courses; count++) //Enter course information
{
cout<<"\nEnter student "<<count<<"'s course name\n";
cin>>coursePTR[count].courseName;
cout<<"\nEnter student "<<count<<"'s course number\n";
cin>>coursePTR[count].courseNumber;
cout<<"\nEnter student "<<count<<"'s credit hours\n";
cin>>coursePTR[count].creditHours;
cout<<"\nEnter student "<<count<<"'s grade\n";
cin>>coursePTR[count].grade;
}
return coursePTR;
}