Question1) Using C++, Splashkit and Visual studio code answer the question. Exam
ID: 3747386 • Letter: Q
Question
Question1) Using C++, Splashkit and Visual studio code answer the question. Example- string read_string(string prompt) { string result; write(prompt); result = read_line(); return result; } a)How would a read_double code portion be to make sure it checks if the user has entered a valid number before doing the conversion so that it does not crash when an invalid value is entered?
b) How would a read_double_range code portion be to read a number between minimum and maximum (inclusive) values? For example, read a double between 0.0 and 1.0.
c) How would a read_boolean function code portion be to ask the function "yes/no" type questions? The user should be able to type "yes" or "y" (for true) and "no" or "n" for false. (This should not be case sensitive, and should work if there are leading or trailing spaces)?
NO iostream code please.
please answer in the example formant
Explanation / Answer
I am going to answer your questions in 3 parts -
a)How would a read_double code portion be to make sure it checks if the user has entered a valid number before doing the conversion so that it does not crash when an invalid value is entered?
Answer -
Parse the given string input and check if there are numbers and 0 or one dot. If these rules aren't met, you just print an error message.
example-consider string num=33.85
read_double(string num)
{
now if num is not 0 then
search for '.' in a given input until the end of the string
if you found then its a valid decimal input otherwise print an error message.
}
b) How would a read_double_range code portion be to read a number between minimum and maximum (inclusive) values? For example, read a double between 0.0 and 1.0.
Answer -
Here we compare “number-minimum” with “maximum-number”. number is in range [minimum, maximum] if and only if number is greater than or equal to minimum and smaller than or equal to maximum.
bool read_double_range(double minimum, double maximum, double number)
{
return ((number-minimum) <= (maximum-minimum));
}
c) How would a read_boolean function code portion be to ask the function "yes/no" type questions? The user should be able to type "yes" or "y" (for true) and "no" or "n" for false.
Answer -
read_boolean function (String)
{
1) At first remove spaces from the string.
For example “n o” should be converted to “no".
Here we to traverse the string from left to right and ignore spaces while traversing. We keep track of two indexes, one for current character and other for current index in output.
So at the end we will get string "no".
2) Now convert a string to upper case either manually or ::toupper STL in c++.
So at the end we will get string "NO".
3)Now Compare string with "N" || "NO"
4)Similar for "Y" || "YES"
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.