Question

In: Computer Science

In the previous verse, we asked the user to input 5 inventory items. In this verse,...

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.

Solutions

Expert Solution

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);

    }

}

}


Related Solutions

Coding in C++: For this verse, you need to write a program that asks the user...
Coding in C++: For this verse, you need to write a program that asks the user to input the inventory id number (integer) and the price (float) for 5 inventory items. Once the 5 inventory items are input from the user, output the results to the screen. Please ensure you use meaningful names for your variables. If your variables are not named meaningfully, points will be deducted.
public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up,...
public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up, ‘d’ for down, ‘l’ for left or ‘r’ for up. And in case the user enters anything except one of these four letters, it will be kept asked to enter a new char until the entered char matches one of them. (in java) *note (do just complete this specific method as this is just one of the methods apart of this.)
5. Take user input and give corresponding output. User will enter a sentence. The program will...
5. Take user input and give corresponding output. User will enter a sentence. The program will output the word that appears most frequently in this sentence. If there are multiple words with same frequency, output the first of these words. Please enter a sentence: I like batman because batman saved the city many times. The most frequent word is: batman The frequency is: 2 PYTHON
Write in javaScript: User input 5 words in one input, and print out the longest word.
Write in javaScript: User input 5 words in one input, and print out the longest word.
In this assignment we will be working with both text strings and a user input integer...
In this assignment we will be working with both text strings and a user input integer array from the data segment. The goal of the assignment is to have the user choose the number of items that they want to have in an array (we will keep it a small number for now, 1-10) and then enter those values which we will store into our array. We then want to allow them to search for a specific item in the...
You have been asked to write program that allows the user to input a first name,...
You have been asked to write program that allows the user to input a first name, middle initial (without the period), and last name of a user and then display that person’s name with the first, middle initial followed by a period, and last name last. BEFORE creating the program, use structured programming principles to document how you are going to develop the program. Use Microsoft Word to complete this part of the assessment. Answer each of the following areas:...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
We are managing the inventory and one of the items is very important for us, so...
We are managing the inventory and one of the items is very important for us, so we have to calculate the best quantity to order. Consider following data related to this inventory item: -      D = quantity over 1 year = 350,000 units -      U = unit cost = 3 Euro -      WACC = 10% -      S = Order cost = 150 Euro -      Orders have to contain multiples of 500 units 1.    Draw the different variable costs in a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT