Oop Lab 4 f200446
Oop Lab 4 f200446
#include<iostream>
#include<string>
using namespace std;
//defining structure
struct StudentData
{
string Fname;
string Lname;
int score;
char grade;
};
//defining functions
void IO();
char Grade(int);
void highestScore(int);
void printHighestscore(int);
void Record();
StudentData s[20];
if (s[i].score >= 0 && s[i].score <= 100)//check weither the test socre are in
the range of 0~100
s[i].grade = Grade(s[i].score); //calls the grade function to assign the
grade
else
{
do {
cout << " Entered invalid test scores" << endl;
cout << " AGAIN Enter the test score" << i + 1 << " :";
cin >> s[i].score;
} while (s[i].score < 0 || s[i].score > 100);
s[i].grade = Grade(s[i].score);
}
cout << endl;
highestScore(i);
}
}
char Grade(int num)//function to assign the grade
{
char grade;
if (num >= 90)
grade = 'A';
else if (num >= 80)
grade = 'B';
else if (num >= 70)
grade = 'C';
else if (num >= 60)
grade = 'D';
else
grade = 'F';
return grade;
}
void highestScore(int j)
{
if (j == 0)
{
highest_score[0] = s[j].score;
highest = s[j].score;
}
else if (s[j].score >= highest)
{
highest_score[j] = s[j].score;
highest = s[j].score;
}
}
void printHighestscore(int h)
{
cout << "Higest test Score= " << h << endl;
cout << "STUDENT(s) got the Highest score are" << endl;
}
}
}
void Record()
{
cout << "LAST NAME" << " First Name" << " \t\tTEST SCORE" << "\tGRADE" << endl;
for (int i = 0; i < 5; i++)
{
cout << s[i].Lname << ", " << s[i].Fname << " \t\t\t " << s[i].score << "
\t\t\t" << s[i].grade << endl;
}
}
Question 2:
#include<iostream>
#include <iomanip>
using namespace std;
//defining structure
struct machine
{
string name;
double cost;
int no;
};
//defining functions
void input(machine* ptr, int size);
void Output(machine* ptr, int size);
//main function
int main()
{
int size = 4;
machine* ptr = new machine[4];
int choose;
//function call
input(ptr, size);
double cash;
double remaining = 0;
do
{
system("cls");
Output(ptr, size);
cout << "PRESS 1 to Select COLA" << endl;
cout << "PRESS 2 to Select ROOT BEAR" << endl;
cout << "PRESS 3 to Select GRAPE SODA" << endl;
cout << "PRESS 4 to Select CREAME SODA" << endl;
cout << "PRESS 0 TO EXIT" << endl;
cin >> choose;
if (choose == 1 || choose == 2)
{
cout << "Enter Money u have" << endl;
cin >> cash;
while (cash < 0 || cash>1)
{
cout << "INVALID INPUT" << endl;
cout << "Enter Money u have AGAIN !" << endl;
cin >> cash;
}
if (cash < .75)
{
cout << "You Don't have enough money" << endl;
}
else
{
remaining = cash - ptr[choose - 1].cost;
cout << "YOU HAVE PURCHASED " << ptr[choose - 1].name <<
endl;
if (remaining < .75)
{
cout << "YOU DON'T HAVE MORE MONEY TO BUY DRINK" <<
endl;
break;
}
}
if (choose == 3 || choose == 4)
{
cout << "Enter Money u have" << endl;
cin >> cash;
while (cash < 0 || cash>1)
{
cout << "INVALID INPUT" << endl;
cout << "Enter Money u have AGAIN !" << endl;
cin >> cash;
}
if (cash < .80)
{
cout << "You Don't have enough money" << endl;
}
else
{
cout << "YOU HAVE PURCHASED " << ptr[choose -
1].name << endl;
remaining = cash - ptr[choose - 1].cost;
if (remaining < .80)
{
cout << "YOU DON'T HAVE MORE MONEY TO BUY
DRINK" << endl;
break;
}
}
}
}
} while (choose != 0);
cout << "REMAINING AMOUNT = " << remaining << endl;
system("pause");
return 0;
}
//storing data
void input(machine* ptr, int size)
{
ptr[0] = { "COLA",.75,20 };
ptr[1] = { "ROOT BEAR",.75,20 };
ptr[2] = { "Grape SODA",.80,20 };
ptr[3] = { "CREAM SODA",.80,20 };
}
//displaying output
void Output(machine* ptr, int size)
{
cout << "DRINK NAME" << "COST" << "NUMBER IN MACHINE" << endl;
for (int i = 0; i < size; i++)
{
cout << left << ptr[i].name << right << fixed << setprecision(2) <<
ptr[i].cost << ptr[i].no << endl;
}
Question 3:
#include<iostream>
#include<fstream>
#include<string>
struct matrix
{
int rows, cols, *array;
};
using namespace std;
int main()
{
matrix m;
m.rows = 0;
m.cols = 0;
int ch;
string n;
ifstream file;
file.open("myfile.txt");
while (!file.eof())
{
file >> ch;
m.cols++;
}
file.close();
file.open("myfile.txt");
while (!file.eof())
{
getline(file, n);
m.rows++;
}
file.close();
file.open("myfile.txt");
int size = m.cols;
m.cols /= m.rows;
m.array = new int[size];
for (int i = 0; i < size; i++)
file >> m.array[i];
cout << "***Matrix***\n";
for (int i = 0; i < size; i++)
{
cout << m.array[i] << " ";
if ((i + 1) >= m.cols && (i + 1) % m.cols == 0)
cout << endl;
}
file.close();
delete[]m.array;
m.array = NULL;
cout << "Rows : " << m.rows << endl;
cout << "Columns : " << m.cols << endl;
system("pause");
return 0;
}
Question 4:
#include<iostream>
#include<cstring>
#include<fstream>
#include<conio.h>
using namespace std;
//funtions
void menu();
void option();
void add();
void update();
void display();
void store();
//structure
struct basketball {
string name, sr;
int score, runs, n;
char x;
};
int i = 0;
basketball player[10];
basketball ch;
//main function
int main()
{
menu();
system("pause");
return 0;
}
//funtion for display menu
void menu()
{
system("cls");
cout << "\n";
cout << endl << endl;
cout << "Management" << endl << endl;
cout << endl << endl;
cout << "1_Adding players" << endl;
cout << "2_Update Data" << endl;
cout << "3_Store data" << endl;
cout << "4_Display data" << endl;
cout << "5_Close The System" << endl;
option();
}
//funtion for receiving user choice
void option()
{
cout << "Enter your choice (1--7): ";
cin >> ch.n;
switch (ch.n)
{
case(1):
{
add();
break;
}
case(2):
{
update();
break;
}
case(3):
{
store();
break;
}
case(4):
{
display();
break;
}
case(5):
{
exit(EXIT_SUCCESS);
break;
}
default:
{
cout << "Invalid Option";
exit(EXIT_SUCCESS);
break;
}
}
}
void add() {
system("cls");
cout << endl << endl;
cout << "ADD PLAYER" << endl << endl;
cout << endl << endl;
for (; i < 10; )
{
}
//funtion for input
void update()
{
system("cls");
cout << "UPDATE PLAYER" << endl << endl;
cout << "Enter Name Of book" << endl;
cin >> ch.sr;
for (int i = 0; i < 10; i++)
{
if (ch.sr == player[i].name)
{
cout << "Enter Name of player : ";
cin >> player[i].name;
cout << "Enter runs : ";
cin >> player[i].score;
cout << "Enter Author of the book : ";
cin >> player[i].runs;
cout << "\n\tPLAYER UPDATED Successfully!";
break;
}
else
{
cout << "\n\tPLAYER Does Not Found!\a";
}
}
cout << "Press any key to go back to menu." << endl;
ch.x = _getch();
menu();
}
//funtion for displaying output
void display()
{
system("cls");
cout << "PLAYERS DATA" << endl << endl;
for (int i = 0; i < 10; i++)
{
cout << "PLAYER " << i + 1 << " DATA" << endl;
cout << "Name: " << player[i].name << endl;
cout << "Homeruns: " << player[i].score << endl;
cout << "Total hits: " << player[i].runs << endl;
}
cout << "Press any key to go back to menu." << endl;
ch.x = _getch();
menu();
}
//funtion for file handling storing data
void store()
{
ofstream write;
write.open("players.txt");
for (int i = 0; i < 10; i++)
{
write << "PLAYER " << i + 1 << " DATA" << endl;
write << "Name: " << player[i].name << endl;
write << "Homeruns: " << player[i].score << endl;
write << "Total hits: " << player[i].runs << endl;
}
write.close();
cout << "DATA HAS BEEN STORED" << endl;
cout << "Do you want to open file? : " << endl;
cin >> ch.x;
if (ch.x == 'y')
{
system("players.txt");
cout << "Press any key to go back to menu." << endl;
ch.x = _getch();
menu();
}
else
{
cout << "Press any key to go back to menu." << endl;
ch.x = _getch();
menu();
}
}
Question 5
#include <iostream>
#include <string>
using namespace std;
//defining structure
struct employee {
int Emp_no;
float basic_pay;
float house_rent;
float medical_allow;
float conveyance_allow;
float net_pay;
};
//main fuction
int main()
{
//structure variable
employee emp;
cout << "Enter Employee number" << endl;
cin >> emp.Emp_no;
cout << "Enter Basic Pay " << endl;
cin >> emp.basic_pay;
//computing house rent
emp.house_rent = (emp.basic_pay * 45 / 100);
//computing medical allowance
emp.medical_allow = (emp.basic_pay * 5 / 100);
//computing conveyance allowance
emp.conveyance_allow = (emp.basic_pay * 10 / 100);
//computing net pay
emp.net_pay = emp.basic_pay + emp.conveyance_allow + emp.house_rent +
emp.medical_allow;
//outputs
cout << "House rent Allowance : " << emp.house_rent << endl;
cout << "Medical Allowance : " << emp.medical_allow << endl;
cout << "Conveyance Allowance : " << emp.conveyance_allow << endl;
cout << "Net pay of Employee : " << emp.net_pay << endl;
system("pause");
return 0;
}