Помогите исправить ошибку в С++
Программа вроде бы прекрасно работает но я пытаюсь включить номер детали в соответствии с инструкциями, но он не отображает это при вызове функции отображения. Пожалуйста, имейте это ввиду, чтобы часть # запрашивалась всякий раз, когда вы хотите изменить запись.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// Define Structure Inventory Date
struct Inventory
{
int month;
int day;
int year;
};
// Define Structure For Inventory Description
struct Item
{
// Declare Variables
string description;
int partNum;
Inventory dateAdded;
int quantity;
float wholesale;
float retail;
};
// Function Prototyes
Inventory stringDate(char * date_ptr);
char * Strchcpy(char * target, char * source, int ch);
int Validate_Date(Inventory date);
Item inputItem();
void outputItem(Item);
void readInventory(string, vector<Item>&);
void writeInventory(string, vector<Item>);
int displayMenu();
void displayRecords(vector<Item>);
int main()
{
// Variables
vector<Item> inventory;
int menuOption;
string fileName;
// Reads in inventory file if there is one if not creates a file
readInventory("Inventory.txt", inventory);
do
{
// Menu Is displayed
menuOption = displayMenu();
switch (menuOption)
{
// Case 1 adds inventory into the file
case 1:
{
inventory.push_back(inputItem());
}
break;
// Case 2 displays a record of an item that you need
case 2:
{
int menuIndex;
do
{
displayRecords(inventory);
cout << "Enter # of record to view: ";
cin >> menuIndex;
// If Vaild input number is not entered it says invalid input
if (menuIndex<0 || menuIndex >= inventory.size())
cout << "Invalid Input!" << endl;
}
while (menuIndex<0 || menuIndex >= inventory.size());
outputItem(inventory[menuIndex]);
}
break;
// Displays records for user to change if needed
case 3:
{
int menuIndex;
do
{
// Asks the user the number of record to change
displayRecords(inventory);
cout << "Enter # of record to change: ";
cin >> menuIndex;
// Validates that the record number is there
if (menuIndex<0 || menuIndex >= inventory.size())
cout << "Invalid Input!" << endl;
}
while (menuIndex<0 || menuIndex >= inventory.size());
inventory[menuIndex] = inputItem();
}
break;
// Closes the program and saves the information
case 4:
{
writeInventory("inventory.txt", inventory);
cout << "Good Bye!" << endl;
}
break;
}
}
while (menuOption != 4);
system("pause");
return 0;
}
// Inventory StringDate copies the date into the file
Inventory stringDate(char * date_ptr)
{
Inventory date;
char month[3],
day[3],
продолжение в комментах
year[3];
char * ch_ptr;
ch_ptr = date_ptr;
ch_ptr = Strchcpy(month, ch_ptr, '/');
++ch_ptr;
ch_ptr = Strchcpy(day, ch_ptr, '/');
++ch_ptr;
Strchcpy(year, ch_ptr, '\0');
date.month = atoi(month);
date.day = atoi(day);
date.year = atoi(year);
return date;
}
char * Strchcpy(char * target, char * source, int ch)
{
while (*source != ch && *source != '\0')
{
*target = *source;
++target;
++source;
}
*target = '\0';
return source;
}
// Validates the date appropriately
// There are leap years which are factored in as well
int Validate_Date(Inventory date)
{
// Normal year days
int normalDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Leap Year days
int leapDays[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (date.month < 1 || date.month > 12)
return 0;
if (date.day < 1)
return 0;
if (date.year % 4 == 0)
{
if (date.day > leapDays[date.month])
return 0;
}
else
if (date.day > normalDays[date.month])
return 0;
if (date.year > 100)
return 0;
return 1;
}
// Output Item to display the records that have been entered thus far
void outputItem(Item output)
{
cout << "\nDescription: " << output.description;
cout << "\nDate: " << output.dateAdded.month << "/" << output.dateAdded.day << "/" << output.dateAdded.year;
cout << "Item Part#: ";
cout << "\nQuantity: " << output.quantity;
cout << "\nWholesale: " << output.wholesale;
cout << "\nRetail: " << output.retail << endl << endl;
}
// Input Item for when a item is entered
Item inputItem()
{
// Variables
Item temp;
char date[9];
// Description of the Item
cout << "Description: ";
cin >> temp.description;
do
{
// Date entered and Validates it
cout << "Date in mm/dd/yy form: ";
cin >> date;
temp.dateAdded = stringDate(date);
if (!Validate_Date(temp.dateAdded))
cout << "Invalid Entry :: Invalid Date!\n";
}
while (!Validate_Date(temp.dateAdded));
do
{
cout << "Item Part#: ";
cin >> temp.partNum;
if(temp.partNum<0)
cout << "Invalid Entry :: Negative!\n";
}
while (temp.partNum<0);
do
{
// Quantity of the item and validates that a
// negative number has not been entered
cout << "Quantity: ";
cin >> temp.quantity;
if (temp.quantity<0)
cout << "Invalid Entry :: Negative!\n";
}
while (temp.quantity<0);
do
{
// Wholesale cost of the item
cout << "Wholesale: ";
cin >> temp.wholesale;
if (temp.wholesale<0)
cout << "Invalid Entry :: Negative!\n";
}
while (temp.wholesale<0);
do
{
// Retail Price
cout << "Retail: ";
cin >> temp.retail;
if (temp.retail<0)
cout << "Invalid Entry :: Negative!\n";
}
while (temp.retail<0);
return temp;
}
// Reads in temporary input and checks
// to see if file has been created already
void readInventory(string fileName, vector&inventory)
{
cout << "\t Opening " << fileName << endl;
fstream file(fileName.c_str(), fstream::in);
if (file.good()) {
cout << "\t\t Success!\n";
while (!file.eof())
{
Item temp;
int readsize = 0;
file >> readsize;
if (readsize) {
file.read((char*)&temp.description, readsize);
file >> temp.dateAdded.day;
file >> temp.dateAdded.month;
file >> temp.dateAdded.year;
file >> temp.partNum;
file >> temp.quantity;
file >> temp.retail;
file >> temp.wholesale;
inventory.push_back(temp);
cout << "\t\t Read item: " << temp.description << endl;
}
}
}
else
cout << "\t\t Failed, a new file will be created when you Save & Exit.\n";
// Closes the file
file.close();
cout << "\t Closed " << fileName << endl;
}
// Writes the inventory into the file
void writeInventory(string fileName, vector inventory)
{
cout << "\t :: Opening " << fileName << endl;
fstream file(fileName.c_str(), fstream::out);
if (file.good()) {
cout << "\t\t :: Success!\n";
for (int x = 0; x<inventory.size(); x++) {
file << sizeof(inventory[x].description);
file.write((char*)&inventory[x].description, sizeof(inventory[x].description));
file << inventory[x].dateAdded.day << endl;
file << inventory[x].dateAdded.month << endl;
file << inventory[x].dateAdded.year << endl;
file << inventory[x].partNum << endl;
file << inventory[x].quantity << endl;
file << inventory[x].retail << endl;
file << inventory[x].wholesale << endl;
cout << "\t wrote item: " << inventory[x].description << endl;
}
}
else
cout << "\t\t Failure!\n";
// Closes the file
file.close();
cout << "\t Closed " << fileName << endl;
}
// Menu of the inventory program
int displayMenu()
{
int choice;
do
{
cout << "-----------------------------" << endl;
cout << "Inventory Program" << endl;
cout << "-----------------------------" << endl;
cout << "1: Add New Record to the File" << endl;
cout << "2: Display Record in the File" << endl;
cout << "3: Change Record in the File" << endl;
cout << "4: Exit" << endl;
cout << "-----------------------------\n";
cout << "Enter Selection: ";
cin >> choice;
if (choice<1 || choice>4)
cout << "Invalid Input!" << endl;
}
while (choice<1 || choice>4);
return choice;
}
// Displays the records that have been entered
void displayRecords(vector inventory)
{
cout << "Records:" << endl;
for (int x = 0; x<inventory.size(); x++)
cout << x << ": " << inventory[x].description << endl;
}
Текст маленький.
Кидай сразу 1000к текста :)