Need ideas on changing to switch structure

Y

yogi_bear_79

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.";
}
}
 
S

Sam

yogi_bear_79 said:
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;

switch (est / 30) {
case 0:
case 1:
case 2:
case 3:

.. . . and all the way up to case 7, then default:.

Your homework assignment is to figure out what to do in each case, and what
to do if est is negative.



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBH1BHjx9p3GYHlUOIRAnZVAJ9F7ZCMAJAnzPrXK/Zita7kFKcCAwCfXVim
+wAvOwwOlqh1yKrTg1twJkg=
=VSQi
-----END PGP SIGNATURE-----
 
J

James Kanze

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.

Well, that's not the sort of code where I'd use a switch, to
begin with. I'd write it pretty much as you've done (except
that I'd use half open intervals in the if's, e.g.:

if ( est >= 0 && est < 30 ) {
// ...
} else if ( est >= 30 && est < 60 )
// ...

.) Except in this specific case, I'd probably use a lookup
table. At any rate, a switch is not an appropriate control
structure here. Typically, I find that most of my uses of
switch involve external data. Processing a one letter command,
for example.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string isAcceptable(int &est, int &act);

Not sure why you pass by reference here. You don't want to
actually modify the values passed in, I think.
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;

It's generally frowned upon to declare variables without
initializing them. In the case of margin, here, I think it's
OK. For the other two, I'd wait, and only declare them when I
could initialize them.
//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;

Just a note, but this is confusing, and in the case of est < 0,
an error. When using chained if/elses, like this, to initialize
a variable, you should *always* end with a pure else, to ensure
that the variable does get initialized. (Similarly, when
initializing in a switch, there should always be a default case,
and when initializing from table lookup, code to handle the case
where table lookup failed---although I'll occasionally use a
sentinal to ensure that it doesn't fail.)
// diff between the est & act times
if (est > act)
diff = (est - act);
else
diff = (act - est);

There's a function std::abs that might interest you here.
//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);

Would you care to explain the scenarios where the if above might
be false? Otherwise, since this is the initialization of x, I'd
write:
int x = diff > margin ? diff - margin : margin - diff ;
or more likely:
int x = std::abs( diff - margin ) ;
//convert x int to char

to string, not to char.
std::string s;
std::stringstream out;
out << x;

Given the above, I'd just write:
out << diff - margin ;
here, or
out << std::abs( diff - margin ) ;
and not use a variable x at all.
s = out.str();
return "\n\t The flight delay is not acceptable."
"\n\t The estimate is off by: " + s + " minutes.";

And I'd definitly just stream the works, e.g.

out << "\n\t The flight delay is not acceptable"
"\n\t The estimate is off by; "
<< diff - margin
<< " minutes." ;
return out.str() ;

But of course, it would be a lot cleaner if you only had a
single return. I'd probably initialize a string variable result
with the OK message, and just have an if to generate the error
message in its place.

(FWIW: compared to many beginners trials, I think you've
actually done a very good job. My comments are either minor
details, or relate to things you typically only learn from
experience. For the rest, the code seems clean and well thought
out. Something that is all too rare even with experienced
programmers.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top