yeliah
yeliah

Reputation: 41

Can I declare a variable in an “if statement”?

I am writing a "vending machine" program and I need to have 5 items where 3 items cost an integer amount and the other 2 cost a decimal amount.

I am only using if statements in this code, but there is an error with my cost variable.

What did I do wrong?

Code below:

#include <iostream>
using namespace std;

int main() {

  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;
  cout << "1. Popcorn: $2" << endl;
  cout << "2. Coconut Clusters: $3" << endl;
  cout << "3. Granola Bar: $2.50" << endl;
  cout << "4. Trail Mix: $1.50" << endl;
  cout << "5. Chocolate: $1" << endl;

  cout << "Enter you selection: " << flush;
  int input;
  cin >> input;


  if (input == 1) {
    cout << "You added Popcorn to your cart." << endl;
    float cost = 2;
    cout << "Your total is $" << cost << endl;
  }
  
  if (input == 2) {
    cout << "You added Coconut Clusters to your cart." << endl;
    float cost = 3;
    cout << "Your total is $" << cost << endl;
  }
  
  if (input == 3) {
    cout << "You added Granola Bar to your cart." << endl;
    float cost = 2.50;
    cout << "Your total is $" << cost << endl;
  }

  if (input == 4) {
    cout << "You added Trail Mix to your cart." << endl;
    float cost = 1.50;
    cout << "Your total is $" << cost << endl;
  }

  if (input == 5) {
    cout << "You added Chocolate to your cart." << endl;
    float cost = 1;
    cout << "Your total is $" << cost << endl;
  } 

  

  cout << "Pay amount: " << flush;
  float money;
  cin >> money;

  if (money > cost) {
    float change = money-cost;
    cout << "Thank you! You have $" << change << " change." << endl;
  }

  if (money == cost) {
    cout << "Thank you! Have a nice day!." << endl;
  }

  if (money < cost) {
    float amountOwed = cost-money;
    cout << "Please insert another $" << amountOwed << endl;

    cout << "Enter amount: " << flush;
    float payment;
    cin >> payment;

    if (payment > amountOwed) {
    float change2 = payment-cost;
    cout << "Thank you! You have $" << change2 << " change." << endl;
    }

    if (payment == amountOwed) {
      cout << "Thank you! Have a nice day!." << endl;
    }

    if (payment < amountOwed) {
      cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
    }

  }
  return 0;
}

Upvotes: 0

Views: 153

Answers (4)

Ayush Jain
Ayush Jain

Reputation: 181

When I try to compile your code, I get the following error message:

Error: ‘cost’ was not declared in this scope

In order to fix this error, you just need to declare cost variable in the function's main block scope, for example like this:

int main()
{
    float cost;

    [...]
}

Upvotes: 2

Andreas Wenzel
Andreas Wenzel

Reputation: 24726

The problem is that you are doing the following:

int main()
{
    [...]
    if (input == 1) {
        cout << "You added Popcorn to your cart." << endl;
        float cost = 2;
        cout << "Your total is $" << cost << endl;
    }
    [...]
    if (money > cost) {
        [...]
    }
    [...]
}

The scope of the variable cost is limited to the if block, because that is where you declare it. Therefore, the variable does not exist anymore when you evaluate the expression money > cost.

To fix this, you should instead declare the variable cost in the main block scope of the function, like this:

int main()
{
    float cost;

    [...]
    if (input == 1) {
        cout << "You added Popcorn to your cart." << endl;
        cost = 2;
        cout << "Your total is $" << cost << endl;
    }
    [...]
    if (money > cost) {
        [...]
    }
    [...]
}

Upvotes: 5

Jesper Juhl
Jesper Juhl

Reputation: 31457

"Can I declare a variable in an “if statement”?"

Yes. For example:

if (auto foo = bar())

The variable foo will be valid inside the body of the if statement and will be initialized to whatever bar() returns and the body of the if will be entered if foo converts to a bool value of true.

Since C++17 you can also do:

if (auto foo = bar(); foo == 42)

Again, the variable foo will be declared and valid inside the if and initialized by bar(), but you now have more control over when to enter the if body.

Upvotes: 2

Tony Delroy
Tony Delroy

Reputation: 106076

Update: you've just changed your question to add float cost = 0; in a suitable place. Now you need to remove the float keyword from the attempted assignments inside each if (input == block: e.g. change float cost = 2.50; to cost = 2.50; so it changes the function-scope cost variable, instead of creating an extra if-scope variable.


Addressing your original problem...

Your if blocks...

if (input == 3) {
    cout << "You added Granola Bar to your cart." << endl;
    float cost = 2.50;
    cout << "Your total is $" << cost << endl;
}

...each create a float cost variable local to the scope of that if, so after the } the value of cost that you set is gone. Instead, you need to have:

float cost;

Before your first if (input == ... statement, so its lifetime extends to the end of the enclosing function scope (i.e. until main() returns).

Upvotes: 2

Related Questions