In: Computer Science
In the previous verse, we asked the user to input 5 inventory items. In this verse, we need to ensure the values they entered are valid values for the two fields of each inventory item. Continue to ask the user to input the inventory id # (integer) and the price (float) for 5 inventory items. After the user enters each value, ensure the value entered is greater than 0. If the value is less than or equal to 0, then write a descriptive error message and end the program. To end a program, call the exit function and pass 0 as an argument.
The programming language is not mentioned in the problem. Hence, the chosen programming language is C++.
Screenshot of the code:
Sample Output:
//Sample Run 1:
//Sample Run 2:
//Sample Run 3:
Code To Copy:
//Include the header files.
#include <iostream>
using namespace std;
//Define the main function.
int main() {
//Declare the variables.
int inventory[5], inventory_id;
float price;
//Begin the for loop to enter the
//details for the 5 inventory items.
for(int i = 0 ; i < 5; i++)
{
//Prompt the user to enter the id of the inventory item.
cout << "For Inventory Item " << i+1 << ":" << endl;
cout << "Enter the Inventory id: ";
cin >> inventory_id;
//Check the validity of the entered id.
if(inventory_id <= 0)
{
//If it is less than or equal to 0, display the
//error message.
cout << "Invalid Id!!!" << endl;
//End the program with exit function passing 0.
exit(0);
}
//Prompt the user to enter the price of the
//inventory item.
cout << "Enter the Inventory price: ";
cin >> price;
//Check the validity of the price.
if(price <= 0)
{
//If it is less than or equal to 0, display
//the error message.
cout << "Invalid Price!!!" << endl;
//End the program with exit function passing 0.
exit(0);
}
}
}