ignoring a piece of code based on specific input?

Hopefully I can explain this well.

For the scenario, this program calculates the cost of copies based on a number of pages * $0.08(the cost of a copy), multiplied by the number of copies per page.

If the number of pages is greater than one, the option of stapling each page is given, at a cost of $0.02 per page.

I got all that working with the code below - my problem is that if the number of pages is exactly 1, I need it to disregard the request for stapling, because who's gonna staple 1 page?

So, how do I say if the number of the pages is greater than 1, ask for stapling, but if it IS 1, stop there and carry on with the rest of the calculations?

What am I missing?

Thanks



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
int main ( )
{
    //constant declaration
    const double COST_OF_COPY = 0.08;       //  cost of one copy
    const double DISCOUNT_RATE = 0.10;
    const double COST_OF_STAPLE = 0.02;
    //variables
    //number of pages
    int numberPages;
    //number of copies
    int numberCopies;
    //stapling cost if only 1 page
    //double costStaple = 0.02;
    //total cost of the pages*copies
    double copyCost;
    //cost of copies * staples for total cost of stapling
    double totalStapled;
    //cost for all copies with stapling costs
    double totalCost;
    //discount if applicable
    double discount = 0.10;
    //final cost after stapling and discounts
    double costFinal;
    //response declaration - yes or no for staples
    char response;
    //print4u header (didn't know the \n trick before I wrote the next few lines)
    cout << endl;
    cout << "Print4U Printing Services" << endl;

    //asking for user input for # of copies
    cout << endl << "Enter number of copies: ";
    cin >> numberCopies;

    //copies must be >= 1
    if (numberCopies < 1)
    {
        cout << "Invalid number of copies - must be >= 1" << endl;
        exit(0);
    }

    //asking for user input for # pages per copy
    cout << "Enter number of pages per copy: ";
    cin >> numberPages;



    //validate # of pages, must be greater than 0
    if (numberPages < 1)
    {
        cout << "Invalid number of pages per copy - must be >= 1" << endl;
        exit(0);
    }
    //if pages is more than 1, ask for staples - with validation - only y,n,Y,N are good responses
        else
        {
            cout << "Do you wish to staple each copy? (y/n): ";
            cin >> response;
        }

    if ( !(response  == 'Y' || response == 'N' || response == 'y' || response == 'n') )
    {
        cout << "Invalid answer for stapling - must be y or n\n";
        exit(0);
    }
    else
        {
            totalStapled = numberCopies * COST_OF_STAPLE;
        }

    //formula to calculate cost based on copies (times) pages (times) cost_of_copy
    copyCost = numberCopies * numberPages * COST_OF_COPY;

    //forumla to calc cost based on copies times stapling
    totalCost = copyCost + totalStapled;

    if (totalCost >= 25)
    {
        discount = totalCost * DISCOUNT_RATE;

    }
        else
        {
            discount = 0;
        }

    //calculate final cost of pages, copies, stapling, discount
    if (totalCost >= 25)
    {
        costFinal = totalCost - discount;

    }
        else
        {
            costFinal = totalCost;
        }

    //dialogue stating number of copies and number of pages, with total cost; including precision of 2 decimal places
    cout << setprecision (2) << fixed << endl;
    cout << left << setw(11) << "Cost Copies:" << right << setw(9) << copyCost << endl;
    cout << left << setw(11) << "Cost Stapling:" << right << setw(7) << totalStapled << endl;
    cout << left << setw(11) << "Cost Total:" << right << setw(10) << totalCost << endl;
    cout << left << setw(11) << "Discount:" << right << setw(10) << discount << endl;
    cout << setw(15) << " "<< "======" << endl;
    cout << "Cost Final:" << setw(10) << costFinal << endl;
    cout << "\n";

    return 0;



heres a picture of what's supposed to be happening - the difference between more than 1 page, and then just 1 page.

http://imageshack.us/a/img402/4589/codebb.jpg
Instead of
1
2
3
4
5
else
        {
            cout << "Do you wish to staple each copy? (y/n): ";
            cin >> response;
        }


Try else if (numberPages > 1){...}
Then if numberPages=1 it won't ask to staple each copy.
I changed that one block to an else if, but now if the number of pages is 1, it jumps past the "do you wish to staple?" and says invalid answer for stapling, and the program ends.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    if (numberPages < 1)
    {
        cout << "Invalid number of pages per copy - must be >= 1" << endl;
        exit(0);
    }
    //if pages is more than 1, ask for staples - with validation - only y,n,Y,N are good responses

        else if (numberPages > 1)
        {
            cout << "Do you wish to staple each copy? (y/n): ";
            cin >> response;
        }

    if ( !(response  == 'Y' || response == 'N' || response == 'y' || response == 'n') )
    {
        cout << "Invalid answer for stapling - must be y or n\n";
        exit(0);
    }

    else if (response == 'n' || response == 'N')
        {
            totalStapled = numberCopies * COST_OF_STAPLE * 0;
        }
    else
        {
            totalStapled = numberCopies * COST_OF_STAPLE;
        }
holy shit I think I did it.
I just added an if statement that said if the number of pages is 1, declare the response as "n" for 'no', and so it did what a 'n' response would have done. or something like that.
it might be messy but this works.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    //validate # of pages, must be greater than 0
    if (numberPages < 1)
    {
        cout << "Invalid number of pages per copy - must be >= 1" << endl;
        exit(0);
    }
    //if pages is more than 1, ask for staples - with validation - only y,n,Y,N are good responses

        else if (numberPages > 1)
        {
            cout << "Do you wish to staple each copy? (y/n): ";
            cin >> response;
        }
    if (numberPages == 1)
    {
        response = 'n';
    }
    else if ( !(response  == 'Y' || response == 'N' || response == 'y' || response == 'n') )
    {
        cout << "Invalid answer for stapling - must be y or n\n";
        exit(0);
    }

    if (response == 'n' || response == 'N')
        {
            totalStapled = numberCopies * (COST_OF_STAPLE * 0.00);
        }
    else
        {
            totalStapled = numberCopies * COST_OF_STAPLE;
        }


I think you put be on the right track with your suggestion. thanks man
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
else if (numberPages > 1)
{
    cout << "Do you wish to staple each copy? (y/n): ";
     cin >> response;

     if ( !(response  == 'Y' || response == 'N' || response == 'y' || response == 'n') )
    {
        cout << "Invalid answer for stapling - must be y or n\n";
        exit(0);
    }

    else if (response == 'n' || response == 'N')
        {
            totalStapled = numberCopies * COST_OF_STAPLE * 0;
        }
    else
        {
            totalStapled = numberCopies * COST_OF_STAPLE;
        }
}


Expand the else if (numberPages > 1) statement to encompass the other if/else statements
Topic archived. No new replies allowed.