Add code to your program from assignment 2 so that you can verify user input, an
ID: 3843815 • Letter: A
Question
Add code to your program from assignment 2 so that you can verify user input, and respond more specifically to the user's input. Produce different output messages depending on the input that the user provides.As an example, if you asked for the user's age, then based on the age, you can respond in different ways, like this:
Age Output < 0 "invalid age" >120 "invalid age" 0-2 "what a cute baby" 3-6 "Let's play hide and seek" 7-11 "Let's play minecraft" 12-17 "Let's play five nights at freddy's" 18-22 "First time voting!" 18-24 "How's school?" 31-55 "How's work?" 56-99 "How's retirement?" 100-120 "Oldest Ever!" Note that some ranges may overlap. You should include something like this (overlapping ranges) in your code; you will need to use nested if statements to handle that situation.
Respond to at least one numerical question and at least one text question. Responding to the text question will require giving the user a choice of options, and verifying that the input is one of the available options. Use a loop to keep asking the user until they give one of the options, like this:
Are you feeling happy today? (yes/no/unsure) > pretty happy Please answer with one of the available options. Are you feeling happy today? (yes/no/unsure) > Yeppers Please answer with one of the available options. Are you feeling happy today? (yes/no/unsure) > yes Great! glad you are happy.
In addition to a screenshot, you should generate an output log file. This means that for every input the program receives, that input should be sent to the output file (an echo), and that every output that the program generates should be displayed to the screen AND sent to the output file. For example, for every line that starts with cout << , you should have an identical line that starts with logfile << (or the name of your ofstream); and for every cin or getline(cin,stringVariable), you should have a corresponding logfile << which writes whatever the user provided to the log file. For simplicity and readability, it's worth using a variable to build up each output line, and then send that string variable to cout<< and logfile<<.
By building a log file in this way, you can do multiple test of input and output without having to find a way to fit them all on your screenshot. When you submit your code, provide a copy of your log file as well as a copy of your .cpp file, in order to show the test cases you used. Indicate (in your assignment file or comments) which test cases you chose and why.
ooo Fido 69% 5:15 PM urcourses.uregina.ca Question 1 97 2 4848 2 24 24/2 12 12/2 6 6/2 3 3 2 1 1/2-0 Answer: 97 1100001 b) 10100101 10100101 2 (1.27+0.2 +1.2 +0 2 0.2 +1.22+0 2 +1.2012 (128 +0+32 +0+0+4 0 112 Answer: 10100101 165 Question 2 a) float subTotal, percentTip, totalBill; total Bill (1.0 percent Tip 100.0) SubTotal b) The dodecahedron has a volume indicated by the following formula: y where a is the length of an edge in C++, the formula for this would be: float edge //edge length float dodecArea; area of the dodecahedron dodecArea (15.0+7.0 sqrt(5.e))/4.0) edge edge edge where the library "cmath would be required for the "sqrt0 function.
Explanation / Answer
Here is the code with comments and sample output for the question. Please do rate the answer if it helped. Thanks.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string choice,response,line;
int age;
ofstream logfile;
//open a logfile
logfile.open("logfile.txt");
bool valid;
if(!logfile.is_open())
{
cout<<" Could not open logfile !";
return 1; //error
}
do
{
//get a numeric input from user
line=" Enter a age : ";
cout<<line;
cin>>age;
//store in logfile the output and input
logfile<<line<<age<<endl;
//start checking various rangees for age
if(age<0)
line="Invalid age !";
else if(age<=2) //0-2
line="What a cute baby !";
else if(age<=6) //3-6
line="Let's play hide and seek";
else if(age<=11)
line="Let's play minecraft";
else if(age<=17)
line="Let's play five nights at freddy's";
else if(age<=24)
{
if(age<=22)
{
line="First time voting! ";
cout<<line;
logfile<<line;
}
line="How's school?";
}
else if(age>=31 && age<=55)
line="How's work?";
else if(age>=56 && age<=99)
line="How's retirement?";
else if(age>=100 && age<=120)
line="Oldest Ever!";
else if(age>120)
line="Invalid age!";
cout<<line;
logfile<<line<<endl;
do
{
line=" Are you feeling happy today? (yes/no/unsure) : ";
cout<<line;
cin>>response;
logfile<<line<<response<<endl;
valid=true; //lets assume user input is valid
if(response.compare("yes")==0)
line="Glad you are happy.";
else if(response.compare("no")==0)
line="Why are you sad today ? Cheer up !";
else if (response.compare("unsure")==0)
line="Why confused ?";
else
{
valid=false; //input not valid so loop should continue
line="Please answer with one of the avaiable options.";
}
cout<<line;
logfile<<line<<endl;
}while(valid==false); //continue till you get valid input
line=" Do you wish to continue? (yes/no) : ";
cout<<line;
cin>>choice;
logfile<<line<<choice<<endl;
}while(choice.compare("yes")==0); //continue as long as user input is yes
//close logfile
logfile.close();
return 0;
}
------------
contents of logfile.txt
------------------
Enter a age : -5
Invalid age !
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 0
What a cute baby !
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 3
Let's play hide and seek
Are you feeling happy today? (yes/no/unsure) : no
Why are you sad today ? Cheer up !
Do you wish to continue? (yes/no) : yes
Enter a age : 8
Let's play minecraft
Are you feeling happy today? (yes/no/unsure) : what
Please answer with one of the avaiable options.
Are you feeling happy today? (yes/no/unsure) : ya
Please answer with one of the avaiable options.
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 12
Let's play five nights at freddy's
Are you feeling happy today? (yes/no/unsure) : unsure
Why confused ?
Do you wish to continue? (yes/no) : yes
Enter a age : 20
First time voting!
How's school?
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 23
How's school?
Are you feeling happy today? (yes/no/unsure) : bye
Please answer with one of the avaiable options.
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 35
How's work?
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 60
How's retirement?
Are you feeling happy today? (yes/no/unsure) : no
Why are you sad today ? Cheer up !
Do you wish to continue? (yes/no) : yes
Enter a age : 100
Oldest Ever!
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 120
Oldest Ever!
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : yes
Enter a age : 125
Invalid age!
Are you feeling happy today? (yes/no/unsure) : yes
Glad you are happy.
Do you wish to continue? (yes/no) : no
-------------
The test cases are choosen such that each of the condition gets tested.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.