I have an assignment to create a CandyBar structure with the members: Name, Weight and Calories. The problem I am having is we are to use a dynamic array and prompt the user for the max amount of candy bars they would like to input overall and then loop through all the candy bars until we reach that amount. In my for() loop i get to the very first prompt and then an "Exited with non zero status" error code. (we also were asked to use a menu)
#include <iostream>
#include <string>
usingnamespace std;
struct CandyBar{
string name;
double weight;
int calories;
};
void cbcMenu();
int getChoice();
int main()
{
int choice;
int CANDY = 0;
do
{
cbcMenu();
choice = getChoice();
if (choice != 4)
{
if (choice == 1)
{
cout << "Enter the total amount of candy bars you wish to input: " << endl;
cin >> CANDY;
CandyBar * candy = new CandyBar[CANDY];
for (int i = 0; i < CANDY; i++)
{
cout << "Enter the name of a candy bar: ";
getline(cin, candy[CANDY].name);
cout << "Enter the candy bars weight: ";
(cin >> candy[CANDY].weight).get();
cout << "Enter the candy bars calories: ";
(cin >> candy[CANDY].calories).get();
}
}
}
}while (choice != 4);
}
//Menu for program
void cbcMenu()
{
cout << "Pick an option from the menu below.\n";
cout << "1. Add Candy\n";
cout << "2. Delete Candy\n";
cout << "3. Display Candy\n";
cout << "4. Quit\n";
}
//Function to make sure you can choose only 1-4 in menu
int getChoice()
{
int choice;
cin >> choice;
while (choice < 1 || choice >4)
{
cout << "You can only choose options 1-4. Please re-enter. ";
cin >> choice;
}
return choice;
}
So I just realized I totally goofed and in my loop candy[CANDY] should have been candy[i] instead...swapped it and it started working just perfectly, now to move on the the rest of the code and hopefully no more errors.