Help with nesting

closed account (GybDjE8b)
Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books.

Write a statement that assigns freeBooks the appropriate value based on the values of the bool variable isPremiumCustomer and the int variable nbooksPurchased .

I tried to do it this way
1
2
3
4
5
if(isPremiumCustomer==true){
if(nbooksPurchased >=5 && nbooksPurchased < 8){freeBooks=1;}
if(nbooksPurcahsed >=8){freeBooks=2;}
}
else{isPremiumCustomer=false;}
This looks right.. You could remove from line one, the "== true" part because the condition will check if it's true with just

 
if (isPremiumCustomer)
closed account (GybDjE8b)
hmmm it still gives me an error
I need to see more of your code... You cannot say else isPremiumCustomer = false because it's an else to the condition isPremiumCustomer == true?

Maybe try

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() {
	if (isPremiumCustomer) {
		if(nbooksPurchased >=5 && nbooksPurchased < 8)
			freeBooks=1;
		else if (nbooksPurchased >= 8)
			freeBooks = 2;
		else
			freeBooks = 0;
	} else {
		if (nbooksPurchased >= 7 && nbooksPurchased < 12)
			freeBooks = 1;
		else if (nbooksPurchased >=12)
			freeBooks = 2;
		else
			freeBooks = 0;
	}

cout << freeBooks;
}


Everything else in this code is correct. It would be something in the rest of your code if this doesn't work.
Last edited on
Topic archived. No new replies allowed.