Invalid Return Type

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


Function to add items.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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");
}


And the save to disk file function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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");
	}
}

Any help on this would be great, I can include the entire source if needed but opted to only include the parts I feel are relevant. Thanks in advance.
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
using namespace 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 );
}
You are a genius, thank you sir (or madam)! Now to see what functions actually having a float breaks.
Topic archived. No new replies allowed.