BinaryFiles and infinite loops

May 1, 2016 at 5:58pm
So, if I just input spaceless data into the 'edit' string it works fine. As soon as I input a sentence for the change description i get an infinite loop. What's actually happening here, I don't get it.....

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
  void createFile(fstream &file, string filename) {
	char code[3], edit[75];
	int iId;
	cout << "Transaction Codes: \n"
		<< "\tAI: Add to Inventory\n"
		<< "\tRI: Reduce from Inventory\n"
		<< "\tCW: Change Wholesale Cost\n"
		<< "\tCR: Change Retail Price\n"
		<< "\tCD: Change Description\n\n"
		<< "Enter the ItemID #: ";
	cin >> iId;
	if (iId < 99999) {
		cout << "ItemID must be 6 digits; Enter the ItemID #: ";
		cin >> iId;
	}
	cout << "Enter the Transaction Code: ";
	cin >> code;
	cout << "Enter the Updated Information: ";
	cin >> edit;
	file.open((filename+".dat").c_str(), ios::out | ios::binary);
	file.write(reinterpret_cast<char *>(&iId), sizeof(iId));
	file.write(code, sizeof(code));
	file.write(edit, sizeof(edit));
	file.close();
}
May 1, 2016 at 6:08pm
What exactly are you inputting?

How can you have an infinite loop, there are no loops in the code you provided.

May 1, 2016 at 6:27pm
Sorry, this is just a function in my program that creates a transaction file in binary format. The user updates data then it is written to a file. Later that file is used to update particular members in array of class objects.
The loop is my program breaking
Last edited on May 1, 2016 at 6:28pm
May 1, 2016 at 6:29pm
here is the whole thing so far.....
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
   #include "InventoryClass.h"
#include "InventoryCostClass.h"
#include<iostream>
#include<fstream>
using namespace std;

const int SIZE = 6;

int menu();
void displayInv(InventoryCost[]);
void createFile(fstream&, string);

int main() {

	InventoryCost itemInv[SIZE] = 
	  { InventoryCost(209875, "Global - Designed Wrench : catalog 145HN - 89", 1056,
		  14.67, 23.99, 10, 13, 2014),
	    InventoryCost(176524, "Steeheel Chainsaw - 120 H - P: catalog 133NM - 65", 2654,
		  234.67, 299.99, 10, 13, 2014),
	    InventoryCost(340965, "Plurers Pliers with Sure - Grip: catalog 764TW - 12", 1043,
		  11.45, 19.98, 10, 13, 2014),
		InventoryCost(453285, "Rapid Recoil Hand Tape Measure : catalog 127UY - 32", 3316, 
		  8.56, 15.99, 10, 13, 2014),
		InventoryCost(893167, "Steady Built Steel Saw Horses : catalog 564HG - 34", 764, 
		  45.54, 79.99, 10, 13, 2014),
		InventoryCost(597895, "Big Boy Bib Overalls - L - XL - XXL: catalog 278LG - 45", 5032, 
		  19.76, 39.99, 10, 13, 2014) };
	int ch = 0;
	fstream file;
	string filename;

	do {
		ch = menu();
		if (ch != 6) {
			switch (ch)
			{
			case 1:
				displayInv(itemInv);
				break;
			case 2:
				{ cout << "Choose a name for the file: ";
				cin >> filename;
				createFile(file, filename);
				};
				break;
			}
		}
	} while (ch != 6);

	file.close();

	system("pause");
	return 0;
}

int menu() {
	int choice;
	cout << "**********************************\n"
		<< "*________MENOCU TOOL SHOP________*\n"
		<< "**********************************\n"
		<< "*  1. Display Inventory          *\n"
		<< "*  2. Create a Transaction File  *\n"
		<< "*  3. Update Inventory           *\n"
		<< "*  4. Display Updated Inventory  *\n"
		<< "*  5. Compute Profit Projection  *\n"
		<< "*  6. EXIT                       *\n"
		<< "**********************************\n"
		<< " Menu Choice: ";
	cin >> choice;
	return choice;
}

void displayInv(InventoryCost itemInv[SIZE]) {
	for (int i = 0; i < SIZE; i++) {
		cout << "Item #" << i+1 << ": " << endl;
		itemInv[i].print();
	}
}

void createFile(fstream &file, string filename) {
	char code[3], edit[75];
	int iId;
	cout << "Transaction Codes: \n"
		<< "\tAI: Add to Inventory\n"
		<< "\tRI: Reduce from Inventory\n"
		<< "\tCW: Change Wholesale Cost\n"
		<< "\tCR: Change Retail Price\n"
		<< "\tCD: Change Description\n\n"
		<< "Enter the ItemID #: ";
	cin >> iId;
	if (iId < 99999) {
		cout << "ItemID must be 6 digits; Enter the ItemID #: ";
		cin >> iId;
	}
	cout << "Enter the Transaction Code: ";
	cin >> code;
	cout << "Enter the Updated Information: ";
	cin >> edit;
	file.open((filename+".dat").c_str(), ios::out | ios::binary);
	file.write(reinterpret_cast<char *>(&iId), sizeof(iId));
	file.write(code, sizeof(code));
	file.write(edit, sizeof(edit));
	file.close();
}
May 1, 2016 at 6:52pm
What loop seems to be causing the problem?

By the way do you know that by default fstream will not create a file, the file must exist or the opening fails.

You should always check to insure that your files open properly every time you try to open any file.


May 1, 2016 at 7:15pm
I know, I know the error checking garbage I put in as an afterthought. The show menu loop is infinite after running option 2 from the menu. It works fine when I put data in the 'edit' that contains no spaces. As soon as I run it and input something like "Big Boy overalls...., catalog 200n-his" it loops the menu. I'm assuming that some leftover cin from the createFile function is causing this.
Also this does make a file for me every time.
May 1, 2016 at 7:18pm
Can't stress enough that this works fine as long as the edit variable gets no spaces. Even typing something like "qwertyuiopasdfghjklzxcvbnm" works, but this "qwertyuiop asdfghjkl zxcvbnm" breaks it.
May 1, 2016 at 8:52pm
Then don't enter any spaces when using the extraction operator since it stops processing strings when it encounters a white space character. If you want your string to contain spaces then you should be using getline() instead.

You should also be using the setw() manipulator to limit the number of characters that the extraction operator to avoid possible buffer overrun errors.

1
2
3
4
5
6
7
8
9
#include <iomanip>
...
    char code[3];
...
    cin >> setw(3) >> code; // This will limit the extraction to two characters,
//remember a C-string requires a terminating character which the extraction
//operator automatically adds to the string. 
...
    


Last edited on May 1, 2016 at 8:53pm
Topic archived. No new replies allowed.