Thread: help with my C++ homework

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    11

    help me understand why my program is not working properly

    I am writing a program that is suppose to ask for the type quantity and size of the lumber that is to be ordered. It is suppose to print the quantity, size, type and cost. Then the program is suppose to total the whole order. So far I am working on getting my program to print correctly. Every time I enter my information the type of wood printed is Pine and I get $0 for the cost. Can someone please explain to me where I am going wrong. Thank you!

    Code:
    #include <string>
    #include <iomanip>
    #include <iostream>
    using namespace std;
    char chr;
    int main()
    {
     string order;
        
    float P;
        
    float F;
        
    float C;
        
    float M;
        
    float O;
        
    float T;
     P=0.89;
     F=1.09;
     C=2.26;
     M=4.50;
     O=3.10;
        
    int quantity=0;
        
    int width=0;
        
    int height=0;
        
    int length=0;
        
    char type;
        
    float cost;
        
    float bmeasure; bmeasure=(width*height*length)/12;
        
    cout<<"Enter item: "<<endl;  // the user enters their order    
        cin>>type>>quantity>>width>>height>>length;  
    //stores the information 
        cost=bmeasure*quantity*type;
         
        
    if(P)
        {
          cout<<quantity<<" "<<width<<"x"<<height<<"x"<<length<<" "<<"Pine"<<", "<<"cost: "<<"$"<<cost<<endl;
        }
        
    else if(F)
        {
           cout<<quantity<<" "<<width<<"x"<<height<<"x"<<length<<" "<<"Fir"<<", "<<"cost: "<<"$"<<cost<<endl;
        }
        
    else if (C)
        {
           cout<<quantity<<" "<<width<<"x"<<height<<"x"<<length<<" "<<"Cedar"<<", "<<"cost: "<<"$"<<cost<<endl;
        }
        
    else if(M)
        {
           cout<<quantity<<" "<<width<<"x"<<height<<"x"<<length<<" "<<"Maple"<<", "<<"cost: "<<"$"<<cost<<endl;
        }
        
    else if (O)
        {
          cout<<quantity<<" "<<width<<"x"<<height<<"x"<<length<<" "<<"Oak"<<", "<<"cost: "<<"$"<<cost<<endl;
        }
            cin>>chr;
        
        
    return 0;
    }
    
    Last edited by sdoyle; 11-18-2013 at 06:47 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Presumably you want to compare the "type" that was entered in to ... whatever you're expecting the user to enter.
    And C is imperative, not relational; saying "bmeasure=(width*height*length)/12;" doesn't mean that that equation will always hold true; it is an imperative statement to calculate, *right at that moment*, the value of (width*height*length)/12 and assign that value to bmeasure. Updating the value of width, height, or length at a later date will not cause any change in bmeasure.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    I changed the if statement to:
    Code:
    if(P)
    Code:
     
    	{
    bmeasure=(width*height*length)/12;
    	  cost=bmeasure*quantity;
    cout<<quantity<<
    " "<<width<<"x"<<height<<"x"<<length<<" "<<"Pine"<<", "<<"cost: "<<"$"<<fixed<<setprecision(2)<<cost*.89<<endl;
    	}

    Will I have to put add the bmeaure and cost= to all of the else if statements to make them work or is there a better way that I should be doing this?

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    So I have realized that my program is only reading and printing the first statement (the if(P)). How do I get the program to actually run through all the else if statements?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to compare type to something in your if statement. P is always true, since it exists and isn't zero. Thus if(P) doesn't help. You need to use == or something similar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help
    By wormy677 in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2012, 12:47 AM
  2. Need Help for my homework
    By 張祖豪 in forum C Programming
    Replies: 4
    Last Post: 05-29-2011, 01:14 AM
  3. Do my homework
    By heiroglikano in forum C Programming
    Replies: 3
    Last Post: 05-31-2009, 06:26 AM
  4. Homework, please help!
    By FandaR in forum C Programming
    Replies: 4
    Last Post: 04-30-2009, 08:59 AM

Tags for this Thread