trying to write into the inputFile.

How do i go about passing the char from loadDataFromFile to my int main? I tried doing it like this int main(char fileName[256]), but i am getting this error

Unhandled exception at 0x0F568B51 (msvcr120d.dll) in a02.exe: 0xC0000005: Access violation reading location 0x00000001.


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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

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[]);

int loadDataFromFile(planet planet[])
{
	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);
	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 main()
{
	ifstream inputFile;
	ofstream outputFile;
	planet planet[PSIZE];
	int pN;
	int planetNum = 0;
	char choice;
	int currentSystem = 0;
	int count = 0;
	cout << fixed << setprecision(2) << showpoint;
	currentSystem = loadDataFromFile(planet);
	if (currentSystem == 0)
	{
		cout << " Sorry Couldn't load any data." << endl;
		system("pause");
		return 0;
	}
	cout << "systems read: " << currentSystem << endl;

	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)
			{
				outputFile.open(fileName);
				fstream(fileName, ios::in | ios::out | ios::app);
				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;
				outputFile <<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)
			{

				cout << "Select a System"; /* List certain system #*/
				cout << "Please pick a planet number: " << endl;
				pN = currentSystem;
				cin >> planetNum;
				if (planetNum <= currentSystem)
				{
					cout << "\nSystem number: " << planetNum << endl;
					cout << "Name: " << planet[planetNum].name << endl;
					cout << "Distance from star: " << planet[planetNum].distance << " " << "million miles" << endl;
					planet[planetNum].travelDays = planet[planetNum].distance / pow(4, 3) * 365;
					cout << "\nTravel time in warp 4: " << planet[planetNum].travelDays << "Days";

				}
				else
					cout << "Sorry, that System is not part of the database." << endl;
				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;
			inputFile.close();
			outputFile.close();
			system("pause");
			return 0;
			break;
		}
	} while (choice != 'q');
}
where is the fileName?

// outputFile.open(fileName);
file name comes from

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);

line 28-30
Please post a small sample of your input file. Also please show exactly what you are entering for your input.

Next I recommend you switch to using std::string instead of those C-strings.

Did you try to debug the program. Usually when the program crashes a popup appears that asks you a question and one of the answers is to debug the program. The debugger should be able to tell you exactly where it detected the problem and allow you to view the variables at the time of the crash.

Also note as presented your code should fail to compile because fileName is not defined at line 84.


I am trying to call fileName from int LoadDataFromFile. The error i am getting is

"Unhandled exception at 0x0F3D8B51 (msvcr120d.dll) in a02.exe: 0xC0000005: Access violation reading location 0x00000001."

when using this

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
/* Name:
Assignment:
Date Written: 
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>

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[]);

int loadDataFromFile(planet planet[])
{
	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);
	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 main(char fileName[256])
{
	ifstream inputFile;
	ofstream outputFile;
	planet planet[PSIZE];
	int pN;
	int planetNum = 0;
	char choice;
	int currentSystem = 0;
	int count = 0;
	cout << fixed << setprecision(2) << showpoint;
	currentSystem = loadDataFromFile(planet);
	if (currentSystem == 0)
	{
		cout << " Sorry Couldn't load any data." << endl;
		system("pause");
		return 0;
	}
	cout << "systems read: " << currentSystem << endl;

	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)
			{
				outputFile.open(fileName);
				fstream file(fileName, ios::in | ios::out | ios::app);
				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;
				outputFile << 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)
			{

				cout << "Select a System"; /* List certain system #*/
				cout << "Please pick a planet number: " << endl;
				pN = currentSystem;
				cin >> planetNum;
				if (planetNum <= currentSystem)
				{
					cout << "\nSystem number: " << planetNum << endl;
					cout << "Name: " << planet[planetNum].name << endl;
					cout << "Distance from star: " << planet[planetNum].distance << " " << "million miles" << endl;
					planet[planetNum].travelDays = planet[planetNum].distance / pow(4, 3) * 365;
					cout << "\nTravel time in warp 4: " << planet[planetNum].travelDays << "Days";

				}
				else
					cout << "Sorry, that System is not part of the database." << endl;
				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;
			inputFile.close();
			outputFile.close();
			system("pause");
			return 0;
			break;
		}
	} while (choice != 'q');
}


I can only use c-strings :\
"Create your array of structs in the main function. Do not use any global variables (constant globals are OK). You must create at least two functions in addition to the main function. One of the functions must be to load the data from the file, and the other must be to search the array for the name of the system. You may create more than those two functions, but you must implement at least those two.
Do not use the string library or the vector library. Use the <cstring> library and c-strings (char arrays) to store strings. You may use a reasonable length for your c-strings, such as 256, declared as a global constant (remember not to use literals in your code for things like array size). Check to make sure that the input file is open before reading from it, using ifstream.is_open(). You may terminate the program if the file does not open, or you may ask the user to try a different file name."

here is my file that i am using.

Proxima_Centauri 4.24
Wolf_359 7
Sirius 8.58
Moon_Rocks 4.20
Last edited on
It looks like you need to increase your compiler warning level and fix all warnings. Here is what my compiler says about your program:

main.cpp||In function ‘int loadDataFromFile(planet*)’:|
main.cpp|35|warning: unused variable ‘count’ [-Wunused-variable]|
main.cpp|54|warning: first argument of ‘int main(char*)’ should be ‘int’ [-Wmain]|
main.cpp|54|warning: ‘int main(char*)’ takes only zero or two arguments [-Wmain]|
main.cpp||In function ‘int main(char*)’:|
main.cpp|59|warning: variable ‘pN’ set but not used [-Wunused-but-set-variable]|


Those warnings about int main() on line 54 must be fixed.

The warning level is whatever VB sets it as. I know that it my issue is line 54 with char filename. I am trying to figure out what can I do about calling char fileName from int loadDataFromFile. I am just totally lost with it and attempting to finish it.
Topic archived. No new replies allowed.