searching for text in file bool.

hello guys, I am trying to figure out how to get back into the swing of things, but my issue at hand is that I am trying to get my bool to work can i get any help? here are my errors. I can only use the cstring library.

1 IntelliSense: expression must be a modifiable lvalue 172 3
2 IntelliSense: expression must have class type 177 8
3 IntelliSense: identifier "line" is undefined 177 21
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Name: 
Assignment: 
Date Written: Oct 6 , 2015
Course: 
Program:a02.cpp
Purpose: to code a program to calculate warp speed
Sources:
http://www.cplusplus.com/forum/beginner/176640/

*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstring>


const int S_SIZE = 256;
const int PSIZE = 20;

using namespace std;


struct planet {
	int currentSystem;
	char name[S_SIZE];
	double distance; /*planet struct*/
	double travelDays;
};



int loadDataFromFile(planet planet[], char fName[])
{
	int currentSystem = 0;
	char fileName[256];
	ifstream inputFile;
	int count = 0;

	cout << "Please inculde the file extension also ie:(.txt)" << endl;
	cout << "What is the file name you would like to open?: ";
	cin.get(fileName, S_SIZE);
	strcpy(fName, fileName);
	inputFile.open(fileName);
	if (!inputFile.is_open()) //Failed to open OH NOES!
		return currentSystem; // end of the function
	while (!inputFile.eof())
	{
		inputFile.get(planet[currentSystem].name, S_SIZE, '\t'); // Read the system name
		inputFile >> planet[currentSystem].distance;
		inputFile.ignore();
		currentSystem++;
	}
	inputFile.close();
	return currentSystem;
}

int LoadDataFromFile(planet planet[]);



int main()
{
	ofstream outputFile;
	planet planet[PSIZE];
	char fileName[256];
	char searchChoice[256];
	int searchWord[256];
	int planetNum = 0;
	char choice;
	int currentSystem = 0;
	int searchSystem = 0;
	int count = 0;
	cout << fixed << setprecision(2) << showpoint;
	currentSystem = loadDataFromFile(planet, fileName);
	if (currentSystem == 0)
	{
		cout << " Sorry Couldn't load any data." << endl;
		system("pause");
		return 0;
	}
	cout << "systems read: " << currentSystem << endl;
	fstream file(fileName, fstream::app);
	do {
		cout << "\n**********************************" << endl;
		cout << "Welcome to Chad's system map" << endl;
		cout << "Here are your following options." << endl;
		cout << "a. Add a system" << endl;
		cout << "s. select a system" << endl;				 /*Main Menu*/
		cout << "l. list all system" << endl;
		cout << "q. To quit the system." << endl;
		cout << "Please use all lower case during the program" << endl;
		cout << "**********************************" << endl;
		cout << "please enter your choice: ";
		cin >> choice;
		cin.ignore(); // Added ignore, to get rid of '\n' stuck in the buffer.

		switch (choice) {
		case 'a':
		case 'A':
			if (currentSystem < PSIZE)
			{

				cout << "Add a system to the database." << endl;  /*Adding a planet*/
				cout << "\n Please enter the name of the system: ";
				cin.get(planet[currentSystem].name, S_SIZE);
				cout << "\n Please enter the planets distance from its star in millions of miles: ";
				cin >> planet[currentSystem].distance;
				file << '\n' << planet[currentSystem].name << '\t' << planet[currentSystem].distance;
				cin.ignore();
				currentSystem++;
				break;
			}
			else
				cout << "Sorry, no more room left in the system.";
			break;
		case 's':
		case 'S':
			if (currentSystem > 0)
			{

				searchForASystem(fileName, planet);
				break;
			}
			else
				cout << "sorry no Systems have been added in the database yet" << endl;
			break;
		case 'l':
		case 'L':
			if (currentSystem > 0) {
				cout << "List the systems." << endl;
				for (int i = 0; i < currentSystem; i++)    /*list all of the systems in the database*/
				{
					cout << "\nSystem number: " << i << endl;
					cout << "Name: " << planet[i].name << endl;
					cout << "Distance from star: " << planet[i].distance << " " << "million miles" << endl;
					planet[i].travelDays = planet[i].distance / pow(4, 3) * 365;
					cout << "\nTravel time in warp 4: " << planet[i].travelDays << "Days";
					count++;
				}
			}
			else
				cout << "No systems have been added to the database" << endl;
			break;
		case 'q':
		case 'Q':
			cout << "Thank you for using Chad's system database." << endl;
			cout << "Enjoy the rest of your day." << endl;
			file.close();
			system("pause");
			return 0;
			break;
		}
	} while (choice != 'q');
}

