Can someone explain to me what I did wrong here? It is a C++ program and it says
ID: 3563036 • Letter: C
Question
Can someone explain to me what I did wrong here? It is a C++ program and it says I have four errors of undeclared identifiers "more_items on line 120, more_items on line 122, item_number on line 125, and quantity on line 125.
#include <iostream>
using namespace std;
int main ()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Welcome to the Organic Store" << endl;
cout << "Do you have a rewards card? (Y/N)" << endl;
{int reward;
cin >> reward;
if (reward=='Y')
{
{ cout << "Please enter your rewards card number" << endl; }
int card;
cin >> card;
{if (card != 12345)
cout << "Incorrect reward card number" << endl;
} }
else
do {
{
cout << "Please enter a choice" << endl;
cout << "1- View Balance" << endl;
cout << "2- Shop" << endl;
cout << "3- Exit" << endl;
int choice;
cin >> choice;
{if (choice=='1')
cout <<"Your balance is $100" << endl;}
{if (choice=='3')
cout << "Thank you for shopping with us! Have a great day!" << endl;}
{if (choice=='2')
cout << "Choose among the three different categories" << endl;
cout << "1- Produce" << endl;
cout << "2- Meat & Fish" << endl;
cout << "3- Beverages" << endl;
int category;
cin >> category;
{if (category=='1')
cout << "1- Bell peppers-- $2.49 ea." << endl;
cout << "2- Bananas-- $0.59 ea." << endl;
cout << "3- Strawberries 1 lb.-- $4.99" << endl;
cout << "4- Romaine lettuce-- $3.99" << endl;
cout << "5- Sweet potatoes-- $2.49 ea." << endl;
cout << "6- Garlic bulb-- $1.29 ea." << endl;
cout << "7- Lime-- $0.79 ea." << endl;
cout << "8- Cucumber-- $1.99 ea." << endl;
cout << "9- Red onion-- $1.69 ea." << endl;
int produce;
cin >> produce; }
{if (category=='2')
cout << "1- Chicken breast 1 lb.-- $5.59" << endl;
cout << "2- Turkey breast slices 6 oz.-- $6.59" << endl;
cout << "3- Ground beef 1 lb.-- $8.99" << endl;
cout << "4- Salmon filet 8 oz.-- $8.29" << endl;
cout << "5- Alaskan halibut 8 oz.-- $14.99" << endl;
int meat;
cin >> meat;}
{if (category=='3')
cout << "1- Kombucha 16 oz.-- $3.69" << endl;
cout << "2- Orange juice 59 oz.-- $5.15" << endl;
cout << "3- Coconut water 8 oz.-- $1.79" << endl;
cout << "4- Sparkling water 12 packs-- $5.49" << endl;
cout << "5- Lemonade 16 oz.-- $1.99" << endl;
cout << "6- Black tea 8 oz.-- $2.99" << endl;
int beverage;
cin >> beverage;}
cout << "Enter item number" << endl;
int item_number;
cin >> item_number;
cout << "Enter quantity" << endl;
int quantity;
cin >> quantity;
cout <<"Do you wish to shop for more items? (Y/N)" << endl;
int more_items,total,reward_total;
cin >> more_items; }}}
while (more_items=='Y');
{if (more_items=='N')
cout << "You're all set!" << endl;}
int total= (item_number*quantity);
{if(reward==12345)
int reward_total=total*.1;}
cout << "Thank you for shopping with us! Have a great day!" << endl;
return 0;
}}
Explanation / Answer
/*Hi, you had declared some variables inside a loop and were trying to access those outside
the loop. Since, these variables go out of scope once the loop is ended, the compiler shows
the error message saying that these variables were not defined. I have updated your code and
declared the variables outside the loop in the starting due to which they are no more going out of scope
and your program is now compiling without any errors. Note: Changes are highlighted in bold*/
#include <iostream>
using namespace std;
int main ()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Welcome to the Organic Store" << endl;
cout << "Do you have a rewards card? (Y/N)" << endl;
{
int reward;
int more_items,total,reward_total;
int item_number;
int quantity;
cin >> reward;
if (reward=='Y')
{
{
cout << "Please enter your rewards card number" << endl;
}
int card;
cin >> card;
{
if (card != 12345)
cout << "Incorrect reward card number" << endl;
}
}
else
do
{
{
cout << "Please enter a choice" << endl;
cout << "1- View Balance" << endl;
cout << "2- Shop" << endl;
cout << "3- Exit" << endl;
int choice;
cin >> choice;
{
if (choice=='1')
cout <<"Your balance is $100" << endl;
}
{
if (choice=='3')
cout << "Thank you for shopping with us! Have a great day!" << endl;
}
{
if (choice=='2')
cout << "Choose among the three different categories" << endl;
cout << "1- Produce" << endl;
cout << "2- Meat & Fish" << endl;
cout << "3- Beverages" << endl;
int category;
cin >> category;
{
if (category=='1')
cout << "1- Bell peppers-- $2.49 ea." << endl;
cout << "2- Bananas-- $0.59 ea." << endl;
cout << "3- Strawberries 1 lb.-- $4.99" << endl;
cout << "4- Romaine lettuce-- $3.99" << endl;
cout << "5- Sweet potatoes-- $2.49 ea." << endl;
cout << "6- Garlic bulb-- $1.29 ea." << endl;
cout << "7- Lime-- $0.79 ea." << endl;
cout << "8- Cucumber-- $1.99 ea." << endl;
cout << "9- Red onion-- $1.69 ea." << endl;
int produce;
cin >> produce;
}
{
if (category=='2')
cout << "1- Chicken breast 1 lb.-- $5.59" << endl;
cout << "2- Turkey breast slices 6 oz.-- $6.59" << endl;
cout << "3- Ground beef 1 lb.-- $8.99" << endl;
cout << "4- Salmon filet 8 oz.-- $8.29" << endl;
cout << "5- Alaskan halibut 8 oz.-- $14.99" << endl;
int meat;
cin >> meat;
}
{
if (category=='3')
cout << "1- Kombucha 16 oz.-- $3.69" << endl;
cout << "2- Orange juice 59 oz.-- $5.15" << endl;
cout << "3- Coconut water 8 oz.-- $1.79" << endl;
cout << "4- Sparkling water 12 packs-- $5.49" << endl;
cout << "5- Lemonade 16 oz.-- $1.99" << endl;
cout << "6- Black tea 8 oz.-- $2.99" << endl;
int beverage;
cin >> beverage;
}
cout << "Enter item number" << endl;
cin >> item_number;
cout << "Enter quantity" << endl;
cin >> quantity;
cout <<"Do you wish to shop for more items? (Y/N)" << endl;
cin >> more_items;
}
}
}
while (more_items=='Y');
{
if (more_items=='N')
cout << "You're all set!" << endl;
}
total= (item_number*quantity);
{
if(reward==12345)
int reward_total=total*.1;
}
cout << "Thank you for shopping with us! Have a great day!" << endl;
return 0;
}
}