Implement the following logic in C++, Use appropriate data types. Data types are
ID: 3760792 • Letter: I
Question
Implement the following logic in C++, Use appropriate data types. Data types are represented as either numeric (num) or string.
start
string name
string address
num item //use int
num quantity
num price //use double as data type
num SIZE = 6
num VALID_ITEM [SIZE] = 106, 108, 307, 405, 457, 688 //use int as data type
num VALID_ITEM_PRICE [SIZE] = 0.59, 0.99, 4.50, 15.99, 17.50, 39.00 //use double as data type
num i
bool foundIt = false
string MSG_YES = “Item available”
string MSG_NO = “Item not found” get name, address, item, quantity
i = 0
while i < SIZE
if item == VALID_ITEM [i] then
foundIt = true
price = VALID_ITEM_PRICE [i]
endif
i = i + 1
endwhile
if foundIt == true then
print MSG_YES
print quantity, “ at “ , price, “ each”
print “Total “, quantity * price
else
print MSG_NO
endif
stop
Explanation / Answer
/* Please note in proper C++ varriable size array declaration is not valid. So "int VALID_ITEM [SIZE] = 106, 108, 307, 405, 457, 688 " declaration like ths is not valid. So either we can use dynamic array declaration or can omit the size in brackets like ths
int valid_item[]={106,108,307,405,457,688}; */
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name,address;
int item,quantity;
double price;
int size=6;
int VALID_ITEM[]={106,108,307,405,457,688};
double VALID_ITEM_PRICE[]={0.59,0.99,4.50,15.99,17.50,39.00};
int i;
bool foundIt=false;
string MSG_YES="Item available";
string MSG_NO = "Item not found" ;
cout<<"input name"<<endl;
cin>>name;
cout<<"input Address"<<endl;
cin>>address;
cout<<"input Item"<<endl;
cin>>item;
cout<<"input Quantity"<<endl;
cin>>quantity;
i = 0;
while(i <size){
if (item ==VALID_ITEM[i]) {
foundIt = true;
price = VALID_ITEM_PRICE[i];
}
i = i + 1;
}
if (foundIt == true){
cout<<MSG_YES<<endl;
cout<<quantity<<" at "<<price<<"each"<<endl;
cout<<"Total"<<quantity * price;}
else
cout<<MSG_NO;
}