File won't open

I'm making a code that takes a file with a list of heights, first and last names of people and then puts them in order. This code isn't fully complete yet, I was just checking to make sure that the file would open- and it won't. I'm having the user specify the file name, and in this case the file name is "Rolecall", or "Rolecall.txt". 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
 #include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

int main ()
{
	char filename[20];
	double height[10];
	string firstname[10];
	string lastname[10];
	
	cout << "This program takes a list of privates and puts them in order\n";
	cout << "based off of each private's height, first name, and last name.\n";
	cout << "Create a .txt file with the list of each private's information\n";
	cout << "and then enter the name of the file here: ";
	cin >> filename;

	ifstream inputFile;
	
	inputFile.open(filename);
	
	if (inputFile.is_open())
	{
		inputFile >> height[0];
		inputFile >> lastname[1];
		inputFile >> firstname[2];
		inputFile >> height[3];
		inputFile >> lastname[4];
		inputFile >> firstname[5];
		inputFile >> height[6];
		inputFile >> lastname[7];
		inputFile >> firstname[8];
		inputFile >> height[9];
		inputFile >> lastname[10];
		inputFile >> firstname[11];
		inputFile >> height[12];
		inputFile >> lastname[13];
		inputFile >> firstname[14];
		inputFile >> height[15];
		inputFile >> lastname[16];
		inputFile >> firstname[17];
		inputFile >> height[18];
		inputFile >> lastname[19];
		inputFile >> firstname[20];
		inputFile >> height[21];
		inputFile >> lastname[22];
		inputFile >> firstname[23];
		inputFile >> height[24];
		inputFile >> lastname[25];
		inputFile >> firstname[26];
		inputFile >> height[27];
		inputFile >> lastname[28];
		inputFile >> firstname[29];
		
                // testing
		cout << height[0] << endl;
		cout << lastname[1] << endl;
		cout << firstname[2] << endl;
		
		
	}

	inputFile.close();
	
	return 0;
}
You're running off the ends of your arrays big time.

Surely you meant
inputFile >> height[0];
inputFile >> lastname[0];
inputFile >> firstname[0];


Then
inputFile >> height[1];
inputFile >> lastname[1];
inputFile >> firstname[1];


At which point, you'd figure out what a for loop was and do
1
2
3
4
5
6
7
for ( int i = 0 ; i < 10 ; i++ ) {
  inputFile >> height[i];
  inputFile >> lastname[i];
  inputFile >> firstname[i];

}
Hello raisetheroof00,

Looking at the whole program.

1
2
3
4
5
6
include <iostream>
#include <string>
#include <cstring>
#include <fstream>

using namespace std;

The blank line makes it easier to read. You should get use to using them.

You include "cstring", but never use anything from the file. Is there a reason you think you need it?

1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
	char filename[20];
	double height[10];
	string firstname[10];
	string lastname[10];

        cin >> filename;

	ifstream inputFile;
	
	inputFile.open(filename);

From C++11 on you do not need a "char" array to open a file. A "std::string" will do the same.

For line 8 it would help to give a list of file names that can be used, so the user knows what is available and how to spell it. I will see what I can come up with when I see the program run.

Defining the file stream and opening the file can be shortened to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        std::string filename;
	double height[10]{};
	string firstname[10];
	string lastname[10];

        cin >> filename;

	ifstream inputFile(filename);
	
	//inputFile.open(filename);

	if (!inputFile)
	{
		std::cout << "\n    File \"" << filename << "\" did not open\n";

		return 1;
	}

When you open a file stream for input it is a must that you check to make sure it worked.

At this point I have the questions:

Was the file created before the program ran?

Where is it located? Do you need a path to get to the file or is it in the current working directory?

What IDE, operating system are you using. And if you know what C++ standard is the compiler/IDE using? This will let me know what you can use.

Next you have if (inputFile.is_open()). If the file stream opened properly this statement will always be true until you close the stream. Be careful with its use it could work against you.

Inside the if statement, that you no longer need, you have:
1
2
3
4
5
6
7
inputFile >> height[0];
inputFile >> lastname[1];
inputFile >> firstname[2];

inputFile >> height[3];
inputFile >> lastname[4];
inputFile >> firstname[5];

Blank lines here will break it up into pieces and make it easier to understand.

The only line that is correct is line 1. You are using parallel arrays which means the each element of each array has to match the same person. What you have is storing the information in a non parallel manner.

So given your code it should look like this:
1
2
3
4
5
6
7
inputFile >> height[0];
inputFile >> lastname[0];
inputFile >> firstname[0];

inputFile >> height[1];
inputFile >> lastname[1];
inputFile >> firstname[1];

With the last group being [9] since the array elements ar numbered 0 - 9 for a 10 element array.

The section marked testing should be done in a for loop as salem c suggested.

While testing the program I saw this when it ran:

This program takes a list of privates and puts them in order
based off of each private's height, first name, and last name.
Create a .txt file with the list of each private's information
and then enter the name of the file here:


If you are expecting the user to create the file then you should give an example to use when creating the file, so the person creating the file knows how it should look. Otherwise you could have problems reading the file.

This should give you an idea of what your program could be:
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
#include <iostream>
#include <string>

#include <fstream>

using namespace std;

int main()
{
	constexpr size_t MAXSIZE{ 10 };

	size_t index{};
	double height[MAXSIZE];
	std::string filename;
	string firstname[MAXSIZE];
	string lastname[MAXSIZE];

	cout << "This program takes a list of privates and puts them in order\n";
	cout << "based off of each private's height, first name, and last name.\n";
	cout << "Create a .txt file with the list of each private's information\n";
	cout << "and then enter the name of the file here: ";
	cin >> filename;

	ifstream inputFile(filename);

	if (!inputFile)
	{
		std::cout << "\n    File \"" << filename << "\" did not open\n";

		return 1;
	}

	while (index < MAXSIZE && inputFile >> height[index])  // <--- Will read a file of any size, but will also stop at 10 records.
	{
		inputFile >> lastname[index];
		inputFile >> firstname[index++];
	}

	// testing
	for (size_t index = 0; index < MAXSIZE; index++)
	{
		cout << height[index] << '\n';
		cout << lastname[index] << '\n';
		cout << firstname[index] << endl;
	}

	inputFile.close();  // <--- Not needed as the file will close when "main", or other function, looses scope.

	return 0;  // <--- Not required, but makes a good break point.
}


Last point: Since you are using an input file it would help to post that file so everyone can see what you did and use the same information.

Andy
Topic archived. No new replies allowed.