2013-07-15 2 views
0

J'essaie d'ajouter une valeur de chaîne (soit "Appliance", "Kitchenware" ou "Tool") au tableau de chaînes C "category [CATEGORY_SIZE]" situé dans la structure newItem mais une fois que l'utilisateur tente de le faire, le programme se termine. Quelqu'un peut-il expliquer pourquoi c'est ainsi et comment stocker correctement la valeur de chaîne si je le fais de manière incorrecte?Ajouter une valeur C-string dans Structure C++

#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; 
     } 

     } 
    } 
    } 
+0

Comme une note sur le style, je vous recommande de déclarer des variables aussi près que possible de la première utilisation. De cette façon, la déclaration est juste là quand vous lisez le morceau de code où ils sont utilisés et cela peut limiter leur portée. – chris

Répondre

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

(Et même): Vous avez un comportement non défini. Vous accédez au tableau hors limites parce que les indices tombent dans la plage [0, size). Utilisez un std::vector et soit push_back ou emplace_back (selon le support 11 C++):

std::vector<std::string> category; //you can construct this with an initial size 
... 
newItem.category.push_back("Appliance");