Thread: Help!

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    33

    Else....if statements help and more.

    Heya guys new to this great board and programming in general (well I did a little plain english if you can consider that a programming language). I'm also back in college as a freshman at 26 so I think I am having a bit more of a problem getting the swing of it then my younger counterparts because I didn't have this stuff in high school. But I regress...

    I am trying to create a mail/weight/cost calculator for introo to c++

    I guess its a problem more or less with if...else statements. Heres my code:
    Code:
    #include <iostream>
    using namespace std;
    const double ENVELOPE_POSTAGE = 1;
    const double ADD_TO_FIRST_CLASS = 0.23;
    const double FIRST_PRIORITY_COST = 3.85;
    const double SECOND_PRIORITY_COST = 8.40;
    const int LOW_RANGE = 1;
    const int FIRST_CLASS_MAX = 13;
    const int FIRST_PRIORITY_MAX = 159;
    const int PRIORITY_MAX = 160;
    int main () 
    {
    	int weight;
    	double cost;
    	int mailService;
    
       
    	cout<<"What is the weight of the envelope that you would like to send? "<<endl;
    	cin>>weight;
    	if (weight > PRIORITY_MAX)
    		cout<<"You have entered an invalid number"<<endl;
    	//return 0;
    	if(weight < LOW_RANGE)
    		cout<<"Your postage is in the low range and it's envelope postage";
    	//return 0;
    	if(weight <= FIRST_CLASS_MAX)
    		cout<<
    		cout<<"First Class Mail"<<endl;
    		cost = .37 + (weight - 1) * .23;
    	    cout<<"You owe "<<cost<<endl;
    		cout<<endl;
    		return 0;
        if(weight = 160)
    		cout<<"Priority mail"<<endl;
    	cost = 8.40;
    	cout<<"you owe 8.40"<<endl;
    	return 0;
    
    		//cout<<"Your postage is ";
    		//cout<<ADD_TO_FIRST_CLASS<<" and it's First Class";
    		//cout<<endl;
        //if(weight <= FIRST_PRIORITY_MAX)
    		//cout<<"your postage is ";
    	    //cout<<FIRST_PRIORITY_COST<<" and it's Priority";
    		//cout<<endl;


    the problem is when I try to get it to calculate a statement it also calculate the others....and I don't want that. Anyone take a stab at what I am doing wrong?
    Last edited by Furious_George; 09-24-2003 at 07:29 PM.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    first of all I would make weight a double instead of an int.
    next:

    Code:
    if(weight <= FIRST_CLASS_MAX){ 
    	cout<< //you should get an error here so delete it
    	cout<<"First Class Mail"<<endl;
    	cost = .37 + (weight - 1) * .23;
    	cout<<"You owe "<<cost<<endl;
                    cout<<endl;
                    return 0;
    } else if    (weight == 160) {
    		cout<<"Priority mail"<<endl;
    	cost = 8.40;
    	cout<<"you owe 8.40"<<endl;
    	return 0;
    }
    
    //and so on....if your if statement is supposed to do more than 
    //one thing always put everything in brackets {}, actually I put 
    //everything in brackets, no matter the length.
    axon
    P.S. Next time you post a thread make sure it has a better title/subject....help doesn't cut it

    edit::this is the if else technioque i use just for completeness:
    Code:
    if( condition ){
           //do stuff here
    } else if ( condition2 ){
          //some more stuff
    } else { 
          //default condition
    }
    Last edited by axon; 09-24-2003 at 07:37 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    33

    Thumbs up

    Thanks Axon that helped me out a bunch

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    Since I got you here how would I go about doing an if/and statement. For example if(mail < x) AND (mail > Y)

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if(mail<x&&mail>y)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    Code:
    // AND statement
    if(mail < x && mail > y)
    {
         // Code here
    }
    
    // OR statement
    if(mail < x || mail > y)
    {
         // Code here
    }
    Hope that helps .

    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Code:
    if( (mail < x) && (mail > Y) ){
        //................
    }
    for the program to go into this if statement both conditions must be true.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed