Logic Error

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
//*********************************************************************
//  FUNCTION: ReadRecordsFromFile
//  DESCRIPTION: reads records from the file Residents.txt 
//  INPUT:
//      Parameters: none
//      File: RESIDENTS.TXT
//  OUTPUT:	
//      Return Val: boolean
//      Parameters: none
//      File: none
//  CALLS TO: ConverStringToNumber
//**********************************************************************
bool ReadRecordsFromFile (int totalRecords, RecordType ResidentRecord)
{
	string fileName;
	char userChoice;
	bool check = false;
	
	ifstream recordFile (fileName);
	
	cout << "Please enter file name: ";
	cin >> fileName;
	
	totalRecords = 0;

	while (!recordFile)
	{
		cout << "This file you have chosen does not exist." << endl << endl;
	
		// if file does not exist
		while (!check)
		{
			check = true;

			// ask user for another choice
			cout << "Would you like to choose another (Y/N)? ";
			cin >> userChoice;

			userChoice = (toupper (userChoice));
		
			if (userChoice == 'Y')
			{
				recordFile.clear ();
		
				// ask user for file name
				cout << "Please enter file name: ";
				cin >> fileName;
		
				// open new file
				recordFile.open (fileName);
			}
			else if (userChoice == 'N')
			{
				return false;
			}
			else
			{
				check = false;
				cout << "Invalid Entry! Please try again." << endl << endl;
			}
		}
	}

	if (recordFile)
	{
		cout << "file found" << endl;
	}
	
	// loop while file has the records
	while (recordFile.good () && recordFile.peek () != EOF)
	{
		recordFile >> ResidentRecord.socialSecurityNumber;
		recordFile >> ResidentRecord.lastName;
		recordFile >> ResidentRecord.phoneNumber;
		recordFile >> (int&) ResidentRecord.typePhone;

		totalRecords++;	
	
		// increment total records
		RecordNode* pcur = new RecordNode ();
		
		// set next an dprevious pointers
		pcur -> ResidentRecord = ResidentRecord;
		pcur -> Next = NULL;
		pcur -> Prev = RecordsTail;
		
		if (RecordsTail == NULL)
		{
			RecordsHead = RecordsTail = pcur;
		}
		else
		{
			RecordsTail -> Next = pcur;
			RecordsTail = pcur;
		}
	}
	
	//close file
	recordFile.close ();
	cin.clear ();

}// end ReadRecordsFromFile 


I can't figure out what is going wrong right now...

I am trying to enter a file name, if the file name isn't found I want it to display the error saying file cannot be found, then ask if they want to choose another file. If they choose to enter another file, i need it to ask again for the file name....and continue that until they enter a valid file name. If they choose not to, i need it return false so that i can go on to the next step. Currently i am getting multiple error messages...
Last edited on
Read the error messages. The compiler prints them specially for you.
Topic archived. No new replies allowed.