Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views4 pages

PF Theory Assignment 5

The document contains two C++ programming assignments. The first assignment involves creating a structure to store student information, calculating averages and grades, and displaying the results. The second assignment focuses on an online shop menu, allowing users to purchase items while managing inventory and calculating total sales.

Uploaded by

soban3247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

PF Theory Assignment 5

The document contains two C++ programming assignments. The first assignment involves creating a structure to store student information, calculating averages and grades, and displaying the results. The second assignment focuses on an online shop menu, allowing users to purchase items while managing inventory and calculating total sales.

Uploaded by

soban3247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Malik Soban Rabbani

Reg no: FA23-BCS-082/D


PF ASSIGNMENT NO 5.
QUESTION 1:
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


// Make structure for storing information.
struct Student {
string name;
int id;
int marks[4];
float average;
char grade;
};
// Make a function for information from user.
void getStudentInfo(Student& st) {
// Get Name
cout << "Enter student name: ";
getline(cin, st.name);
// Get ID
cout << "Enter student ID: ";
cin >> st.id;
// Get Marks
cout << "Enter student marks in 4 subjects: \n";
for (int i = 0; i < 4; i++) {
cin >> st.marks[i];
// Input validation.
while (st.marks[i] < 0 || st.marks[i] > 100) {
cout << "Invalid input. Enter a number between 0 and 100: ";
cin >> st.marks[i];
}
st.average += st.marks[i];
}
st.average /= 4;
}
// Make a function for calculating average.
float calculateAverage(int marks[]) {
float sum = 0;
for (int i = 0; i < 4; i++) {
sum += marks[i];
}
return sum / 4;
}
// Make a function for calculating garade.
char calculateGrade(float average) {
if (average >= 90) {
return 'A';
} else if (average >= 80) {
return 'B';
} else if (average >= 70) {
return 'C';
} else if (average >= 60) {
return 'D';
} else if (average >= 50) {
return 'E';
} else {
return 'F';
}
}

int main() {
Student st;
getStudentInfo(st);
st.average = calculateAverage(st.marks);
st.grade = calculateGrade(st.average);
cout << "=====================================" << endl;
cout << " >> Student Information: " << endl;
cout << "-------------------------------------" << endl;
cout << " >> Name: " << setw(15) << st.name << endl;
cout << " >> ID: " << setw(15) << st.id << endl;
cout << " >> Average: " << setw(15) << fixed << setprecision(2) <<
st.average << endl;
cout << " >> Grade: " << setw(15) << st.grade << endl;

return 0;
}

QUESTION 2:
#include <iostream>
#include <string>

using namespace std;

// Structure to represent each item in the menu


struct Menu {
string itemName;
float itemCost;
int numItemsAvailable;
};
// List of available items in the shop
Menu itemList[5] = {
{"Shirt", 30.0, 5},
{"Pant", 50.50, 15},
{"Jacket", 75.25, 10},
{"Shoe", 20.0, 25},
{"Scarf", 10.15, 30},
};

// Function to display the available items in the menu


void displayMenu(Menu itemsList[], int numItems) {
cout << "Welcome to our online shop! Here are the items available for
purchase:\n";
for (int i = 0; i < numItems; i++) {
cout << i + 1 << ". " << itemsList[i].itemName << " - $ " <<
itemsList[i].itemCost << " (" << itemsList[i].numItemsAvailable << " available)\
n";
}
cout << "6. Quit.\n";
}

// Function to handle the purchase of items


void purchaseItems(Menu itemsList[], int numItems) {
float totalCost = 0; // Variable to track the total cost of purchased items
bool validAmount = false;

// Loop to allow multiple item purchases until the user decides to quit
while (true) {
int itemChoice;
cout << "Enter the number of the item you would like to purchase (or 6 to
finish): ";
cin >> itemChoice;

if (itemChoice == 6) {
return;
} else if (itemChoice < 1 || itemChoice > numItems) {
cout << "Invalid choice. Please enter a number from 1 to " <<
numItems << ".\n";
continue;
} else if (itemsList[itemChoice - 1].numItemsAvailable == 0) {
cout << "Sorry, that item is sold out.\n";
continue;
}

int numPurchased;
while (!validAmount) {
cout << "Enter the quantity you would like to purchase: ";
cin >> numPurchased;
if (numPurchased <= 0 || numPurchased > itemsList[itemChoice -
1].numItemsAvailable) {
cout << "Invalid quantity. Please enter a valid quantity.\n";
} else {
validAmount = true;
}
}

// Calculate the total cost and update the available items


totalCost += numPurchased * itemsList[itemChoice - 1].itemCost;
itemsList[itemChoice - 1].numItemsAvailable -= numPurchased;

cout << "You purchased " << numPurchased << " " << itemsList[itemChoice -
1].itemName << "(s) for $ " << (numPurchased * itemsList[itemChoice -
1].itemCost) << ".\n";
cout << "Your total cost is $ " << totalCost << ".\n";

validAmount = false;
}
}

// Function to calculate total sales


float calculateTotalSales(Menu itemsList[], int numItems) {
float totalSales = 0;
for (int i = 0; i < numItems; i++) {
totalSales += (itemsList[i].numItemsAvailable * itemsList[i].itemCost);
}
return totalSales;
}

int main() {
float totalSales = 0;
int itemchoice;
// Loop to continuously display the menu and handle purchases until the user
decides to quit
do {
displayMenu(itemList, 5);
purchaseItems(itemList, 5);
} while (itemchoice == 6); // This loop runs infinitely as itemchoice is
never updated

// Calculate and display the total sales


totalSales = calculateTotalSales(itemList, 5);
cout << "Total sales: $ " << totalSales << "\n\n";

return 0;
}

You might also like