Skip to content

CS201 - Introduction to Programming

3 Topics 56 Posts
  • CS201 Assignment 3 Solution and Discussion

    Solved cs201 assignment 3 solution discussion spring 2020
    5
    0 Votes
    5 Posts
    2k Views
    zaasmiZ
    CS201 ASSIGNMENT 3 SOLUTION SPRING 2021 #include <iostream> using namespace std; #define PI 3.14159265 class Circle { private: double radius; public: void setRadius(); void computeAreaCirc(); Circle(); ~Circle(); }; Circle::Circle() { radius = 0.0; } void Circle::setRadius() { radius = 5.6; } void Circle::computeAreaCirc() { cout << "Area of circle is: " << PI * (radius * radius) << endl; cout << "Circumference of circle is: " << 2 * PI * radius << endl; } Circle::~Circle() { } class Rectangle { private: double length; double width; public: void setLength(); void setWidth(); void computeArea(); Rectangle(); ~Rectangle(); }; Rectangle::Rectangle() { length = 0.0; width = 0.0; } void Rectangle::setLength() { length = 5.0; } void Rectangle::setWidth() { width = 4.0; } void Rectangle::computeArea() { cout << "Area of Rectangle: " << length * width << endl; } Rectangle::~Rectangle() { } main() { cout<<"********************SCIENTIFIC CALCULATOR********************"<<endl; cout<<""<<endl; int run = 1; string option, choice; while(run) { cout << "\nOPTION 1 for computing Area and Circumference of the circle" << endl; cout << "OPTION 2 for computing Area of the Rectangle" << endl; cout << "Select your desired option(1-2): "; cin >> option; if(option == "1") { Circle nCircle; nCircle.setRadius(); nCircle.computeAreaCirc(); cout << "Do you want to perform anyother calculation(Y/N):"; cin >> choice; if(choice == "Y" || choice == "y") { continue; } else { break; } } else if(option == "2") { Rectangle nRectangle; nRectangle.setLength(); nRectangle.setWidth(); nRectangle.computeArea(); cout << "Do you want to perform anyother calculation(Y/N):"; cin >> choice; if(choice == "Y" || choice == "y") { continue; } else { break; } } else { cout << "Invalid Option!, Option should be from (1-2)" << endl; } } }
  • CS201 Assignment 3 Solution and Discussion

    cs201 assignment 3 fall 2020 assignment
    2
    0 Votes
    2 Posts
    2k Views
    zaasmiZ
    @zaasmi said in CS201 Assignment 3 Solution and Discussion: Re: CS201 Assignment 3 Solution and Discussion Assignment No. 3 Semester: Fall 2020 CS201 – Introduction to Programming Total Marks: 20 Due Date: 29-01-2021 Instructions Please read the following instructions carefully before submitting assignment: It should be clear that your assignment will not get any credit if: o Assignment is submitted after due date. o Submitted assignment does not open or file is corrupt. o Assignment is copied (From internet/students). o Assignment is not in .cpp format. Software allowed to develop Assignment Dev C++ Objectives: In this assignment, the students will learn: • Use of class in programming • How to deal with a file in programming • How to implement switch statement for Class based functions. ABC bakery is using a console based inventory management system for the receipt generation purposes. Console application will help the cashier in calculating total price of each item with respect to its price. Menu list will provide Add an item option to take data about an inventory item, include Item Id, Item name, price and quantity. Relevant data of all items will be displayed on screen with the help of menu option. Quantity amount of items will changeable if user wants to add quantity of an item. Problem Statement Write a C++ program to manage the inventory item using your knowledge about file handling and classes. User will manage details of an Inventory item using menu list that will provide three options: ENTER CHOICE ADD AN INVENTORY ITEM DISPLAY FILE DATA INCREASE QUANTITY Prompt will show to the user for continue the program after dealing with each option, until user will press a key other than ‘y’. Instructions to write C++ program: You will use class “Inventory” to declare inventory data and info Make “Inventory.txt” file to save inventory item record “Inventory.txt” file will delete each time when the program will run “ERROE IN OPENING FILE” will be shown if user not press ‘1’ when program will execute first time. You will use switch statement to handle different conditions and to perform different actions based on the different actions, that is, choice 1, 2, and 3. Code structure [ Demonstration]: You will be using the following class and other functions to develop the assignment: class Inventory { private: int itemID; char itemName[20]; float itemPrice; float quantity; float totalPrice; public: void readItem(); void displayItem(); int getItemID() ; float getPrice() ; float getQuantity() ; void updateQuantity(float q); }; //Deleting existing file void deleteExistingFile(){--------} //Appending item in file void appendToFille(){------------} //Displaying items void displayAll(){------------} //Increasing Quantity of item void increaseQuanity(){------------} Program Output: User’s prompt when program will execute for the first time. [image: Rokeghr.png] When user press ‘1’ , It will take data about an inventory item as an input from the user. [image: wryzxvA.png] When user press ‘2’, it will read data from “Inventory.txt” file and display record of all inventory items on the screen [image: hxs7hvH.png] If user press ‘3’, it will ask to enter Item id against which user want to increase item quantity. [image: tDlho0s.png] Now, when the user will press ‘2’, the inventory item record will be shown as: [image: hQCSrHs.png] Assignment#3 covers course contents from lecture 17 to 30. Best of Luck! #include<iostream> #include<fstream> #include<stdio.h> using namespace std; class Inventory { private: int itemID; char itemName[20]; float itemPrice; float quantity; float totalPrice; public: void readItem(); void displayItem(); int getItemID() { return itemID;} float getPrice() { return itemPrice;} float getQuantity() { return quantity;} void updateQuantity(float q) { quantity=q; totalPrice = (itemPrice*quantity); } }; // Getting Item void Inventory::readItem(){ cout << "Please enter item id: "; cin >> itemID; cout << "Please enter item name: "; cin.ignore(1); cin.getline(itemName,20); cout << "Please enter price: "; cin >> itemPrice; cout << "Please enter quantity: "; cin >> quantity; totalPrice = (itemPrice*quantity); } // Displaying Item void Inventory::displayItem() { cout << "Item id:" << itemID << "\tItem name:" << itemName << "\t ItemPrice:" << itemPrice << "\t Quantity:" << quantity << "\t TotalPrice:" << totalPrice << endl; } // Deleting existing file void deleteExistingFile(){ remove("Inventory.txt"); } // Appending item in file void appendToFille(){ Inventory x; x.readItem(); ofstream file; file.open("Inventory.txt",ios::binary | ios::app); if(!file){ cout << "ERROR WHILE CREATING A FILE!\n"; return; } file.write((char*)&x,sizeof(x)); file.close(); cout<<"Inventory record(s) added sucessfully.\n"; } // Displaying items void displayAll(){ Inventory x; ifstream file; file.open("Inventory.txt",ios::binary | ios::in); if(!file){ cout<<"ERROR IN OPENING FILE \n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))) x.displayItem(); } file.close(); } // Increasing Quantity of item void increaseQuanity(){ int itemValue; int isFound=0; int q; Inventory x; cout<<"Enter item id: \n"; cin >> itemValue; ifstream fileRead; fileRead.open("Inventory.txt",ios::binary | ios::in); if(!fileRead){ cout<<"ERROR IN OPENING FILE \n"; return; } while(fileRead){ if(fileRead.read((char*)&x,sizeof(x))){ if(x.getItemID() == itemValue){ cout << "Add quantity? "; cin >> q; x.updateQuantity(x.getQuantity() + q); isFound=1; break; } } } if(isFound==0){ cout<<"Record not found!!!\n"; } fileRead.close(); deleteExistingFile(); ofstream fileWrite; fileWrite.open("Inventory.txt",ios::binary | ios::app); fileWrite.write((char*)&x,sizeof(x)); fileWrite.close(); cout << "Item Quantity updated successfully."<<endl; } int main() { char ch; do { char n; cout << "ENTER CHOICE\n" << "1. ADD AN INVENTORY ITEM\n" << "2. DISPLAY FILE DATA\n" << "3. INCREASE QUANTITY\n"; cout << "Please select a choice: "; cin >> n; switch(n) { case '1': appendToFille(); break; case '2' : displayAll(); break; case '3': increaseQuanity(); break; default : cout << "Invalid Choice\n"; break; } /* else cout<<"Enter integer value only"; } */ cout << "Do you want to continue? : "; cin >> ch; } while(ch=='Y'||ch=='y'); return 0; }
  • 0 Votes
    7 Posts
    3k Views
    zareenZ
    Ideas Solution 2: #include<iostream> #include<fstream> #include<stdio.h> using namespace std; class Employee{ private: int code; char name[20]; float salary; public: void read(); void display(); int getEmpCode() { return code;} int getSalary() { return salary;} void updateSalary(float s) { salary=s;} }; void Employee::read(){ cout<<"Enter employee code: "; cin>>code; cout<<"Enter name: "; cin.ignore(1); cin.getline(name,20); cout<<"Enter salary: "; cin>>salary; } void Employee::display() { cout<<code<<" "<<name<<"\t"<<salary<<endl; } fstream file; void deleteExistingFile(){ remove("EMPLOYEE.DAT"); } void appendToFille(){ Employee x; x.read(); file.open("EMPLOYEE.DAT",ios::binary|ios::app); if(!file){ cout<<"ERROR IN CREATING FILE\n"; return; } file.write((char*)&x,sizeof(x)); file.close(); cout<<"Record added sucessfully.\n"; } void displayAll(){ Employee x; file.open("EMPLOYEE.DAT",ios::binary|ios::in); if(!file){ cout<<"ERROR IN OPENING FILE \n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))) if(x.getSalary()>=10000 && x.getSalary()<=20000) x.display(); } file.close(); } void searchForRecord(){ Employee x; int c; int isFound=0; cout<<"Enter employee code: "; cin>>c; file.open("EMPLOYEE.DAT",ios::binary|ios::in); if(!file){ cout<<"ERROR IN OPENING FILE \n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))){ if(x.getEmpCode()==c){ cout<<"RECORD FOUND\n"; x.display(); isFound=1; break; } } } if(isFound==0){ cout<<"Record not found!!!\n"; } file.close(); } void increaseSalary(){ Employee x; int c; int isFound=0; float sal; cout<<"enter employee code \n"; cin>>c; file.open("EMPLOYEE.DAT",ios::binary|ios::in); if(!file){ cout<<"ERROR IN OPENING FILE \n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))){ if(x.getEmpCode()==c){ cout<<"Salary hike? "; cin>>sal; x.updateSalary(x.getSalary()+sal); isFound=1; break; } } } if(isFound==0){ cout<<"Record not found!!!\n"; } file.close(); cout<<"Salary updated successfully."<<endl; } void insertRecord(){ Employee x; Employee newEmp; newEmp.read(); fstream fin; file.open("EMPLOYEE.DAT",ios::binary|ios::in); fin.open("TEMP.DAT",ios::binary|ios::out); if(!file){ cout<<"Error in opening EMPLOYEE.DAT file!!!\n"; return; } if(!fin){ cout<<"Error in opening TEMP.DAT file!!!\n"; return; } while(file){ if(file.read((char*)&x,sizeof(x))){ if(x.getEmpCode()>newEmp.getEmpCode()){ fin.write((char*)&newEmp, sizeof(newEmp)); } fin.write((char*)&x, sizeof(x)); } } fin.close(); file.close(); rename("TEMP.DAT","EMPLOYEE.DAT"); remove("TEMP.DAT"); cout<<"Record inserted successfully."<<endl; } int main() { char ch; deleteExistingFile(); do{ int n; cout<<"ENTER CHOICE\n"<<"1.ADD AN EMPLOYEE\n"<<"2.DISPLAY\n"<<"3.SEARCH\n"<<"4.INCREASE SALARY\n"<<"5.INSERT RECORD\n"; cout<<"Make a choice: "; cin>>n; switch(n){ case 1: appendToFille(); break; case 2 : displayAll(); break; case 3: searchForRecord(); break; case 4: increaseSalary(); break; case 5: insertRecord(); break; default : cout<<"Invalid Choice\n"; } cout<<"Do you want to continue ? : "; cin>>ch; }while(ch=='Y'||ch=='y'); return 0; }
Reputation Earning
How to Build a $1,000/Month World CUP LIVE Matches Live Cricket Streaming
Ads
File Sharing
Stats

0

Online

3.0k

Users

2.8k

Topics

8.5k

Posts
Popular Tags
Online User
| |