0

I am trying to add a string value (either "Appliance", "Kitchenware", or "Tool")to the C string array "category[CATEGORY_SIZE]" located in the struct newItem but once the user attempts to do this, the program terminates. Can someone explain why this is and how to properly store the string value if I am doing so incorrectly?

#include <fstream>
#include <iostream>
#include <sstream> 
#include <cctype>
using namespace std;

//Array sizes
 const int CATEGORY_SIZE = 15, DATE_SIZE = 12;

//Declare structure for record
struct Item
{
  int SKU;
  string category[CATEGORY_SIZE];
  int quantity;
  double cost;
  string date[DATE_SIZE];
};

int main()
{
  //declare variables
  int answer;
  int cat;
  int month;
  string categoryChoice;
  string monthChoice;
  string theNumberString;
  string theNewNumberString;
  string fullDate;
  int dayChoice;
  int yearChoice;
  char anotherRecord;
  Item newItem; // to hold info about an item.
  fstream dataFile;

  fstream data("test.dat", ios::out | ios::binary);

  cout << "This program allows you to store inventory data for appliances, kitchenware,  
           and tools in a file.";

while (true)
{

cout << "What would you like to do? :\n\n";
cout << "1. Add new records to the file\n";
cout << "2. Display a record in the file\n";
cout << "3. Change any record in the file\n";
cout << "4. Display total wholesale value of a particular item inventory.\n";
cout << "5. Display total wholesale value of a particular category inventory.\n";
cout << "6. Display total quantity of all items in the inventory." << endl;
cout << "Please enter a number[1-6] or enter 0 to quit program: ";
cin >> answer;

if (answer == 0)
{
  return 0;
}

if (answer < 0 || answer > 6)
  cout << "That is not a valid response, please try again. ";

if (answer == 1)
{
  while (true)
  {
    cout << "Enter the following data about an item: \n\n";
    cout << "SKU Number (Stock Keeping Unit Number): ";
    cin >> newItem.SKU;
    cin.ignore(); //Skip over the remaining newline.

    while (true)
    {
      cout << "Please enter what category the item falls in : \n";
      cout << "1. Appliance \n" "2. Kitchenware \n"  "3. Tool: \n" << endl;
      cout << "Please enter a choice [1-3]: ";
      cin >> cat ;

      if (cat < 1 || cat > 3)
        cout << "Invalid choice. Please select again." << endl;
      else if (cat == 1)
      {
        newItem.category[CATEGORY_SIZE] = "Appliance";
        break;
      }
      else if (cat == 2)
      {
        newItem.category[CATEGORY_SIZE] = "Kitchenware";
        break;
      }
      else if (cat == 3)
      {
        newItem.category[CATEGORY_SIZE] = "Tool";
        cout << newItem.category[CATEGORY_SIZE];
        break;
      }

      }
     }
    }
1
  • As a note on style, I recommend declaring variables as close as possible to first use. That way, the declaration is right there when you read the piece of code where they're used and it can limit their scope. Commented Jul 15, 2013 at 3:45

1 Answer 1

1
newItem.category[CATEGORY_SIZE] = "Appliance";

(And likewise): You have undefined behaviour. You're accessing the array out of bounds because indices fall in the range [0, size). Use a std::vector and either push_back or emplace_back (depending on C++11 support):

std::vector<std::string> category; //you can construct this with an initial size
...
newItem.category.push_back("Appliance");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.