bool searchForASystem(char* filename, planet planet[])
{
	int countwords = 0;
	char path[S_SIZE];
	char system[S_SIZE];
	char word[S_SIZE];
	
	cout << "Please inculde the file extension also ie:(.txt)" << endl;
	cout << "What is the file name you would like to open?: ";
	cin.get(path, S_SIZE);
	ifstream File(path);
	if (File.is_open())
	{
		cout << "File found." << endl;
		system = File.get();
		cout << "Write the word you're searching for." << endl;
		cin.get(word, 256);
		while (!File.eof())
		{
			if (word.compare(line) == 0)
				countwords++;
		}
		cout << "The word has been found " << countwords << " times." << endl;
		File.close();
	}
	else
	{
		cout << "Error! File not found!";

}
Last edited on
Please compile your code and then post the actual compile errors. Intellinonsense errors generally should not be relied upon, trust only compiler/linker errors.

Error 1 error C3861: 'searchForASystem': identifier not found 123 1 lab2
Error 2 error C2440: '=' : cannot convert from 'int' to 'char [256]' 172 1 lab2
Error 3 error C2228: left of '.compare' must have class/struct/union 177 1 lab2
Error 4 error C2065: 'line' : undeclared identifier 177 1 lab2
Error 5 error C1075: end of file found before the left brace '{' at 'lab2\a02.cpp(159)' was matched lab2\a02.cpp 188 1 lab2
6 IntelliSense: expression must be a modifiable lvalue 172 3 lab2
7 IntelliSense: expression must have class type 177 8 lab2
8 IntelliSense: identifier "line" is undefined 177 21 lab2

here is the data for the txt file.

Proxima_Centauri 4.24
Wolf_359 7
Sirius 8.58
test 3.4
Last edited on
For the first error, where is the function searchForASystem() implemented, before or after line 123? It looks like you need to add a function prototype for this function somewhere before it is used.

For the next error the variable system is an array of char not a single int/char. What exactly are you trying to assign to this variable?

For the next error a C-string doesn't have a compare function. Perhaps you should be using a std::string instead of the C-string.

For the next error where was the variable "line" declared?

Lastly you seem to have problems with your braces. Does every beginning brace '{' have a closing brace '}'?

i ended up changing it up, so this is what i have now.
EDIT: issue i am having now is not having it print on screen the system that i am searching for
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Name
Assignment:
Date Written: 
Course: 
Program:a02.cpp
Purpose: to code a program to calculate warp speed
Sources: 
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstring>


const int S_SIZE = 256;
const int PSIZE = 20;

using namespace std;


struct planet {
	int currentSystem;
	char name[S_SIZE];
	double distance; /*planet struct*/
	double travelDays;
};



int loadDataFromFile(planet planet[], char fName[])
{
	
	char fileName[256];
	ifstream inputFile;
	int count = 0;
	int currentSystem = 0;

	cout << "Please inculde the file extension also ie:(.txt)" << endl;
	cout << "What is the file name you would like to open?: ";
	cin.get(fileName, S_SIZE);
	strcpy(fName, fileName);
	inputFile.open(fileName);
	if (!inputFile.is_open()) //Failed to open OH NOES!
		return currentSystem; // end of the function
	while (!inputFile.eof())
	{
		inputFile.get(planet[currentSystem].name, S_SIZE, '\t'); // Read the system name
		inputFile >> planet[currentSystem].distance;
		inputFile.ignore();
		currentSystem++;
	}
	inputFile.close();
	return currentSystem;
}

int LoadDataFromFile(planet planet[]);

bool searchForASystem(char* filename, planet planet[])
{
	char path[S_SIZE];
	char systemName[S_SIZE];
	int currentSystem = 0;
	cout << "test";
	cout << "Please inculde the file extension also ie:(.txt)" << endl;
	cout << "What is the file name you would like to open?: ";
	cin.get(path, S_SIZE);
        cin.ignore();
	ifstream file(path);
	if (file.is_open())
	{
		cout << "File has been opened!";
		cout << "Please enter the system you would like to find: ";
		cin.get(systemName, S_SIZE);
		cin.ignore();
		if (systemName == planet[currentSystem].name)
		{
			cout << "\nSystem number: " << currentSystem << endl;
			cout << "Name: " << planet[currentSystem].name << endl;
			cout << "Distance from star: " << planet[currentSystem].distance << " " << "million miles" << endl;
			planet[currentSystem].travelDays = planet[currentSystem].distance / pow(4, 3) * 365;
			cout << "\nTravel time in warp 4: " << planet[currentSystem].travelDays << "Days";
		}
	}
	else
	{
		cout << "Error: File could not be found.";
	}
	return 0;
}

int main()
{
	ofstream outputFile;
	planet planet[PSIZE];
	char fileName[256];
	int planetNum = 0;
	char choice;
	int currentSystem = 0;
	int searchSystem = 0;
	int count = 0;
	cout << fixed << setprecision(2) << showpoint;
	currentSystem = loadDataFromFile(planet, fileName);
	if (currentSystem == 0)
	{
		cout << " Sorry Couldn't load any data." << endl;
		system("pause");
		return 0;
	}
	cout << "systems read: " << currentSystem << endl;
	fstream file(fileName, fstream::app);
	do {
		cout << "\n**********************************" << endl;
		cout << "Welcome to Chad's system map" << endl;
		cout << "Here are your following options." << endl;
		cout << "a. Add a system" << endl;
		cout << "s. select a system" << endl;				 /*Main Menu*/
		cout << "l. list all system" << endl;
		cout << "q. To quit the system." << endl;
		cout << "Please use all lower case during the program" << endl;
		cout << "**********************************" << endl;
		cout << "please enter your choice: ";
		cin >> choice;
		cin.ignore(); // Added ignore, to get rid of '\n' stuck in the buffer.

		switch (choice) {
		case 'a':
		case 'A':
			if (currentSystem < PSIZE)
			{

				cout << "Add a system to the database." << endl;  /*Adding a planet*/
				cout << "\n Please enter the name of the system: ";
				cin.get(planet[currentSystem].name, S_SIZE);
				cout << "\n Please enter the planets distance from its star in millions of miles: ";
				cin >> planet[currentSystem].distance;
				file << '\n' << planet[currentSystem].name << '\t' << planet[currentSystem].distance;
				cin.ignore();
				currentSystem++;
				break;
			}
			else
				cout << "Sorry, no more room left in the system.";
			break;
		case 's':
		case 'S':
			if (currentSystem > 0)
			{

				searchForASystem(fileName, planet);
				break;
			}
			else
				cout << "sorry no Systems have been added in the database yet" << endl;
			break;
		case 'l':
		case 'L':
			if (currentSystem > 0) {
				cout << "List the systems." << endl;
				for (int i = 0; i < currentSystem; i++)    /*list all of the systems in the database*/
				{
					cout << "\nSystem number: " << i << endl;
					cout << "Name: " << planet[i].name << endl;
					cout << "Distance from star: " << planet[i].distance << " " << "million miles" << endl;
					planet[i].travelDays = planet[i].distance / pow(4, 3) * 365;
					cout << "\nTravel time in warp 4: " << planet[i].travelDays << "Days";
					count++;
				}
			}
			else
				cout << "No systems have been added to the database" << endl;
			break;
		case 'q':
		case 'Q':
			cout << "Thank you for using Chad's system database." << endl;
			cout << "Enjoy the rest of your day." << endl;
			file.close();
			system("pause");
			return 0;
			break;
		}
	} while (choice != 'q');
}
Last edited on
Topic archived. No new replies allowed.