Help reading data from a text file

Hello, I am having trouble displaying data from a text file into my project, it just displays 0 values. I need to read the data, and display it when the user chooses options 1 or 2 in my code.

Thank you for any help!

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

using namespace std;

void mainmenu(string name[], int numberofvotes[], double percentage[], int arraysize);
int readdata(string[], int[], double[], int);
void display(string[], int[], double[], int);


int main()
{
    string name[4] = { "" };
    int numberofvotes[4] = { 0 };
    double percentage[4] = { 0.0 };
    int arraysize = 4;
    
    
    mainmenu(name, numberofvotes, percentage, arraysize);
    
    return 0;
}


void mainmenu(string name[], int numberofvotes[], double percentage[], int arraysize)
{
    
    int choice = int();
    char cont = 'y';
    
    while (cont == 'y' || cont == 'Y')
    {
        system("cls");
        cout << "\t\tMain Menu" << endl;
        cout << "\t\t___________" << endl;
        cout << endl << endl << endl;
        cout << "\t\t1. Read Candidate Data" << endl;
        cout << "\t\t2. Display Results" << endl;
        cout << "\t\t3. Exit Program" << endl;
        
        cout << "\n\n\n\n\n\t\tPlease choose an option: ";
        cin >> choice;
        
        if (cin.fail())
        {
            cout << "Invalid selection" << endl;
            cin.clear();
            cin.ignore(1000, '\n');
        }
        else
        {
            if (choice == 1)
            {
                
                arraysize = readdata(name, numberofvotes, percentage, arraysize);
                
            }
            else if (choice == 2)
            {
                
                display(name, numberofvotes, percentage, arraysize);
                
            }
            else if (choice == 3)
            {
                cout << "Goodbye" << endl;
                exit(1);
            }
            else
            {
                cout << "Selection not valid, please try again." << endl;
            }
        }
        cout << "\nWould you like to continue? Y/N";
        cin >> cont;
    }
}

int readdata(string name[], int numberofvotes[], double percentage[], int arraysize)
{
    ifstream in;
    in.open("h:\\results.rtf"); //working on a mac(tried txt on pc, same prob

    char char1 = char();
    int i = 0;
    
    system("cls");
    cout << endl;
    
    cout << fixed << setprecision(2);
    
    while (!in.eof())
    {
        in >> name[i] >> numberofvotes[i] >> percentage[i];
        cout << setw(10) << left << name[i] << setw(15) << left << numberofvotes[i] << setw(15) << left << percentage[i];
        cout << endl;
        ++i;
    }
    arraysize = 4;
    return arraysize;
    
}

void display(string name[], int numberofvotes[], double percentage[], int arraysize)
{
    system("cls");
    for (int i = 0; i < arraysize; i++)
    {
        cout << "Name: " << name[i] << endl;
        cout << "Number of Votes: " << numberofvotes[i] << endl;
        cout << "Percentage of Votes: " << percentage[i] << endl;
        cout << "==============================" << endl;
    }
}

Please post a sample of your input file.

You really should be checking to see if your file opened correctly, a if it doesn't you'll probably want to stop the program. By the way eof() is not the best way to control your file reading loop. This can quite often lead to an "repeat" input that is not wanted.
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800


that is my input file. it's just supposed to display those (number of votes)
If your file has only two columns how can you try to read 3 ?
in >> name[i] >> numberofvotes[i] >> percentage[i];

1
2
3
4
5
6
7
while (in && i < 4)
  {
    in >> name[i] >> numberofvotes[i];
    cout << setw (10) << left << name[i] << setw (15) << left << numberofvotes[i] << setw (15) << left << percentage[i];
    cout << endl;
    ++i;
  }


Why do you declare your array size as 4 when your file has 5 rows ?
I deleted the percentage column but it's still displaying 0s :(

I thought array sizes began counting at 0, so it would be 0, 1, 2, 3, 4?
I thought array sizes began counting at 0, so it would be 0, 1, 2, 3, 4?

There is no array size "counting."

Array indexes begin at 0, so an array with size 5 would have valid indexes 0, 1, 2, 3, 4, but the size, which also happens to be the number of valid indexes, would be 5.
I deleted the percentage column but it's still displaying 0s :(

Of course it does. That's the value it contains - you initialize it in main.
cout << setw(10) << left << name[i] << setw(15) << left << numberofvotes[i] << setw(15) << left << percentage[i];
Topic archived. No new replies allowed.