Simple problem with declaring scope

#include <iostream>
using namespace std;

int main()
{
cout << "How much money do you have?";
cin >> cash;

cout << "How much does the product cost?";
cin >> cost;

if (cash > cost)
cout << "Looks like you'll have enough money to pay for the product";
cout << "The amount of cash back will be " << (cash - cost) << endl;
if (cash == cost)
cout << "Looks like you just barely have enough money";
else
cout << "Looks like you'll need more money to buy the product";
cout << "The amount of money needed to buy the product is " << (cost - cash) << endl;

return 0;
}


//I almost have it finished
What is the problem?
And, What are Cash and Cost?
Edit: After looking at your previous posts, Let me say you need all the most basic tutorials. You may want to begin here: http://www.cplusplus.com/doc/tutorial/
Last edited on
Hi!
I'm a beginner in C++ as well but I think I can help you out with this assignment.
First thing you need to do is declare the variables before you call them.
Second thing is to format the if statements with the right syntax.
And the third thing would be to fix the formula for cash returned.

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
#include <iostream>
#include <iomanip> // add this directive so you can format the output
#include <cmath> // add this directive so you can use more arithmetic operators 
using namespace std;

int main()
{
double cash, cost, change; // add a variable for cash returned, I called it "change"

cout << fixed << showpoint << setprecision(2); // This will display cash limited to two decimal places.
cout << "How much money do you have?" << endl; // Add end of line statement so that your display looks nice.

cin >> cash;

cout << "How much does the product cost?" << endl;
cin >> cost;

if (cash > cost)  // Don't forget to add brackets with the if statement
{
change = cash - cost;  // It's easier to make a variable for cash returned & the formula before your print statments
cout << "Looks like you'll have enough money to pay for the product." << endl;
cout << "The amount of cash back will be " << "$" << change << endl;
}

if (cash == cost)
{
change = cash - cost;
cout << "Looks like you just barely have enough money" << endl;
cout << "The amount of cash back will be " << "$" << change << endl;
}

if ( cash < cost)
{
change = abs (cash - cost); // abs takes the absolute value so you don't have a negative value for cash back.
cout << "Looks like you'll need more money to buy the product" << endl;
cout << "The amount of money needed to buy the product is " << "$" << change << endl;
}

return 0;
}

Last edited on
Topic archived. No new replies allowed.