Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question1) Using C++, Splashkit and Visual studio code answer the question. Exam

ID: 3746840 • 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)?

Explanation / Answer

I have written the all three required methods and demonstrated by calling them in main

save the below code as validate.cpp or any name that u want

#include <iostream>

#include <string>

#include<string.h>

using namespace std;

//defined the method prototype

double read_double();

double read_double_range(double, double);

bool read_boolean();

double read_double() {

double d;

while (1) {

cout << "Enter double value : ";

cin >> d;

//if user input invalid input then it will get fail

if (cin.fail()) {

cout << "invalid value ! " << endl;

// clearing the cin buffer

cin.clear();

cin.ignore(999, ' ');

} else {

//if value is correct returning value

return d;

}

}

}

//checking the value in range method

double read_double_range(double d1, double d2) {

cout << "Enter value in range " << d1 << " -> " << d2 << endl;

while (1) {

//getting the double from by calling read_double method

double val = read_double();

// checking the range

if (d1 < val && val < d2)

return val;

else {

cout << "Number not in range " << endl;

}

}

}

bool read_boolean() {

string val;

while (1) {

cout << "Enter [yes/no] ? : ";

cin >> val;

//trimming the whitespaces

val = val.erase(0, val.find_first_not_of(' '));

val = val.erase(val.find_last_not_of(' ') + 1);

//converting to lower case

for(int i=0;i<val.length();i++){

val[i] = tolower(val[i]);

}

//comparing the value and returning boolean value

if(val=="yes" || val == "y")

return true;

else if(val=="no"|| val=="n"){

return false;

}

else{

cout<<"Invalid value! ";

}

}

}

int main(int argc, char const *argv[]) {

cout<<"Read Double method"<<endl;

read_double();

cout<<" Read Double method in range ";

read_double_range(0.0, 1.0) ;

cout<<" Read boolean method : "<<endl;

read_boolean();

return 0;

}//end of code

//OUTPUT

Read Double method
Enter double value : t
invalid value !
Enter double value : ds
invalid value !
Enter double value : qwe
invalid value !
Enter double value : 78.1

Read Double method in range
Enter value in range 0 -> 1
Enter double value : 3
Number not in range
Enter double value : d
invalid value !
Enter double value : e
invalid value !
Enter double value : .4

Read boolean method :
Enter [yes/no] ? : T
Invalid value!
Enter [yes/no] ? : U
Invalid value!
Enter [yes/no] ? : Ye
Invalid value!
Enter [yes/no] ? : YeS

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote