468,484 Members | 1,582 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Need ideas on changing to switch structure

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.";
}
}
Mar 9 '08 #1
0 950

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by John N. | last post: by
3 posts views Thread by moondaddy | last post: by
6 posts views Thread by Darian | last post: by
1 post views Thread by CptDondo | last post: by
reply views Thread by Dale | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.