C++
#include <iostream>
using namespace std;
bool checkinventoryid(int id)
{
return id > 0;
}
bool checkinventoryprice(float price)
{
return price > 0;
}
void endProgram()
{
system("pause");
exit(0);
}
void errorID(string str)
{
cout << "Invalid Id!!! " << str << " should be greater than 0" << endl;
}
void errorPrice(string str)
{
cout << "Invalid Price!!!" << str << " should be greater than 0" << endl;
}
int inputId()
{
int inventoryitem = 0;
cout << "Enter the Inventory id: ";
cin >> inventoryitem;
while (!checkinventoryid(inventoryitem))
{
errorID("Inventory id");
cout << "Enter the Inventory id: ";
cin >> inventoryitem;
}
return inventoryitem;
}
float inputPrice()
{
float price = 0.0;
cout << "Enter the Inventory price: ";
cin >> price;
while (!checkinventoryprice(price))
{
errorPrice("Inventory price");
cout << "Enter the Inventory price: ";
cin >> price;
}
return price;
}
void display(int id, float price)
{
cout << "------------\n";
cout << "ID: " << id << endl;
cout << "Price: " << price << endl;
}
int main()
{
for (int i = 0; i < 5; i++)
{
int inventoryitem = inputId();
float price = inputPrice();
display(inventoryitem, price);
}
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
class InventoryItem
{
private :
int id;
float price;
public :
InventoryItem()
{
this->id=0;
this->price=0.0;
}
InventoryItem(int id,float price)
{
this->id=id;
this->price=price;
}
int getId()
{
return id;
}
float getPrice()
{
return price;
}
void setId(int id)
{
this->id=id;
}
void setPrice(float price)
{
this->price=price;
}
};
bool checkinventoryid(int id)
{
return id > 0;
}
bool checkinventoryprice(float price)
{
return price > 0;
}
void endProgram()
{
system("pause");
exit(0);
}
void errorID(string str)
{
cout << "Invalid Id!!! " << str << " should be greater than 0" << endl;
}
void errorPrice(string str)
{
cout << "Invalid Price!!!" << str << " should be greater than 0" << endl;
}
int inputId()
{
int inventoryitem = 0;
cout << "Enter the Inventory id: ";
cin >> inventoryitem;
while (!checkinventoryid(inventoryitem))
{
errorID("Inventory id");
cout << "Enter the Inventory id: ";
cin >> inventoryitem;
}
return inventoryitem;
}
float inputPrice()
{
float price = 0.0;
cout << "Enter the Inventory price: ";
cin >> price;
while (!checkinventoryprice(price))
{
errorPrice("Inventory price");
cout << "Enter the Inventory price: ";
cin >> price;
}
return price;
}
void display(InventoryItem inv[],int size)
{
for(int i=0;i<size;i++)
{
cout << "------------\n";
cout << "ID: " << inv[i].getId() << endl;
cout << "Price: $" << inv[i].getPrice() <<
endl;
}
}
int main() {
const int size=5;
InventoryItem* inv=new InventoryItem[size];
for (int i = 0; i < 5; i++)
{
cout<<"Item#"<<i+1<<":"<<endl;
int invenitem = inputId();
float price = inputPrice();
InventoryItem in(invenitem,price);
inv[i]=in;
}
display(inv,size);
return 0;
}
___________________________
Output:
_______________Could you plz rate me well.Thank
You
C++ #include <iostream> using namespace std; bool checkinventoryid(int id) { return id > 0; }...
#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...
#include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int lab[4]; myPtr = new int *[row]; for(int i = 0; i < row; i++) myPtr[i] = new int[lab[i]]; for(int i = 0; i<row ; i++) if(myPtr[i] == 0) cout<<"empty"; return myPtr; } void getinput(int ID,int &Station,int &labnumb) { cout<<" Enter your ID number: "<<endl; cin>>ID; cout<<" Enter your station number: "<<endl; cin>>Station; cout<<" Enter your lab number: "<<endl; cin>>labnumb; return; } void logout(int ID,int...
#include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...
#include <iostream> using namespace std; int main(void) { int SIZE; cout<<"Enter the size of the array"<<endl; cin>>SIZE; int *numlist = new int[SIZE]; // Read SIZE integers from the keyboard for (int i = 0; i<SIZE; i++ ) { cout << "Enter value #" << i+1 << ": "; cin >> numlist[i]; } // Display the numbers in a reverse order for (int i = SIZE; i > 0; i--...
#include "stdafx.h" #include <iostream> using namespace std; class dateType { private: int dmonth; int dday; int dyear; public: void setdate (int month, int day, int year); int getday()const; int getmonth()const; int getyear()const; int printdate()const; bool isleapyear(int year); dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) { int numofdays; if (year<=2008) { dyear=year;...
Here is the code from the previous three steps:
#include <iostream>
using namespace std;
class Student
{
private:
//class variables
int ID;
string firstName,lastName;
public:
Student(int ID,string firstName,string lastName)
//constructor
{
this->ID=ID;
this->firstName=firstName;
this->lastName=lastName;
}
int getID() //getter method
{
return ID;
}
virtual string getType() = 0; //pure virtual function
virtual void printInfo() //virtual function to print basic details
of a student
{
cout << "Student type: " << getType() <<
endl;
cout << "Student ID: " << ID...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
This program illustrates dynamic variables #include <iostream> using namespace std; int main () { int *p1; p1 = new int; // Variables created using the new operator are called dynamic variables cout << "Enter an integer \n"; cin >> *p1; *p1 = *p1 + 7; cout << << "Your input + 7 = " << *p1 << endl; delete p1; // Delete the dynamic variable p1 and return the memory occupied by p1 to...
#include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...