C++ inventory item removal - c++

Increase the inventory to be 10 items. DONE!!!
Create a for loop that asks the user to input the initial items in the inventory.DONE!!!
After the for loop create a minor story where the healer changes two items (i.e. items 4 and 8).What I want is to SWAP an item for another item ie would you like to trade your {[item] for [item2] y or n
Sort the inventory in alphabetical order. You can use your own sort if you want, but here is a bubble sort algorithm:
// A simple inventory program using a struct to store data
// in an array.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
// define a data structure
struct InventoryRecord
{
string name; // inventory item name
int qty; // how many are in stock
double value; // the value
};
// const for the max size of the record array
const int MAX_SIZE = 9;
// function prototypes
void addData(InventoryRecord list[], int& size);
void dispData(const InventoryRecord list[], int size);
void remData( const InventoryRecord list[], int size);
void saveFile(const InventoryRecord list[], int size);
void openFile(InventoryRecord list[], int& size);
char getMenuResponse();
int main(int argc, char *argv[])
{
InventoryRecord recList[MAX_SIZE];
int numOfRecs = 0;
bool run = true;
do
{
cout << "Hero's Inventory - " << numOfRecs << " items in your bag" << endl;
switch ( getMenuResponse() )
{
case 'A': addData(recList, numOfRecs); break;
case 'D': dispData(recList, numOfRecs); break;
case 'R': remData(recList, numOfRecs); break;
case 'O': openFile(recList, numOfRecs); break;
case 'S': saveFile(recList, numOfRecs); break;
case 'Q': run = false; break;
default : cout << "That is NOT a valid choice" << endl;
}
} while (run);
cout << endl << "Program Terminated" << endl;
// system("PAUSE"); // Program exits immediatly upon "Quit" if commented out
return EXIT_SUCCESS;
}
// Task: Allow data entry of one inventory item
// Accepts: References to the inventory array and its size
// Returns: Nothing
// Modifies: The array and size 'actual parameter'
// NOTE: Could be modified to allow entry of more than one item
void addData(InventoryRecord list[], int& size)
{
InventoryRecord tmp; // declare a temp item that we will load before putting in the array
char response;
char str[256]; // needed for cin.getline; we are going to use a char array
if (size < MAX_SIZE) {
system("cls");
cout << "Please enter 10 items helpful to your quest! " << endl;
cout << "Enter item: " << endl << endl;
cout << "Name: ";
// Get up to 256 characters from the keyboard including white space.
// Stop reading if encounter the \n first. If there's any chance of
// more than 256 characters you will have to clean up cin with
// cin.ignore before the next input.
cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
tmp.name = str;
cout << "Quantity: ";
cin >> tmp.qty;
cout << "Value: ";
cin >> tmp.value;
cout << endl;
// see if this record should be added to the array
cout << "Add to inventory? (y/n) ";
cin >> response;
if (toupper(response) == 'Y')
list[size++] = tmp;
} else {
cout << "Inventory is full; cannot enter more units." << endl;
system("pause");
}
system("cls");
}
void dispData(const InventoryRecord list[], int size)
{
system("cls");
double cost = 0;
if(size < 1) {
cout << "Nothing to display" << endl;
} else {
cout << "All Items in your Bag" << endl << endl;
cout << fixed << setprecision(2);
cout << "Item Name Qty Value" << endl;
cout << "~~~~~~~~~~~~~~~~~~" << endl;
cout << left;
for (int i = 0; i < size; i++) {
cout << setw(21) << list[i].name << right
<< setw(4) << list[i].qty
<< setw(10) << list[i].value << left << endl;
cost = cost + list[i].value * list[i].qty;
}
cout << "~~~~~~~~~~~~~~~~~~~" << endl;
cout << right << setw(3) << size;
cout << " items listed";
cout << right << setw(19) << cost << endl << endl;
}
system("PAUSE");
system("cls");
}
void remData(const InventoryRecord list[], int size) {
system("cls");
cout <<"Enter Item you wish to remove from your inventory: " << endl;// This is being displayed so user can see items in the inventory
double cost = 0;
if(size < 1) {
cout << "Nothing to display" << endl;
} else {
cout << "All Items in your Bag" << endl << endl;
cout << fixed << setprecision(2);
cout << "Item Name Qty Value" << endl;// It is not displaying right the alignment is off
cout << "~~~~~~~~~~~~~~~~~~" << endl;
cout <<"Item Name: ";/* from here I do not know what to do! What I want is have use type the item name they want removed
also display an error if they enter an item wrong*/
cout << left;
for (int i = 0; i < size; i++) {
cout << setw(21) << list[i].name << right
<< setw(4) << list[i].qty
<< setw(10) << list[i].value << left << endl;
cost = cost + list[i].value * list[i].qty;
}
cout << "~~~~~~~~~~~~~~~~~~~" << endl;
cout << right << setw(3) << size;
cout << " items listed";
cout << right << setw(19) << cost << endl << endl;
}}
// Save records to disc
void saveFile(const InventoryRecord list[], int size) {
ofstream outfi("Inventory.txt");
// make sure the file stream is open before doing IO
if (!outfi.fail()) {
system("cls");
cout << "Saving inventory to the disc ";
for(int i = 0; i < size; i++) {
outfi << list[i].name << ';'
<< list[i].qty << ';'
<< list[i].value;
// Start a new line after all but the last record
// Simplifies reading the file as EOF is at end of last line
if (i < size-1) outfi << endl;
}
cout << endl << size << " records writen to the disc." << endl;
outfi.close();
system("PAUSE");
system("cls");
}
else {
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
// Open file and load array
void openFile(InventoryRecord list[], int& size)
{
ifstream infi("Inventory.txt");
string str;
stringstream strstrm;
// make sure the file stream is open before doing IO
if (!infi.fail()) {
system("cls");
cout << "Reading inventory from the disc ";
size = 0; // overwrite any existing records
while(!infi.eof() && size < MAX_SIZE) {
// get and store the name
getline(infi, str, ';');
list[size].name = str;
// get, convert and store the quantity
getline(infi, str, ';');
strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
strstrm << str;
strstrm >> list[size].qty;
// get, convert and store the cost
getline(infi, str);
strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
strstrm << str;
strstrm >> list[size++].value;
}
cout << endl << size << " records read from the disc." << endl;
system("PAUSE");
system("cls");
}
else { // something went wrong with opening the file
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
char getMenuResponse()
// Task: Put the menu on screen and get a response
// Accepts: Nothing
// Returns: The users response
// Modifies: Nothing
// NOTE: Characters are far more intuitive at the command
// line than numbers; avoid using numbers.
{
char response;
cout << endl << "Make your selection" << endl
<< "(A)dd Items, (D)isplay Items, (R)emove items, (O)pen File, (S)ave File, (Q)uit" << endl
<< "> ";
cin >> response;
cin.ignore(256, '\n');
// clean-up up to 256 chars including the delimiter specified (\n, the endl)
// OR stop when the \n is encountered after removing it.
return toupper(response);
}

To remove an element from your inventory, you just need to move all elements after the removed element one position forward. Once that is done, you also need to reduce the lengthy by one. For example, to remove the elements at the position index from an array array with currently length elements you could use
if (index < length) {
std::copy(array + index + 1, array + length, array + index);
++length;
}

There are essentially two ways to remove the element, the easiest is swap the element which you want to delete with the last one and resize the list:
void deleteElem(Data[] list, int & listLength, int ix) {
if (ix < listLength - 1) {
list[ix] = list[listLength - 1];
}
--listLength;
}
The second solution is memmove everthing after the to deleting element one to the left:
void deleteElem(Data[] list, int & listLength, int ix) {
memmove(list[ix], list[ix + 1], (listLength - ix - 1) * sizeof(Data));
--listLength;
}
EDIT: There was an error in the length for memmove, always the same. You should read the documentation.

Related

Binary Search Output Issue in Inventory Program c++

I am having issues with binary search function within my homework (I am completely done with it except for this one issue).
The program allows a user to manage the inventory of a small store that sells various products of any type. It will first read the inventory from a text file named “inventory.dat”, reading the product name, sku, quantity, and price. It will contain up to 100 products.
Then, it should offer the user a menu with the following options:
Display the inventory sorted by sku. (displayInventory function)
Lookup a product by sku. (lookupSku function)
Lookup a product by name. (lookupname function)
Quit
The program should perform the selected operation and then re-display the menu.
When I select option 2 and enter a sku number, my program outputs a random item's information instead of the one that was typed in by the user.
Here is my code starting in the main function along with the lookupBySku function:
int main()
{
// Initialize variables & array
int userInput;
//max array size
const int MAX_SIZE = 100; //const to make it constantly be 100
//declare and open input file
ifstream fin;
fin.open("inventory.dat");
//declare strings to hold data
string productName, sku, quantity, price;
if (!fin) // checking if file was inputted correctly or not.
{
cout << "File not found! Try again. " << endl;
return 1;
}
Inventory items[MAX_SIZE];
Inventory temp[MAX_SIZE];
cout << left << setw(20) << "Product Name " <<
left << setw(10) << "sku #" <<
left << setw(3) << "Quantity " <<
left << setw(0) << " Price\n";
//declare number of objects in inventory
int numOfObjects = 0;
while(!fin.eof())
{
getline(fin, productName);
getline(fin, sku);
getline(fin, quantity);
getline(fin, price);
if(productName == "")
{
break;
}
items[numOfObjects].name = productName;
cout << left << setw(20) << items[numOfObjects].name;
items[numOfObjects].sku = sku;
cout << left << setw(10) << items[numOfObjects].sku;
items[numOfObjects].quantity = quantity;
cout << left << setw(4) << items[numOfObjects].quantity;
items[numOfObjects].price = price;
cout << left << setw(0) <<"\t\t" << items[numOfObjects].price << endl;
++numOfObjects;
}
//check to see if there are more than 100 items
if(numOfObjects > 100)
{
cout << "File is too large, terminating program...\n";
return -1;
}
// Constants for menu choices
const int DISPLAY_BY_SKU = 1,
LOOKUP_BY_SKU = 2,
LOOKUP_BY_NAME = 3,
QUIT_CHOICE = 4;
do
{
// Set up numeric output formatting.
cout << fixed << showpoint << setprecision(2);
cout << "Manage Inventory Menu\n" <<endl;
cout << "1. Display inventory sorted by sku. " <<
"\n2. Lookup a product by sku. " <<
"\n3. Lookup a product by name. " <<
"\n4. Quit the program\n" << endl;
cout << "Enter your choice : ";
cin >> userInput;
cout << endl;
while (userInput < DISPLAY_BY_SKU || userInput > QUIT_CHOICE)
{
cout << "Please enter a valid menu choice: ";
cin >> userInput;
}
switch(userInput)
{
case DISPLAY_BY_SKU:
{
displayInventory(items, numOfObjects);
break;
}
case LOOKUP_BY_SKU:
{
lookupSku(items, numOfObjects);
break;
}
case LOOKUP_BY_NAME:
{
lookupName(items, numOfObjects);
break;
}
case 4:
cout << "Exiting the program." <<endl;
break;
}
}
while (userInput != QUIT_CHOICE);
fin.close();
return 0; }
void lookupSku (Inventory items[], int numOfObjects) {
string number;
int first = 0;
int last = numOfObjects - 1;
int middle;
int position = -1;
bool found = false;
cout << "Enter the sku that you'd like to search for : ";
cin >> number;
cout << endl;
while(!found && first <= last)
{
middle = (first + last)/2;
if(items[middle].sku == number)
{
found = true;
position = middle;
}
else if(items[middle].sku > number)
{
last = middle - 1;
}
else if(items[middle].sku < number)
{
first = middle + 1;
}
}
cout << "Your item is shown below : \n" << endl
<< "Product Name : "<< items[middle].name << endl
<< " Sku : " << items[middle].sku << endl
<<" Quantity : " << items[middle].quantity << endl
<< " Price : " << items[middle].price << endl
<< endl;
}

Dynamically Allocating String Arrays

This is what I have so far. I am trying to edit a dynamically allocated array in C++, however, when the for loop runs, it is skipping over the first item. I need it to get all of the items. Any help is appreciated. Thanks in advance.
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// Declare Variables
int userChoice = 0;
int numItems = 0;
cout << "How many items will be on your list? ";
cin >> numItems;
string *list = new string[numItems];
// Give the user some options
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Sort Items" << endl;
cout << "4. Exit" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
cout << endl;
// Perform the operation
switch(userChoice)
{
case 1:
{
cin.clear(); // Remove new line from cin
for(int i = 0; i < numItems; i++)
{
cout << "Item #" << i + 1 << " --> ";
getline(cin, list[i]);
}
}
break;
case 2:
{
}
break;
case 3:
{
}
break;
case 4:
{
return 0;
}
default:
{
cout << "Error! Invalid Selection" << endl;
}
}
// Output the list
cout << "-------Items-------" << endl
<< *list << endl << endl;
// free memory
delete [] list;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
return 0;
}
It seems odd to me to be using the STL for some things (like string) and then use a standard array to hold the strings. Also, list is a standard type of object in STL, so that is not a great name for a variable. This revised code fixes your issues with ignoring the first line, and also uses a vector instead of doing new and delete.
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
// Declare Variables
int userChoice = 0;
int numItems = 0;
cout << "How many items will be on your myList? ";
cin >> numItems;
cin.ignore ();
vector<string> myList;
while (true)
{
// Give the user some options
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Sort Items" << endl;
cout << "4. Exit" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
cin.ignore ();
cout << endl;
// Perform the operation
switch(userChoice)
{
case 1:
{
for(int i = 0; i < numItems; i++)
{
cout << "Item #" << i + 1 << " --> ";
string s;
getline(cin, s);
myList.push_back (s);
}
}
break;
case 2:
{
}
break;
case 3:
{
sort (myList.begin (), myList.end ());
}
break;
case 4:
{
return 0;
}
default:
{
cout << "Error! Invalid Selection" << endl;
}
}
// Output the myList
cout << "-------Items-------" << endl;
copy(myList.begin(), myList.end(), ostream_iterator<string>(cout, "\n"));
} // end of while
}
The project description explicitly says I can't use standard containers, sort functions, or smart pointers
Redone below to not use those things. :)
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
using namespace std;
int myCompare (const void * a, const void * b)
{
if ((*(string *) a) < *(string *) b) return -1;
if ((*(string *) a) > *(string *) b) return 1;
return 0;
}
int main()
{
// Declare Variables
int userChoice = 0;
int numItems = 0;
cout << "How many items will be on your myList? ";
cin >> numItems;
cin.ignore ();
string *myList = new string[numItems];
while (true)
{
// Give the user some options
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Sort Items" << endl;
cout << "4. Exit" << endl;
cout << "Enter the number of the operation you wish to perform: ";
cin >> userChoice;
cin.ignore ();
cout << endl;
// Perform the operation
switch(userChoice)
{
case 1:
{
for(int i = 0; i < numItems; i++)
{
cout << "Item #" << i + 1 << " --> ";
getline(cin, myList [i]);
}
}
break;
case 2:
{
}
break;
case 3:
{
qsort (myList, numItems, sizeof (string *), myCompare);
}
break;
case 4:
{
delete [] myList;
return 0;
}
default:
{
cout << "Error! Invalid Selection" << endl;
}
}
// Output the myList
cout << "-------Items-------" << endl;
for(int i = 0; i < numItems; i++)
cout << myList [i] << endl;
} // end of while
}
The for loop isn't skipping the first element. The first element is just a an empty line.
Because the following clears the error flags.
cin.clear(); // Remove new line from cin --> No!!!!
If you want to skip until the new line you have to use ignore() instead.
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // this removes until newline from cin !

Search in Dynamic Array

When I'm searching an integer in my dynamic array, the search function isn't working well as its always showing its positioned at 1. whether the data is actually there or not.
What i'm actually trying to do is using dynamic data structure, I'm adding the data. Deleting, searching and saving to txt file. and Loading it back. But the problem is search. I used switch cases and search is at Case 4.
#include<iostream>
#include<string>
#include<fstream> //to save file in text
using namespace std;
int main()
{
int *p1;
int size = 0;
int counter = 0;
p1 = new int[size];
int userchoice;
int i;
int position;
while (1)
{
cout << "Please enter your choice " << endl;
cout << endl;
cout << "To insert Press '1'" << endl;
cout << "To Delete press '2'" << endl;
cout << "To View press '3'" << endl;
cout << "To Search press '4'" << endl;
cout << "To Save Press '5'" << endl;
cout << "To Load Previously saved Data press '6'" << endl;
cout << "To Exit press '7'" << endl;
cout << endl;
cout << "Enter your choice: ";
cin >> userchoice;
switch (userchoice) // User's selection from the menu
{
case 1: //Insert Number
cout << "Enter a Number: ";
cin >> p1[size];
counter++;
size++; //Add's memory space
break;
case 2: //Delete Number
int udelete;
cout << "Enter a number to delete: ";
cin >> udelete; //User enters Number to be deleted
//Checking if the number is in an array.
for (position = 0; position<size; position++)
{
if (p1[position] == udelete)
break;
}
if (position>size)
{
cout << "The number is not in the memory: ";
cout << endl;
break;
}
for (i = position; i<size; i++) {
p1[i] = p1[i + 1];
}
size--;
cout << "Successfully Deleted!!! ";
cout << endl;
break;
case 3: // View
for (i = 0; i<size; i++)
{
cout << "Your data" << " " << i << " " << "-->" << p1[i] << endl;
}
break;
case 4:
{
int usearch;
cout << "Please enter the figure you would like to search ";
cout << "->";
cin >> usearch;
for (i = 0; i>size; i++)
{
if (p1[size] == usearch)
break;
}
if (usearch == size)
{
cout << "not found. ";
}
cout << "Position at: " << i + 1 << endl;
break;
}
case 5: // Save
{
ofstream save;
save.open("Dynamicdata.txt", ofstream::out | ofstream::app);
for (i = 0; i<size; i++)
{
save << p1[i] << endl;
}
save.close();
cout << "File Saved " << endl;
break;
}
case 6: //Read from File
{
string read;
ifstream file_("Dynamicdata.txt");
if (file_.is_open())
{
while (getline(file_, read))
{
cout << read << "\n";
}
file_.close();
}
else
cout << "File Not open" << endl;
cin.get();
break;
}
case 7:
{
return 0;
}
}
}
}
Your problem is that the size of your array is 0. Here you set size to 0 and then size for the size of p1
int size=0;
int counter=0;
p1 = new int[size];
You are going to need to make size bigger so that you can actually store elements in p1 or instead of using arrays and dynamic memory allocation use a std::vector and let it handle that for you.
The code has undefined behaviour because initailly the dynamically allocated array pointed to by pointer p1 has no elements
int size=0;
^^^^^^^^^^
//...
p1 = new int[size]; // size is equal to 0
So in the following code snippet an atttempt to write data to p1[size] results in undefined behaviour
case 1: //Insert Number
cout<<"Enter a Number: ";
cin>>p1[size]; // undefined behaviour
^^^^^^^^^^^^^
counter++;
size++; //Add's memory space
break;
You need to reallocate the array to reserve memory for the added new element.
Take into account that for example this loop
for (i = 0; i>size; i++)
^^^^^^^
{
if (p1[size] == usearch)
break;
}
will never iterate because variable i set to zero can not be greater than size that at least equal to zero.
And it would be logically more correct to write
if (p1[i] == usearch)
^^^^^
instead of
if (p1[size] == usearch)
^^^^^^^^
Consequently this if statement
if (usearch == size)
^^^^^^^
{
cout << "not found. ";
}
should be replaced with this for statement
if (i == size)
^^
{
cout << "not found. ";
}

use function to filled an array in number. if the array is full, then the number will be filled on the second array

I working on a program store registered student id in array called registered. and then if that array is full then it will automatically store the student id in another array called waitlist. this action will be done with a function.
I wont be able to make my computer to store the student id into waitlist array when my registered array is full. i am only can use the specific function to stored student id into both array. i also need to print list of student id that is registered and in waitlist by using the same function.
I don't know what to do, i could not find a good tutorial and book is not even helpful at all. Thank you.
here is my code.
#include <iostream>
using namespace std;
const int maxCap = 10;
void showOption();
void addToRear(int arr[], int& howMany, int value);
void printArray(int arr[], int howMany);
bool isFull(int capacity, int howMany);
bool isEmpty(int howMany);
int main()
{
bool repeat = false;
bool repeatOption = false;
int reg[maxCap];
int waitlist[maxCap];
int option, value, capacity;
int howMany = 0;
int registered = 0;
int waitlisted = 0;
do
{
showOption();
cout << "please enter your option: 1 ~ 5 " << endl;
cin >> option;
switch (option)
{
case 1:
if(isEmpty(capacity, howMany) == true)
addToRear(reg, howMany, maxCap);
if(isFull(capacity, howMany) == true)
addToRear(waitlist, howMany, maxCap);
break;
case 3:
printArray(reg, howMany);
break;
case 4:
printArray(waitlist, howMany);
break;
case 5:
cout << "thank you for using this program. " << endl;
return 0;
break;
default:
cout << "thank you for using this program. " << endl;
break;
}
//cout << "there are " << howMany << " registered student in this class. " << endl;
cout << "back to main menu? enter 1 for yes: " << endl;
cin >> repeatOption;
}while(repeatOption);
}
void showOption()
{
cout << "Main Menu: " << endl;
cout << "1) Register Student. " << endl;
cout << "2) Unregister Student. " << endl;
cout << "3) Print list of registered student. " << endl;
cout << "4) Print list of waitlisted student. " << endl;
cout << "5) Exit. " << endl;
cout << " " << endl;
}
//void getID(int& value)
//enter student id and store it in array register and waitlist
void addToRear(int arr[], int& howMany, int value)
{
cout << "there are only " << maxCap << " spot for student to be registered." << endl;
cout << "if there are " << maxCap << " students registered in this class, " << endl;
cout << "then the rest of the student will be put on waitlist. " << endl;
cout << " " << endl;
cout << "enter student ID. " << endl;
cout << "enter negative value to stop. " << endl;
int id;
cin >> id;
while((id >= 0) && (howMany < maxCap))
{
arr[howMany] = id;
howMany++;
cin >> id;
}
}
//print list of student that is registered or waitlisted
void printArray(int arr[], int howMany)
{
cout << "there are " << howMany << " number of student in registered list. " << endl;
cout << "the student ID number is " << endl;
for (int index = 0; index < howMany; index++)
{
cout << arr[index];
cout << " " << endl;
}
}
bool isFull(int capacity, int howMany)
{
/if((capacity < 1) || (howMany > 9))
return true;
else
return false;
}
bool isEmpty(int capacity, int howMany)
{
if((capacity > 9) || (howMany < 1))
return true;
else
return false;
}
It would be easier to have a single array that has both registered and wait listed candidates.
While adding you just have to have an end index. So as long as the index is less than your cutoff number your candidate is registered.
While deleting you have to move the list up one space after the one candidate entry deleted. This way the registered list will automatically be updated to reduce the waitlisted candidate by 1.
Printing you have to check the index if it is greater than cutoff you have both registered and wait list.
Your code is failing to identify full and empty condition for registration list and waiting list. I have made some modification in the below code. you can try below code snippet.
case 1:
int len = sizeof(reg)/sizeof(reg[0]); // Get the length of reg list
int waitlistLen = sizeof(waitlist)/sizeof(waitlist[0]); //get length of wait list
if(maxCap > len) // Check if reg list not full. Add to reg list
addToRear(reg, howMany, maxCap);
else if(maxCap > waitlistLen) // if reg list is full. Check if wait list is full or not
addToRear(waitlist, howMany, maxCap);
else // if both the list are full show user option menu and break.
showOption();
break;

Why won't my ofstream work when I put it outside my while statement?

Every time I do anything, and my while(1) gets called in my main function, my file gets cleared. It's driving me crazy. I've tried EVERYTHING. I try to put my ofstream out("data.dat"); outside the while(1) statement so it isn't called everytime but then nothing is written to the file like the ofstream isn't even working!
I've tried to make the ofstream static, so it isn't called over and over again like:
static ofstream open("data.dat");
That doesn't work.
And like I said, when I put the ofstream OUTSIDE the while statement, nothing is written to the file. Like:
ofstream out("data.dat");
while (1)
{
string line = "";
cout << "There are currently " << structList.size() << " items in memory.";
cout << endl << endl;
cout << "Commands: " << endl;
cout << "1: Add a new record " << endl;
cout << "2: Display a record " << endl;
cout << "3: Edit a current record " << endl;
cout << "4: Delete a record " << endl;
cout << "5: Save current information " << endl;
cout << "6: Exit the program " << endl;
cout << endl;
cout << "Enter a command 1-6: ";
getline(cin , line);
int rValue = atoi(line.c_str());
system("cls");
switch (rValue)
{
case 1:
structList = addItem(structList);
break;
case 2:
displayRecord(structList);
break;
case 3:
structList = editRecord(structList);
break;
case 4:
deleteRecord(structList);
break;
case 5:
if (!structList.size()) { cout << "There are no items to save! Enter one first!" << endl << endl; system("pause"); system("cls"); break; }
writeVector(out , structList);
break;
case 6:
return 0;
default:
cout << "Command invalid. You can only enter a command number 1 - 6. Try again. " << endl;
}
out.close();
}
And can someone tell me why my check to prevent reading of a empty file won't work?
My Check:
bool checkFileEmpty()
{
ifstream in("data.dat");
if (in.peek() == in.eofbit)
{
return true;
}
return false;
}
I am so sick and tired of my program crashing on startup over and over again because my vector is getting set to a size of 200 million. I've tried a BUNCH of stuff for this... none of it works... Please GOD someone help me with both of these! I've been up for 18 hours working on this ( all night yes ) and i'm ALMOST done. I'm begging you....
My Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace System;
using namespace std;
#pragma hdrstop
bool isValidChoice(int size, int choice);
bool checkFileEmpty();
template<typename T>
void writeVector(ofstream &out, const vector<T> &vec);
template<typename T>
vector<T> readVector(ifstream &in);
template<typename T>
vector<T> addItem(vector<T> &vec);
template<typename T>
void printItemDescriptions(vector<T> &vec);
template<typename T>
int displayRecord(vector<T> &vec);
template<typename T>
vector<T> editRecord(vector<T> &vec);
template<typename T>
vector<T> deleteRecord(vector<T> &vec);
struct InventoryItem {
string Description;
int Quantity;
int wholesaleCost;
int retailCost;
string dateAdded;
} ;
int main(void)
{
cout << "Welcome to the Inventory Manager extreme! [Version 1.0]" << endl;
ifstream in("data.dat");
if (in.is_open()) { cout << "File \'data.dat\' has been opened successfully." << endl; } else { cout << "Error opening data.dat" << endl;}
cout << "Loading data..." << endl;
vector<InventoryItem> structList = readVector<InventoryItem>( in );
cout <<"Load complete." << endl << endl;
in.close();
while (1)
{
string line = "";
cout << "There are currently " << structList.size() << " items in memory.";
cout << endl << endl;
cout << "Commands: " << endl;
cout << "1: Add a new record " << endl;
cout << "2: Display a record " << endl;
cout << "3: Edit a current record " << endl;
cout << "4: Delete a record " << endl;
cout << "5: Save current information " << endl;
cout << "6: Exit the program " << endl;
cout << endl;
cout << "Enter a command 1-6: ";
getline(cin , line);
int rValue = atoi(line.c_str());
system("cls");
ofstream out("data.dat");
switch (rValue)
{
case 1:
structList = addItem(structList);
break;
case 2:
displayRecord(structList);
break;
case 3:
structList = editRecord(structList);
break;
case 4:
deleteRecord(structList);
break;
case 5:
if (!structList.size()) { cout << "There are no items to save! Enter one first!" << endl << endl; system("pause"); system("cls"); break; }
writeVector(out , structList);
break;
case 6:
return 0;
default:
cout << "Command invalid. You can only enter a command number 1 - 6. Try again. " << endl;
}
out.close();
}
system("pause");
return 0;
}
template<typename T>
void writeVector(ofstream &out, const vector<T> &vec)
{
out << vec.size();
for(vector<T>::const_iterator i = vec.begin(); i != vec.end(); i++)
{
out << *i;
}
cout << "Save completed!" << endl << endl;
}
ostream &operator<<(ostream &out, const InventoryItem &i)
{
out << i.Description << ' ';
out << i.Quantity << ' ';
out << i.wholesaleCost << ' ' << i.retailCost << ' ';
out << i.dateAdded << ' ';
return out;
}
istream &operator>>(istream &in, InventoryItem &i)
{
in >> i.Description;
in >> i.Quantity;
in >> i.wholesaleCost >> i.retailCost;
in >> i.dateAdded;
return in;
}
template<typename T>
vector<T> readVector(ifstream &in)
{
size_t size;
if (checkFileEmpty())
{
size = 0;
} else {
in >> size;
}
vector<T> vec;
vec.reserve(size);
for(unsigned int i = 0; i < size; i++)
{
T tmp;
in >> tmp;
vec.push_back(tmp);
}
return vec;
}
template<typename T>
vector<T> addItem(vector<T> &vec)
{
system("cls");
string word;
unsigned int number;
InventoryItem newItem;
cout << "-Add a new item-" << endl << endl;
cout << "Enter the description for the item: ";
getline (cin , word);
newItem.Description = word;
cout << endl;
cout << "Enter the quantity on hand for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.Quantity = number;
cout << endl;
cout << "Enter the Retail Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.retailCost = number;
cout << endl;
cout << "Enter the Wholesale Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.wholesaleCost = number;
cout << endl;
cout << "Enter current date: ";
getline (cin , word);
newItem.dateAdded = word;
vec.push_back(newItem);
return vec;
}
template<typename T>
void printItemDescriptions(vector<T> &vec)
{
int size = vec.size();
if (size)
{
cout << "---------------------------------" << endl;
cout << "| ~ Item Descriptions ~ |" << endl;
cout << "---------------------------------" << endl;
cout << "*********************************" << endl;
for (int i = 0 ; i < size ; i++)
{
cout << "(" << i+1 << ")" << ": " << vec[i].Description << endl;
}
cout << "*********************************" << endl << endl;
}
}
template<typename T>
int displayRecord(vector<T> &vec)
{
string word = "";
string quit = "quit";
int choice = 1;
int size = vec.size();
if (size)
{
printItemDescriptions(vec);
cout << endl;
while (1)
{
cout << "Type \"exit\" to return to the Main Menu." << endl << endl;
cout << "Enter \"list\" to re-display the items." << endl << endl;
cout << endl;
cout << "Pick the number of the item you would like to display: ";
getline (cin , word);
if (convertToLower(word) == "exit") { system("cls"); return 0; }
if (convertToLower(word) == "list") { system("cls"); displayRecord(vec); }
choice = atoi(word.c_str());
choice -= 1;
if (isValidChoice(size, choice))
{
system("cls");
cout << endl << "[Item (" << choice << ") details] " << endl << endl;
cout << "******************" << endl;
cout << "* Description * " << vec[choice].Description << endl;
cout << "******************" << endl << endl;
cout << "******************" << endl;
cout << "*Quantity On Hand* " << vec[choice].Quantity << endl;
cout << "******************" << endl << endl;
cout << "******************" << endl;
cout << "* Wholesale Cost * " << vec[choice].wholesaleCost << endl;
cout << "****************** " << endl << endl;
cout << "******************" << endl;
cout << "* Retail Cost * " << vec[choice].retailCost << endl;
cout << "****************** " << endl << endl;
cout << "******************" << endl;
cout << "* Data Added * " << vec[choice].dateAdded << endl;
cout << "****************** " << endl << endl;
} else { system("cls"); cout << "That item doesn't exist!" << endl; cout << "Pick another item or enter \"list\" to see available items." << endl << endl; }
}
} else { cout << "There are currently no items to display." << endl << endl; system("pause"); system("cls"); return 0; }
return 1;
}
bool isValidChoice(int size, int choice)
{
for (int i = 0 ; i <= size ; i++)
{
if (choice == i) { return true; }
}
return false;
}
string convertToLower(string word)
{
for (unsigned int i = 0 ; i < word.size() ; i++)
{
word[i] = tolower(word[i]);
}
return word;
}
bool checkFileEmpty()
{
ifstream in("data.dat");
if (in.peek() == in.eofbit)
{
return true;
}
return false;
}
template<typename T>
vector<T> editRecord(vector<T> &vec)
{
string word;
int choice;
printItemDescriptions(vec);
cout << "Choose item to edit: ";
getline ( cin, word );
choice = atoi(word.c_str());
system("cls");
unsigned int number;
InventoryItem newItem;
cout << "-Edit an item-" << endl << endl;
cout << "Enter the description for the item: ";
getline (cin , word);
vec[choice-1].Description = word;
cout << endl;
cout << "Enter the quantity on hand for the item: ";
getline (cin , word);
number = atoi(word.c_str());
vec[choice-1].Quantity = number;
cout << endl;
cout << "Enter the Retail Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
vec[choice-1].retailCost = number;
cout << endl;
cout << "Enter the Wholesale Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
vec[choice-1].wholesaleCost = number;
cout << endl;
cout << "Enter current date: ";
getline (cin , word);
vec[choice-1].dateAdded = word;
system("cls");
cout << "Item edited successfully! " << endl;
return vec;
}
template<typename T>
vector<T> deleteRecord(vector<T> &vec)
{
if (!vec.size()) { cout << "There are no items to delete!" << endl << endl; return vec; }
printItemDescriptions(vec);
string word;
int choice;
cout << "Choose item to delete: ";
getline( cin, word);
choice = atoi(word.c_str());
vec.erase (vec.begin()+choice-1);
return vec;
}
You'd better move the ofstream openning and closing inside case 5.
Here you create a new file at each while iteration.
case 5:
{
ofstream out("data.dat");
writeVector(out , structList);
out.close();
}
break;
ofstream out("data.dat");
Opens the file for writing. By default, it will start at the very beginning wiping out anything that was there previously. First, use a different output file than the file you are reading from.
Try adding the close to the case 6 statements:
case 6:
out.close();
return 0;
I'm pretty sure the close isn't getting called as the return will exit main before getting to that statement. With the file unclosed you are left with a buffer unflushed and I suspect that will leave the data unwritten.
You should move the open to before the while loop and also remove the out.close() from the while loop as it's going to close the file after the first menu selection.
To check if a file is empty or cannot be opened
bool IsEmpty( const std::string & filename ) {
std::ifstream ifs( filename.c_str() );
if ( ifs.is_open() ) {
std::string line;
return ! std::getline( ifs, line );
}
else {
return true;
}
}