Reading from file and output is a large blank space

This code reads 26 names from a file. When the user enters 1 as their menu choice, the output is a large blank space, almost as though the names are there, but they just don't appear.

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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    string reader;
    int counter=0;
    int menuChoice;

    ifstream file;
    file.open("Names.txt");

    while (getline(file, reader))
    {
        counter++;
    }

    string names [counter];

    for (int j=0;j<counter;j++)
    {
        file >> names[j];
    }

    while (menuChoice != 7)
    {
        cout << "1. Show unsorted list" << endl;
        cout << "2. Sort list" << endl;
        cout << "3. Show sorted list" << endl;
        cout << "4. Add name" << endl;
        cout << "5. Delete name" << endl;
        cout << "6. Save to a new file" << endl;
        cout << "7. Exit" << endl << endl;
        cout << "Please enter the number of the menu option you would like to proceed with: ";
        cin >> menuChoice;
        cout << endl;

        switch (menuChoice)
        {
            case 1:
                for (int j=0;j<counter;j++)
                {
                    cout << names[j] << endl;
                }
                cout << endl;
                break;
        }
    }
    
    return 0;
}
closed account (E0p9LyTq)
Please do not make multiple posts.

http://www.cplusplus.com/forum/beginner/234673/

Check one of your posts so any people wanting to help you do it in one post only, please.
Sorry I didn't intend to make multiple posts. The first couple of times I made the post it did not go through, so I kept trying until it did. But it looks like the times it said it did not go through, it had. Again, I'm sorry.
After your while loop the stream is at its end so the for loop won't read anything which leaves your array with empty strings.
Inside your switch statement you print only empty lines.
Hello Shezshade,

I see you have included the header file "vector". You should make use of it.

Line 22 is not allowed in C++ or even C. When defining an array either "counter" needs to be defined as a constant or you hae to use a constant number like 50 or 100 to define the size of the array.

When the program is compiled the size of the array needs to be known so the compiler can set aside the correct amount of space on the stack for the array. If you need to use a variable then you would have to create a dynamic array at run time.

You would save yourself some work by moving line 22 to the top and define it as a vector. This way you could combine the while loop and for loop into one. Line 19 could be replaced with names.emplace_back(reader);. Eliminate lines 24 - 27 and change "counter" in line 45 to "names.size()" and that should work. Granted this is untested.

Hope that helps,

Andy
Line 22 is not allowed in C++ or even C

Actually VLA are allowed in ISO C99.
https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
@Thomas1965,

Thank you. Point noted. Somewhere I got the impression that VLAs are not allowed. Either I misunderstood or got the wrong idea. I will watch that in the future.

Andy
Topic archived. No new replies allowed.