Concept explainers
A)
String object:
- The variable “char” is used to hold only the characters but the variable string is to hold the whole set of character.
- Thus, string object is used to carry set of characters and it is declared with the keyword “string” followed by the object name.
A)
Explanation of Solution
“cin” statement to read a one word string:
When a string input is read using “cin” with a “>>” operator then the “cin” reads the data passed over it and it removes the whitespace characters such as spaces, tabs and line breaks in the string.
- Once it reaches the first non-blank character then it starts reading and it stops the reading process until it reaches the next whitespace character.
“cin” statement that reads a string in one word is as follows:
//get value of the description as a one word string
cin >> description;
- The above statement is used to read the input string until it receives the whitespace and then it ignores the remaining part of the string.
Therefore, “cin >> description;” is the “cin” statement which reads a string as one word string.
B)
String object:
- The variable “char” is used to hold only the characters but the variable string is to hold the whole set of character.
- Thus, string object is used to carry set of characters and it is declared with the keyword “string” followed by the object name.
B)
Explanation of Solution
“cin” statement to read multiple words:
In C++, using the “cin” statement, the multiple words can be read through the function getline().
- This getline() function read the line from the user including both leading and embedded spaces and then stores that line into the string object.
“cin” statement that reads multiple words in a string is as follows:
/* Read a string which contains multiple words separated by blanks */
getline(cin, description);
The above line is used to get input from the user by using “getline()”; the input contains multiple words which are separated by space.
Therefore, “getline(cin, description);” is the input statement that reads multiple words in a string using the getline() function.
Want to see more full solutions like this?
Chapter 3 Solutions
Starting Out with C++: Early Objects (9th Edition)
- Create program to create a class in which first method accepts a string from the user and other prints it.arrow_forwardTrue or false: When an object is passed to a function, but the function is not supposed to change it, it is best to pass it by value.arrow_forwardThe enum class construct: Select one: a. does not require one to write anything in front of an enumeration value-name b. requires one to use the name of the enumeration, followed by "::" (colon-colon), before each name whenever the enumeration's value-name is used c. requires one to use the name of the enumeration, followed by "." (period), before each name whenever an enumeration's value-name is usedarrow_forward
- #include <string>#include <iostream>#include <vector>class Snack{public:Snack(std::string, float, bool);int set_price(float);void set_expired(bool);void set_name(std::string);std::string name() const{return name_;}float price() const{return price_;}bool expired() const{return expr_;}int how_many_for_ten();private:std::string name_;float price_;bool expr_;};void SortByPrice(std::vector<Snack>&,bool); Task: Implement a function working with Snack objects that are stored using a vector. It has the following signature: void SortByPrice(std::vector<Snack>& list_ref, bool asc). This function should sort the vector list_ref based on the price_ of the Snacks. The sorting should be done in the following order: Ascending if asc is true Descending if asc is false. In both cases if variables have the same value for price_, the sorting should be done based on the name_ in ascending order.arrow_forward#include <string>#include <iostream>#include <vector>class Snack{public:Snack(std::string, float, bool);int set_price(float);void set_expired(bool);void set_name(std::string);std::string name() const{return name_;}float price() const{return price_;}bool expired() const{return expr_;}int how_many_for_ten();private:std::string name_;float price_;bool expr_;};void SortByPrice(std::vector<Snack>&,bool); Task: Define the public function int Snack::set_price(float price) : If the value passed by price is smaller than 0 the function should return -2. If the value of price is greater or equal to 0, proceed as follows: If the value passed by price is smaller than the current value of price_, the function should assign price to price_ and return -1. If the value passed by price is larger than the current value of price_, the function should assign price to price_ and return 1. If price is equal to price_, the function should assign price to price_ and return…arrow_forwardTrue or False _(1) The expression n/3*(8+4) evaluates to 16 if n=4 ___(2) x is greater than both y and z can be written as the statement x>y&&z ; in Java. ___(3) In Java a string is automatically an object.arrow_forward
- A member of a structure or of a class is accessed using : c++ A. Comma operator B. The dot operator C. The indexing operator D. The ampersand operatorarrow_forwardA class object can encapsulate more than one [answer].arrow_forwardLanguage: C++ Also, create two hurricane objects in the main function and print the average wind speed of them. All the data members can be public.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT