How would I change the food choices in the code to them being random? Rather tha
ID: 3779002 • Letter: H
Question
How would I change the food choices in the code to them being random? Rather than having each case correspond to the assigned number.:
#include<iostream>
#include<conio.h>
#include<istream>
#include<stdlib.h>
#include<string>
using namespace std;
int main() {
int mood;
int isvalidReal();
cout << "1.Happy 2.Depressed 3.Peaceful 4.Agitated 5.Exit" << endl;
cout << "Hungry? Enter the number corresponding to your mood." << endl;
while (true) // never ending loop which break untill user enters valid input
{
mood = isvalidReal();
switch (mood) {
case 1:
cout << "Enjoy some food at HipHop Fish & Chicken" << endl;
break;
case 2:
cout << "Go to Chick-fil-A?" << endl;
break;
case 3:
cout << "I recommend Five Guy Burgers and Fries" << endl;
break;
case 4:
cout << "Try Potbelly Sandwich Shop" << endl;
break;
case 5:
cout << "Goodbye" << endl;
break;
default:
cout << "Enter correct Mood" << endl;
continue; // continue to while loop
}
break; // break and exit the loop
}
_getch();
return 0;
}
int isvalidReal()
{
bool isvalidInt(string);
bool notanint = true;
string svalue;
while (notanint)
{
try
{
cin >> svalue;
if (!isvalidInt(svalue)) throw svalue;
}
catch (string e)
{
cout << "Please enter a Mood: ";
continue;
}
notanint = false;
}
return atoi(svalue.c_str());
}
bool isvalidInt(string str)
{
int start = 0;
int i;
bool valid = true;
bool sign = false;
if (int(str.length()) == 0) valid = false;
if (str.at(0) == '-' || str.at(0) == '+')
{
sign = true;
start = 1;
}
if (sign && int(str.length()) == 1) valid = false;
i = start;
while (valid && i< int(str.length()))
{
if (!isdigit(str.at(i))) valid = false;
i++;
}
return valid;
}
Explanation / Answer
Program:
#include <iostream>
#include <istream>
#include <stdlib.h>
#include <string>
using namespace std;
int main() {
int mood;
int isvalidReal();
cout << "1.Happy 2.Depressed 3.Peaceful 4.Agitated 5.Exit" << endl;
cout << "Hungry? Enter the number corresponding to your mood." << endl;
while (true) // never ending loop which break untill user enters valid input
{
mood = isvalidReal();
if (mood==1)
cout << "Enjoy some food at HipHop Fish & Chicken" << endl;
else if(mood==2)
cout << "Go to Chick-fil-A?" << endl;
else if(mood==3)
cout << "I recommend Five Guy Burgers and Fries" << endl;
else if(mood==4)
cout << "Try Potbelly Sandwich Shop" << endl;
else if(mood==5)
cout << "Goodbye" << endl;
else
cout << "Enter correct Mood" << endl;
continue; // continue to while loop
}
return 0;
}
int isvalidReal()
{
bool isvalidInt(string);
bool notanint = true;
string svalue;
while (notanint)
{
try
{
cin >> svalue;
if (!isvalidInt(svalue)) throw svalue;
}
catch (string e)
{
cout << "Please enter a Mood: ";
continue;
}
notanint = false;
}
return atoi(svalue.c_str());
}
bool isvalidInt(string str)
{
int start = 0;
int i;
bool valid = true;
bool sign = false;
if (int(str.length()) == 0) valid = false;
if (str.at(0) == '-' || str.at(0) == '+')
{
sign = true;
start = 1;
}
if (sign && int(str.length()) == 1) valid = false;
i = start;
while (valid && i< int(str.length()))
{
if (!isdigit(str.at(i))) valid = false;
i++;
}
return valid;
}
Output:
1.Happy
2.Depressed
3.Peaceful
4.Agitated
5.Exit
Hungry? Enter the number corresponding to your mood. 2
Go to Chick-fil-A?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.