Program will not retrieve file

I'm working on a hotel management program, I'm very new to this and i'm ready to throw this comp across the room. It keeps telling me

Dead.cpp: In function 'int main()':
Dead.cpp:34:20: error: 'hotelList' was not declared in this scope
ifstream theFile (hotelList);

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	
// Constants for menu choices
const int Fill_Room = 1;
const int Customer_leave = 2;
const int Room_map = 3;
const int Search_customer = 4;
const int Exit = 5;

int choice, Floor, Room, Vacancy, Name;
string name;
bool userChoice = true;
while (userChoice != false){
	cout <<" 1) Fill room\n";
	cout <<" 2) Customer leave\n";
	cout <<" 3) Room map\n";
	cout <<" 4) Search customer\n";
	cout <<" 5) Exit\n";
	cout <<" Enter user choice: \n";

		cin >> choice;

switch (choice)
{

case Fill_Room:

	ifstream theFile (hotelList);
	
		string Floor;
		int Room;
		string Vacancy;
		string Name;
		
		while (theFile>> Floor >> Room >> Vacancy >> Name){
		
                cout << Floor << "Room:" << Room << Vacancy << Name << endl;
		}
	
		break;}
}
return 0;}


- I'm not looking for someone to write the code, but if there is anyone online and willing to help walk me through this via facebook messenger or phone I would be eternally grateful. I have a lot of things to do for my last final in physics and would like to get some studying done for that.
ifstream theFile (hotelList);
You're trying to use a variable named hotelList.

To use variables, they have to exist. They need to be created.

See this line:
string Floor;
This line creates a variable named Floor. After the variable has been created, you can use it. The variable hotelList was never created. You have to create variables before you can use them. C++ provides some variables for you (like cout) but you're going to have to create hotelList before you try to use it.

Or is hotelList meant to be the name of a file? What's the file's name?


Last edited on
I figured out the issue, I needed to use parentheses. However, Now I need to be able to over write that text file and have been looking every where and can not find anything helpful to get me there...I've seen a lot about arrays, but I dont believe that will work...How do I specify which line will be edited??
Basically I needed to be able to ready out my .txt file; from the firt menu option I need to be able to add someone and edit the txt file with out losing the rest of the data.
2) I needed to be able to check someone out.
3) show the occupancy as a grid
4) search for customer by name.
5) exit program..

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
/ #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	
// Constants for menu choices
const int Fill_Room = 1;
const int Customer_leave = 2;
const int Room_map = 3;
const int Search_customer = 4;
const int Exit = 5;
// global
int choice, Floor, Room, Vacancy;
string name;
//Menu, while loop
bool userChoice = true;
while (userChoice != false){
	cout <<" 1) Fill room\n";
	cout <<" 2) Customer leave\n";
	cout <<" 3) Room map\n";
	cout <<" 4) Search customer\n";
	cout <<" 5) Exit\n";
	cout <<" Enter user choice: \n";
		//input choice
		cin >> choice;

switch (choice)
{
//edit .txtfile hotel list
case Fill_Room:

ifstream inputFile;
		//declaring variables to output data from txt file
		string Floor;
		int Room;
		string Vacancy;
		string Name;
		//open file to read
	inputFile.open ("hotelList.txt");
	if (inputFile)
	{
		//output data to display occupancy
		while (inputFile>> Floor >> Room >> Vacancy >> Name){
			cout << Floor << "\t" << "Room:" << "\t" << Room << "\t" << "Vanancy: " << Vacancy << "\t" << "Name: " <<Name << endl;
		}
		//close the .txt file
	inputFile.close();
	}else{
		cout << "Error opening the file.\n";
	}
	
		break;
}

switch (choice)
case Room_map:
ifstream inputFile;
		//declaring variables to output data from txt file
		string Floor;
		int Room;
		string Vacancy;
		string Name;
		//open file to read
	inputFile.open ("hotelList.txt");
	if (inputFile)
	{
		//output data to display occupancy
		while (inputFile>> Floor >> Room >> Vacancy >> Name){
			cout << Floor << "\t" << "Room:" << "\t" << Room << "\t" << "Vanancy: " << Vacancy << "\t" << "Name: " <<Name << endl;
		}
		//close the .txt file
	inputFile.close();
	//check for opening file errors
	}else{
		cout << "Error opening the file.\n";
	}
	
break;
}
return 0;

return 0;
}
Last edited on
I need to be able to add someone and edit the txt file with out losing the rest of the data.


Put simply, not going to happen. You have to rewrite the entire file with the new data in.

Put more complicated, you can write over pieces of an existing file but you have to make everything you write over it fit exactly, which seems unlikely. You can also append to the end of an existing file.
Ok I have revised my program and using a void function. I'm getting an error and I'm not sure what it means exactly I've posted the code below any help would be appreciated.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void printRow(int line, string hotelList[21][4])
{
cout << hotelList[line-1][0]<< " " <<hotelList[line-1][1]<< " " << hotelList[line-1][2] <<" "<< hotelList[line-1][3] << endl;
}
void fillRoom(string hotelList[21][4], string Floor, string Room, string Name){
	int a=0;
	int b=0;
	while (a==0){
			if(Floor==hotelList[b][0]){
			//close if loop
				if(Room==hotelList[b][1]){
					a++;}//second if
			else b++;}//first clsoe if
			else b++;}//while close
cout << b;
}//void	
int main()
{
	
// Constants for menu choices
const int fill_Room = 1;// displays lay out of floors and rooms
const int Customer_leave = 2;//customer check out
const int room_map = 3;// add a customer to the roster
const int Search_customer = 4;// look for a speicific customer
const int Exit = 5;//exit the program

	string Floor;
	string Room;
	string Name;
	char Vacancy;
	string hotelList[21][4];
	ifstream inputFile;
	inputFile.open ("hotelList.txt");
	for (int a=0; a<21;a++)
	{
		for (int b=0; b<4; b++)
			inputFile  >> hotelList[a][b];
		
		}
		inputFile.close();
		
//Menu
	cout <<" 1. Fill Room\n";
	cout <<" 2. Customer leave\n";
	cout <<" 3. Room map\n";
	cout <<" 4. Search customer\n";
	cout <<" 5. Exit\n";
	
		int choice;
	cout <<" Enter user choice 1-5 : \n";
	cin >> choice;
	
switch (choice)
{
	case 1:
	
	cout << "What floor?  ";
	cin >> Floor;
	cout << "What room?  ";
	cin >> Room;
	cout << "Enter guest's name: ";
	cin >> Name;
	fillRoom(hotelList[21][4],Floor,Room,Name);
}
return 0;
}


With this error flagged. I'm not sure exactly what the problem is any help would be appreciated.

tryAgain.cpp:67:43: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'std::__cxx11::string (*)[4] {aka std::__cxx11::basic_string<char> (*)[4]}' for argument '1' to 'void fillRoom(std::__cxx11::string (*)[4], std::__cxx11::string, std::__cxx11::string, std::__cxx11::string)'
fillRoom(hotelList[21][4],Floor,Room,Name);
Line 67: You're not calling fillRoom correctly. You're try to pass a specific element (which happens to be out of bounds) of hotelList as an argument. Simply call it passing only the name of the array.
 
fillRoom (hotelList,Floor,Room,Name);

Topic archived. No new replies allowed.