I'm trying to get this to save as a float but each time I attempt it's saving data type int to both the temp array and the text file and just creating what looks like a garbage value for the float value entered.
Here is the structure.
1 2 3 4 5 6 7
struct InventoryRecord
{
string name; // inventory item name
int qty; // how many are in stock
int number; // the item number
float price; //the price of the 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 << "Enter Inventory Records" << endl << endl;
cout << "Name: ";
// Get up to 256 characters from the keyboard including white space.
// Stop reading if encounter the \n first.
cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
tmp.name = str;
cout << "Quantity: ";
cin >> tmp.qty;
cout << "Item Number: ";
cin >> tmp.number;
cout << "Item Price: ";
cin >> tmp.price;
cout << endl;
// see if this record should be added to the array
cout << "Add the record to inventory? (y/n) ";
cin >> response;
if (toupper(response) == 'Y')
list[size++] = tmp;
}
else {
cout << "Inventory full; cannot enter more." << endl;
system("pause");
}
system("cls");
}
From testing your program I suspect the main problem is that you are mixing cin.getline() (which gets past the EOL character '\n') and cin >> (which doesn't). I had to add the single line cin.ignore( 256, '\n' ); // Added line
to your code to make it work (otherwise it skipped asking for name).
You obviously need to initialise size to 0 before any calls to addData (but I'm relying on you having done that).
The test code that I wrote to check is below and it seems to work. I added a simple int main() function - perhaps you could consider doing the same in future, so that others are able to check compileable code without the whole unnecessary plethora of other routines.
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
usingnamespace std;
#define MAX_SIZE 10
struct InventoryRecord
{
string name; // inventory item name
int qty; // how many are in stock
int number; // the item number
float price; //the price of the 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 << "Enter Inventory Records" << endl << endl;
cout << "Name: ";
// Get up to 256 characters from the keyboard including white space.
// Stop reading if encounter the \n first.
cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
tmp.name = str;
cout << "Quantity: ";
cin >> tmp.qty;
cout << "Item Number: ";
cin >> tmp.number;
cout << "Item Price: ";
cin >> tmp.price;
cout << endl;
// see if this record should be added to the array
cout << "Add the record to inventory? (y/n) ";
cin >> response;
if (toupper(response) == 'Y')
list[size++] = tmp;
cin.ignore( 256, '\n' ); // Added line
}
else {
cout << "Inventory full; cannot enter more." << endl;
system("pause");
}
system("cls");
}
void saveFile(const InventoryRecord list[], int size) {
ofstream outfi("store.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 << endl << list[i].name << ';'
<< list[i].qty << ';'
<< list[i].number << ';' << list[i].price;
}
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");
}
}
int main()
{
InventoryRecord list[MAX_SIZE];
int size = 0;
addData( list, size );
addData( list, size );
saveFile( list, size );
}