I am a beginner. I am supposed to use a switch structure. I wrote
the code below, which works great, but I can't come up with a
reasonable usage of the switch structure for this program. Any ideas
would be appreciated.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string isAcceptable(int &est, int &act);
int main ()
{
int estTime, actTime;
cout << "\n\n What is the estimated flight time? ";
cin >estTime;
cout << "\n\n What is the actual flight time? ";
cin >actTime;
cout << isAcceptable(estTime, actTime) << endl;
}
string isAcceptable(int &est, int &act)
{
int margin, diff, x;
//acceptable error margin in minutes table
if (est >= 0 && est <= 29)
margin = 1;
else if (est >= 30 && est <= 59)
margin = 2;
else if (est >= 60 && est <= 89)
margin = 3;
else if (est >= 90 && est <= 119)
margin = 4;
else if (est >= 120 && est <= 179)
margin = 6;
else if (est >= 180 && est <= 239)
margin = 8;
else if (est >= 240 && est <= 359)
margin = 13;
else if (est >= 360)
margin = 17;
// diff between the est & act times
if (est act)
diff = (est - act);
else
diff = (act - est);
//acceptable differences are less than or equal to the
//margin of error for the estimated time slot
if (diff <= margin)
return "\n\t The flight time is acceptable.";
else{ //excedes the margin of error
if (diff margin)
x = (diff - margin);
else
x = (margin - diff);
//convert x int to char
std::string s;
std::stringstream out;
out << x;
s = out.str();
return "\n\t The flight delay is not acceptable."
"\n\t The estimate is off by: " + s + " minutes.";
}
}