Errors with my project..infinite loop.

I was given this as my final project for my C++ Logic course.

1
2
3
My assignment is in the .doc file contained in this link:  
http://www.richardgnall.com/mcc/C++ Intro/Assessment/
Screenshots are provided in the .doc file and it is meant to run like the .exe file contained in the link as well.


Here's what i have and I can't test anything else because I am being hit with an infinite loop after creating a file if it doesn't exist and for a reference location for the array..

Help is appreciated! Thanks!!


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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// SuperDuperBank.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct Customer
{
	string custName;
	double custTran;
};

void Clearscreen()
{
	cout << "\n" << "Press any key to continue...";
	system("pause");
	system("CLS");
}

string getFileName()
{
	string fName;
	cout << "Name of data file: ";
	cin >> fName;
	return fName;
}


int main()
{
	const int ARRAYSIZE = 10;
	Customer cust[ARRAYSIZE];
	int choice;
	string file;
	ifstream myFile;
	ofstream newFile;
	string name;
	double tran;
	bool flag = true;
	int length = 0;

	while (flag == true)
	{
		cout << "** MENU **" << "\n" << endl;
		cout << "Current Data File: " << "\n" << endl;
		cout << "(1) Select / create data file (.txt file extension will be added automatically)" << endl;
		cout << "(2) Display all records" << endl;
		cout << "(3) Display all records and totals for a specific customer" << endl;
		cout << "(4) Add record to the data file  " << endl;
		cout << "(5) Display the reocrd with the largest purchase" << endl;
		cout << "(6) Display all records sorted alphabetically" << endl;
		cout << "\n" << "Make choice: ";
		cin >> choice;
		
		if (choice == 1)
		{	
			flag = false;
			break;
		}
		else
		{
			cout << "\n" << "Please select a data file" << endl;
			Clearscreen();
		}
	}

	while (choice != 999) 
	{

		switch(choice)
		{
		case 1:
			file = getFileName();
			myFile.open(file + ".txt");
			if(myFile.good())
			{
				cout << "Valid file name" << endl;
			}
			else
			{
				cout << "File not found... Creating new file" << endl;
				//newFile.open(file + ".txt");
			}
			Clearscreen();
			break;
		case 2:
			int i = 0;
			while(myFile >> name >> tran)
			{
				cust[i].custName = name;
				cust[i].custTran = tran;
				i++;
				length++;
			}
		
			for(int j = 0; j < length; j++)
			{
				cout << setw(ARRAYSIZE) << cust[j].custName << setw(ARRAYSIZE) <<
					cust[j].custTran << endl;
			}
			Clearscreen();
			break;
		}

		cout << "** MENU **" << "\n" << endl;
		cout << "Current Data File: " + file << "\n" << endl;
		cout << "(1) Select / create data file (.txt file extension will be added automatically)" << endl;
		cout << "(2) Display all records" << endl;
		cout << "(3) Display all records and totals for a specific customer" << endl;
		cout << "(4) Add record to the data file  " << endl;
		cout << "(5) Display the reocrd with the largest purchase" << endl;
		cout << "(6) Display all records sorted alphabetically" << endl;
		cout << "\n" << "Make choice: ";
		cin >> choice;
	}
	myFile.close();
	return 0;
}

Last edited on
I'm not getting any infinite loop when I run it. I did however pass a c-string into the fstream.open() calls. Maybe that's a problem, I couldn't even get it to compile before I did that.

1
2
3
4
5
6
7
8
9
10
11
file = getFileName() + ".txt";
myFile.open(file.c_str());
if(myFile.good())
{
   cout << "Valid file name" << endl;
}
else
{
   cout << "File not found... Creating new file" << endl;
   newFile.open(file.c_str());
}


Also should probably add this to the end, instead of myFile.close();
1
2
3
4
if (myFile.is_open())
   myFile.close();
if (newFile.is_open())
   newFile.close();
Last edited on
@Ashishduh: I could access the records.txt before i added the c_str, but after I loaded it in and hit 2, that's when the infinite loop occurred.

After adding c_str, now it says that it cant locate the file, which instantly sends me into the else statement..

Thanks for your quick reply
Last edited on
Ah, so you actually loaded a file. I can't help you there because I don't know what's in the file. Place some breakpoints at the beginning of the while loop in case 2: and step through it. See what kind of data you're getting and if something's wrong there.

