Another C++ Problem

Started by
7 comments, last by Starwind8748 22 years, 4 months ago
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.
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.
  switch( type ){case firstClass:   // do some stuff   break;case priority:   // do some stuff   break;}  
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
No, the user is going to enter a 1 or a 2. Thats why I''m wondering why I have to use enum. And its the algebra part I can''t figure out.
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.
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;           } }}  

---signature---" Actually, at the time, I didn't give a damn about the FoxNews issue... just that you would come swooping in with your Super Mith cape flapping behind you and debunk it just because it didn't happen in your living room." - InnocuousFox
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!).
---signature---People get ready.I'm ready to play.
if (type = 1 && ounces > 11) type = 2;

== instead of =
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.

This topic is closed to new replies.

Advertisement