### I need help with ifs, elses, and looping

[u][b][b]#include <iostream>
using namespace std;

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

int loopCount;
int cost;
cout << "How much does this product cost? ";
cin >> cost;
{
loopCount = 0;
}

int sum = (cash - cost);
if (cash > cost);
cout << "Looks like you'll have enough money to pay for the product" << endl;
cout << "The amount of cash back will be " << sum << endl;

if (cash == cost);
cout << "Looks like you just have enough" << endl;
cout << "The amount of cash back will be " << (sum - cost) << endl;

if (cash < cost);
cout << "Looks like you'll need more money to buy the product" << endl;
cout << "The amount of money needed to buy the product is " << (cost - cash) << endl;

return 0;
}
//The goal of the program is to put in a certain amount of money a person has, then it subtracts the total amount that a product costs. the program will respond with a total amount of money as well as something to say about being able to buy more products or not, it will then ask for a second item and its cost then find the sum of that and so on

The problem is that I cant finish the if and else statement, and the program doesnt loop to ask if there are anymore products, and the program only runs if i have the if statements written above in the certain order.

Also this is what outputs when i put in the cash of 10 and the cost of 9:
How much money do you have? 10
How much does this product cost? 9
Looks like you'll have enough money to pay for the product
The amount of cash back will be 1
Looks like you just have enough
The amount of cash back will be -8
Looks like you'll need more money to buy the product
The amount of money needed to buy the product is -1
Please use the code brackets - it helps a lot.

First off - you need to insert a loop.

 ``1234`` ``````for(loopCount = 0; loopCount < Yourchoicehere; loopCount++) { //Stuff you want to loop through }``````

Youchoicehere could be a number to loop to, or you can use user input as well to end it.

Next you need to use brackets with if statements, what is in between the brackets.

 ``123456789`` ``````if (cash == cost) { cout << "Looks like you just have enough" << endl; cout << "The amount of cash back will be " << (sum - cost) << endl; } else if (condition) { // Code }``````
Last edited on
if, else, and loop statements stop at the first semicolon or after the following braces.

Newlines/indentation/whitespace is irrelevent

Examples:
 ``12345678910111213`` ``````if( some_condition ) PartOfIfStatement(); // <- semicolon, if stops here NotPartOfIfStatement(); //... if( some_condition ) { // <- opening brace PartOfIfStatement(); AlsoPartOfIfStatement(); } // <- closing brace, if stops here NotPartOfIfStatement();``````

Your problem is you have a semicolon immediately after the if, effectively making it worthless:

 ``123`` ``````if (cash < cost); // <- semicolon, if stops here (ie: does nothing) cout << "Looks like you'll need more money to buy the product" << endl; // <- not part of if statement ``````

Alright I got it, thanks guys
Topic archived. No new replies allowed.