Thread: A little help with if/else in a function

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    12

    A little help with if/else in a function

    Hey everyone,

    I'm having a bit of trouble getting the correct calculations out of a function. When you choose pants [P] for the “double pantsFab()” function and enter 30 for waist and 72 for height, the first ‘if’ (where “pleat” and “baggy” are both == ‘Y’) works fine, as does the first ‘else if’ (“pleat” and “baggy == ‘Y’ and ‘N’ respectively). However, the following two ‘else ifs’ (where “pleat” and “baggy” are respectively == ‘N’ and ‘Y’ and then ‘N’ and ‘N’) don’t return the correct calculation. For those two statements, “ttlFab” is not passing the correct value back to main even though it appears correct when I do it manually by calculating the values as assigned to ttlFab in each of the two statements. For the ‘N’ and ‘Y’ statement (around line 82), ttlFab passes back 125.4 when it should be 122.4. With the ‘N’ and ‘N’ statement (around line 86), 114 is returned when it should be 111. Basically the values for the first two statements (‘if’ and ‘else if’) are also being returned in the third and fourth statements even though ttlFab is assigned different values. Hopefully someone can see where I’m screwing this up because my eyes are anything but fresh right now.

    The calculations are supposed to be:
    · 2 1/2 square inch per waist size inch of the person being fitted
    · 1/2 square inch per height inch of the person being fitted
    · 1/10 square inch per waist size inch for pleated front
    · for the baggy look, add an extra 10% to the total fabric

    30 waist, 72 height, pleats: Yes [Y], and baggy: No [N] should return a value of 114.

    ** I did put a couple of temp couts at around line 92 to show the values of the assignments to ttlFab in the two statements in question.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    double pantsFab(double sizeWaist, double heightTall);
    double shortsFab(double sizeWaist, double heightTall);
    double shirtFab(double sizeWaist, double heightTall);
    int main()
    {
       double sizeWaist = 0.0 , heightTall = 0.0, fabArea;
        char itemChoice, usrCont;
    
    
        do
        { // Program description and user input for garment choice
            cout << "\n**********FABRIC CONSUMPTION CALCULATOR**********\n";
            cout << "\nWould you like pants [P], shirt [S] or shorts [T]?: ";
            cin >> itemChoice;
            cout << "\n";
        
        //Switch statement depending on value of itemChoice
        switch(itemChoice)
        {
            case 'P' : case 'p' : fabArea = pantsFab(sizeWaist, heightTall); break;
            case 'S' : case 's' : fabArea = shirtFab(sizeWaist, heightTall); break;
            case 'T' : case 't' : fabArea = shortsFab(sizeWaist, heightTall); break;
            default : cout << "\nInput error! Try again.\n"; continue;
        }
        
        cout << "\nYour garment will require " << fabArea << " square inches of fabric.\n";
        
        cout << "\nCalculate more fabric? 'Y' or 'N': ";
        cin >> usrCont;
        cout << "\n";
        }while(usrCont == 'Y' || usrCont == 'y');
        
        system("pause");
        return 0;
    }
    
    
    //Calculate fabric for pants
    double pantsFab(double sizeWaist, double heightTall)    
    {
        const double waistMult = 2.5, heightMult = 0.5; 
        double ttlFab, fabWaist, fabHeight, pleat, baggy;
        char pleatAsk, baggyAsk;    
    
    
        cout << "\n**********CALCULATOR FOR PANTS FABRIC**********\n";
        cout << "\nEnter waist size in inches: ";
        cin >> sizeWaist;
        cout << "\n";
        
        cout << "\nEnter height in inches: ";
        cin >> heightTall;
        cout << "\n";
        
        cout << "\nWill pants have pleated front? 'Y' or 'N': ";
        cin >> pleatAsk;
        cout << "\n";
        
        cout << "Will pants style be baggy? 'Y' or 'N': ";
        cin >> baggyAsk;
        cout << "\n";
        
        pleat = sizeWaist * 0.1;
        fabWaist = sizeWaist*waistMult;
        fabHeight = heightTall*heightMult;
        
        if (pleatAsk == 'Y' || pleatAsk == 'y' && baggyAsk == 'Y' || baggyAsk == 'y')
        {
             baggy = ((fabWaist + fabHeight + pleat) * 0.1);
             ttlFab = fabWaist + fabHeight +  pleat + baggy;
        }
        else if (pleatAsk == 'Y' || pleatAsk == 'y' && baggyAsk == 'N' || baggyAsk == 'n')
        {
             ttlFab = fabWaist + fabHeight + pleat;
        }
        else if (pleatAsk == 'N' || pleatAsk == 'n' && baggyAsk == 'Y' || baggyAsk == 'y')
        {
             baggy = ((fabWaist + fabHeight) * 0.1);
             ttlFab = fabWaist + fabHeight + baggy;
        }
        else if (pleatAsk == 'N' || pleatAsk == 'n' && baggyAsk == 'N' || baggyAsk == 'n')
        {
             ttlFab = fabWaist + fabHeight;
        }
        else
        {
            cout << "\nInput error! Must enter 'Y' for YES. Try again.\n";
        }
        
        //*** temp couts****************************************************************************
        cout << "ttlFab = "<<ttlFab<<"\n";
        cout << "pleat: 'N' and baggy: 'Y' = " << fabWaist + fabHeight + baggy<<"\n";
        cout << "pleat: 'N' and baggy: 'N' = " << fabWaist + fabHeight<<"\n";
        //******************************************************************************************
        
        return (ttlFab);
    }
    
    
    // Calculate fabric for shirt
    double shirtFab(double sizeWaist, double heightTall)    
    {
        const double waistMult = 2.375, heightMult = 0.44, armMult = 2.8;
        double fabWaist, fabHeight;
        double slvFab, armLength, ttlFab;
        char sleeveAsk;    
        
        cout << "\n**********CALCULATOR FOR SHIRT FABRIC**********\n";
        cout << "\nEnter waist size in inches: ";
        cin >> sizeWaist;
        cout << "\n";
        
        cout << "\nEnter height in inches: ";
        cin >> heightTall;
        cout << "\n";
        
        cout << "\nWill shirt have long sleeves? 'Y' or 'N': ";
        cin >> sleeveAsk;
        cout << "\n";
    
    
        fabWaist = sizeWaist * waistMult;
        fabHeight = heightTall * heightMult;
        slvFab = armLength * armMult;
    
    
        if (sleeveAsk == 'Y' || sleeveAsk == 'y')
        {
            cout << "\nEnter length of arms in inches: ";
            cin >> armLength;
            
            ttlFab = fabWaist + fabHeight + slvFab;
            //ttlFab = fabWaist + fabHeight + (armLength * armMult);
        }
        else
        {
            ttlFab = fabWaist + fabHeight;
        }
        
        return (ttlFab);
    }
    
    
    // Calculate fabric for shorts
    double shortsFab(double sizeWaist, double heightTall)    
    {
        const double waistMult = 1.3, heightMult = 0.25, pcktMult = .15;
        double fabWaist, fabHeight, ttlFab, pcktFab;
        char pcktAsk;    
        
        cout << "\n**********CALCULATOR FOR SHORTS FABRIC**********\n";
        cout << "\nEnter waist size in inches: ";
        cin >> sizeWaist;
        cout << "\n";
        
        cout << "\nEnter height in inches: ";
        cin >> heightTall;
        cout << "\n";
        
        cout << "\nWill shorts have pockets? 'Y' or 'N': ";
        cin >> pcktAsk;
        cout << "\n";
        
        fabWaist = sizeWaist * waistMult;
        fabHeight = heightTall * heightMult;
        pcktFab = (fabWaist + fabHeight) * pcktMult;
        
        if (pcktAsk == 'Y' || pcktAsk == 'y')
        {
            ttlFab = fabWaist + fabHeight + pcktFab;
        }
        else
        {
        ttlFab = fabWaist + fabHeight;
        }
        
        return (ttlFab);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > else if (pleatAsk == 'Y' || pleatAsk == 'y' && baggyAsk == 'N' || baggyAsk == 'n')
    && has higher precedence than ||
    C++ Operator Precedence - cppreference.com

    So what you've written is
    else if (pleatAsk == 'Y' || ( pleatAsk == 'y' && baggyAsk == 'N' ) || baggyAsk == 'n')

    Edit:
    Doing tolower() on all your inputs would save an awful lot of || usage through your code, since you'd only have to test for each letter once.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    Thanks for the quick reply Salem. I'll try the tolower() suggestion and see how it goes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM