Final exam(need help)

I have to create a program for a buy one get one half off deal, and I don't know how I would write that part in the code. The half off will be to the cheaper item, and if they are the same, then to be on the second item.
We aren't here to do your homework. You have to put some effort in, and show what you've tried so far, and what specific problems you're having with the code that you have written.

If you literally don't know where to begin, please check out the site's tutorial on control flow: http://www.cplusplus.com/doc/tutorial/control/ (You'll need to use if statements)

That being said, here's some rough pseudo-code:
1
2
3
4
5
6
7
8
9
10
11
if (item is part of "buy one get one half off deal" and you have 2 of the items)
{
    if (first item's price equals second item's price)
    {
        Take half off the second item's price.
    }
    else
    {
        take half off the cheaper item's price.
    }
}
Last edited on
#include <iostream>
#include <iomanip>
#include "stdafx.h"


int main()
{
// Declare variables
int item1 = 0;
double item1Price; 0.0;
int item2 = 0;
double item2Price = 0.0;
int total = item1Price + item2Price;
double total = 0.0;

// Request input
cout << "What is the price of the first item?: ";
cin >> item1Price;
cout << "What is the price of the second item?: ";
cin >> item2Price;

// Calculate and display total
total = item1Price + item2Price;
if (item2 is part of "buy one get one half off deal" and you have 2 of the items)
{
if (first items price equals second items price)
{
Take half off the second items price.
}
else
{
take half off the cheaper items price.
}
} //end if
cout << fixed << setprecision(2);
cout "Total: $" << total << endl;
return 0;
} //end of main function


This is what i got done, i hope if you didn't mind me using your code directly, my main problems are my teachers requirements of 3 variables (all double) and 2 in/out statements (Fyi, I didn't want you to do my homework).
Last edited on
I think the if/else block you need 1 if block, 1 else if and 1 else;

Pseudocode:

1
2
3
4
5
6
7
8
9
if( first item is equivalent to second item ){
 // Half off second item
}
else if ( first item is greater than second item){
 // Half off second item
} 
else{
 // Half off first item
}
#include <iostream>
#include <iomanip>
#include "stdafx.h"
#include <string>

int main()
{
using namespace std;
// Declare variables
int item1 = 0;
double item1Price; 0.0;
int item2 = 0;
double item2Price = 0.0;
int total = item1Price + item2Price;
double total = 0.0;

// Request input
cout << "What is the price of the first item?: ";
cin >> item1Price;
cout << "What is the price of the second item?: ";
cin >> item2Price;

// Calculate and display total
if (item1 is equivalent to item2) {
// Half off second item
}
else if (item1 is greater than item2) {
// Half off second item
}
else {
// Half off first item
}
//end if
cout << fixed << setprecision(2);
cout "Total: $" << total << endl;
return 0;
} //end of main function

I have three errors: 2 expected a ')' and expected a ';', and I don't know how to get rid of them.
Please put your code between [code] [/code] tags...

What lines does it say the errors happen on?

cout "Total: $"
You forgot to put the << operator.

And obviously the pseudo-code inside the if statements isn't going to be compile... the pseudo-code provided is for you to then translate into actual code.

How do we know if two items are the same thing? Is the item the ID number of the item (ex, banana's ID is 3, apple's ID is 4)? Then, in the if statement, you should check if item1 and item2 are equal in value to each other.

Do you know how to use comparison operators in C++?
Go to http://www.cplusplus.com/doc/tutorial/operators/ Relational and comparison operators section.

_________________________

Also, if item1Price and item2Price are both doubles, then total should probably be a double as well, unless you want it to truncate the values. doubles emulate the Real number line (-1.5, 0.0, 1.0, 3.14, 2.78, 1.618, etc), while ints can only be Integers (-1, 0, 1, 2, 3 ...}.
Last edited on
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double item1Price = 0.0;
	double item2Price = 0.0;
	double total; 0.0;

	cout << "Item 1's price: ";
	cin >> item1Price;
	cout << "Item 2's price: ";
	cin >> item2Price;

	if (item1Price < item2Price)
		total = item1Price / 2 + item2Price;

	else (item2Price > item1Price)
	; total = item2Price / 2 + item1Price;
	//end if
	cout << fixed << setprecision(2);
	cout << "Total: $" << total << endl;
	return 0;
} //end of main function 

I got it working after changing up some of the code, thank you all for helping me, this is due tomorrow.
Topic archived. No new replies allowed.