Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Starwind8748

Another C++ Problem

This topic is 6146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yes this is a school problem. However I am having trouble on it and would appreciate some help. Here''s the problem: Priority mail costs $3.00 for the first two pounds and $1.00 for each additional pound or fraction. First class mail costs 32 cents for the first ounce and 23 cents for each additional ounce or fraction, up to 11 ounces, after which priority rates apply. Here is what I have so far: // MAIN double ounces; enum MAIL {firstClass, priority}; cout << endl << endl << endl; cout << "1: First class " << endl; cout << "2: Priority mail " << endl; cout << endl << "How would you like to send your mail: "; cin >> type; cout << endl << "Enter the weight of your package (ounces): "; cin >> ounces; cout << endl << "You will require $" << mailMeter (ounces, type) << endl; } double mailMeter (double ounces, MAIL type) /* ....So this is the spot I''m having trouble with. In this function I''m supposed to calculate the postage. Here is what I tried first: */ if (type - 1 == true) { postage = /*...I tried a whole bunch of things here but they wouldn''t work. */ } /* For First Class mail, I was gonna do this: */ if (type - 1 == false) { ....then do some stuff here. } Any suggestions on how I could figure out the postage? Also how would I use the enum in a problem like this? I would appreciate any help at all.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Starwind8748
if (type - 1 == true)

An enum is an enumerated integer. The first value defaults to zero unless you explicitly set it to something else. So, firstClass would be zero. firstClass - 1 would be -1, which never evaluates to true or false. Try again:

if(type == firstClass)
{
}
if(type == priority)
{
}

The rest is just algebra.

Share this post


Link to post
Share on other sites
  
switch( type )
{
case firstClass:
// do some stuff

break;
case priority:
// do some stuff

break;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Starwind8748
No, the user is going to enter a 1 or a 2. Thats why I''m wondering why I have to use enum.

Who said you have to?

Either way, an enum is an integer as I said above, so you can compare one to the other:

int input;
...
cin >> input;
switch(input)
{
case priority:
...
}

quote:
And its the algebra part I can''t figure out.

Well, now, whose problem is that? It''s your homework. Go talk to your teacher or other kids in your class - that''s what your teacher would like.

Share this post


Link to post
Share on other sites
You never HAVE to use an enumerated constant. I only find them useful when I want to use values which describe the problem I''m trying to solve -- to help me visualize things. Generally, I don''t need them. In this case, I personally wouldn''t use them.

As it has been said before, the rest is algebra coupled with some C++ that you learn in chapter 2 of any book. Since I''m a nice guy, and I kinda like math, I''ll give it a whirl. I make no promises about this working, though. I''m not exactly batting 1000 today:

  

#include <iostream.h>
#include <stdlib.h>

int Main()
{
double ounces;
short type;

cout << endl << endl << endl;

cout << "1: First class " << endl;
cout << "2: Priority mail " << endl;
cout << endl << "How would you like to send your mail: ";

cin >> type;

cout << endl << "Enter the weight of your package (ounces): ";

cin >> ounces;

/* Check to see if First Class AND over 11 ounces. If so, switches to Priority pricing */
if (type = 1 && ounces > 11) type = 2;

cout << endl << "You will require $" << mailMeter (ounces, type) << endl;

system("PAUSE");
return 0;

}

int mailMeter(double ounces, short type)
{

double lbs = ounces / 16;
double total;

switch(type)
{

case 1 : if(lbs <= 2) total = 3;
if(lbs > 2)
{
total = (lbs - 2) * 1 + 3;
}

case 2 : if(ounces <= 1) total = .32;
if(ounces > 1)
{
total = (ounces - 1) * .23 + .32;
}
}

}


Share this post


Link to post
Share on other sites
A couple corrections (since I obviously cant edit my own post on this forum), you have to make mailMeter return total (duh!), and you''ll also need a prototype for mailMeter at the top of the code (also duh!).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by utwo007
A couple corrections (since I obviously cant edit my own post on this forum

Yes you can! Click on the little EDIT icon at the top of your post.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!