I think the proper way to parse a file isn't while(myFile >> name >> tran) but rather

1
2
while(!myFile.eof())
myFile >> name >> tran
Last edited on
@Ashishduh: Ah sorry for the confusion.. everything related to the project was in the link ..
The text file looks like this:
http://richardgnall.com/mcc/C++%20Intro/Assessment/records.txt
Ok, didn't see that.

Hmm, well I just tried it with that file with both methods of parsing the file (with myFile.eof() and without) and they both worked, giving no infinite loop.

Just step through that loop with breakpoints and look at what data you're getting.
@Ashishduh: Are you able to see the data and not a reference to an address?
Yes it prints to the console exactly as it is in the txt file.
I am still having trouble finding the file.. The eof() works but the c_str() causes issues that i cant get around. Instead, it prints off an address, but the infinite loop is gone now.

Sorry for the trouble, but do you mind posting the way you have it coded?

Thanks x
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
	while (choice != 999) 
	{

		switch(choice)
		{
		case 1:
			file = getFileName() + ".txt";
			myFile.open(file.c_str());
			if(myFile.good())
			{
				cout << "Valid file name" << endl;
			}
			else
			{
				cout << "File not found... Creating new file" << endl;
				newFile.open(file.c_str());
			}
			Clearscreen();
			break;
		case 2:
			int i = 0;
			while(myFile >> name >> tran)
			{
				cust[i].custName = name;
				cust[i].custTran = tran;
				i++;
				length++;
			}
		
			for(int j = 0; j < length; j++)
			{
				cout << setw(ARRAYSIZE) << cust[j].custName << setw(ARRAYSIZE) <<
					cust[j].custTran << endl;
			}
			Clearscreen();
			break;
		}


Can you post what output you're getting? Also, your compiler may be different than mine and will accept a string being passed to fstream.open().
Last edited on
@Ashishduh: This is a screenshot of what I'm getting:

https://docs.google.com/open?id=0B-O_fWsadifDMDcwYTk5NTgtNzE1ZC00NTQyLThjN2MtMDg5MjA5YjhkNjQ0


And this is the code:
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
while (choice != 999) 
{

switch(choice)
{
case 1:
file = getFileName() + ".txt";
myFile.open(file.c_str());
if(myFile.good())
{
cout << "Valid file name" << endl;
}
else
{
cout << "File not found... Creating new file" << endl;
//newFile.open(file.c_str());
}
Clearscreen();
break;
case 2:
int i = 0;
while(!myFile.eof())
{
myFile >> name >> tran;
cust[i].custName = name;
cust[i].custTran = tran;
i++;
length++;
}
for(int j = 0; j < length; j++)
{
cout << setw(ARRAYSIZE) << cust[j].custName << setw(ARRAYSIZE) <<
cust[j].custTran << endl;
}
Clearscreen();
break;
}
Last edited on
Yeah that's weird...

Did you try debugging through it?
I don't understand what's happening..And I did, but its not saying what values are being placed into those variables.
Nothing is being put into them. I just debugged mine and the values from your screenshot ("" and -9.255e61) are the values in the array before you assign them anything. So your code either isn't finding the file (you sure it says "Valid file name"?) or the file is empty.

Put a breakpoint at line 94: cust[i].custName = name; and see if it ever gets hit, disable all other breakpoints. If it does get it but you still get bad output, then there's something wrong with the file. Maybe at some point earlier your program accidentally created a new file records.txt(due to some bug) and overwrote the original one?
Last edited on
@Ashishduh: I just did that and it does get hit, but the value still isn't being assigned..:/
Last edited on
You checked the records.txt file? Also, what are the values of name and tran when the breakpoint gets hit?
@Ashishduh: The values don't show at all .. any idea? :o
Last edited on
What do you mean that the values don't show at all? There is always a value associated with those variables as long as they're in scope, which they should be. When you hover over the variable names or when you check the "Locals" tab in visual studio, you don't see anything for values of "name" and "tran" (when the breakpoint is hit)?

If you really don't see a value then I have no idea, I thought the only time you couldn't see the value would be if they're out of scope. In that case maybe you have some stray/missing brackets somewhere? But that would surely throw a compiler error...no idea.
Last edited on
Topic archived. No new replies allowed.