problems reading file into parallel arrays

I'm supposed to read a txt file into parallel arrays. The file is like this:
Name
number
Name
number
etc.
What I have compiles but doesn't output what it should. I'm not sure exactly what tbe problems are. I am confused about array syntax, including how to get the functions to pass the revised array back to main(). Here's the beginning of the program:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void welcome(ifstream&);
int firstArray(ifstream&, string[]);
void secondArray(ifstream&, int[]);

int main()
{
    string candidatesArray[9];
    for (int i=0; i<9; i++)
        candidatesArray[i]="0";
    int votesArray[9];
    for (int i=0; i<9; i++)
        votesArray[i]=0;
    ifstream inputStream;
    int dataCount;

    welcome(inputStream);
    dataCount = firstArray(inputStream, candidatesArray);  
    secondArray(inputStream, votesArray);

    cout << dataCount;
    cout << candidatesArray[5];

    inputStream.close();
    return 0;
}

void welcome(ifstream& input)
{
    string fileName;

    cout << "This program will display vote statistics for election candidates." << endl;
    cout << "Please enter the pathway of the input file." << endl;
    getline(cin, fileName);

    input.open(fileName.c_str());
    while (input.fail())
    {
        cout << "File failed to open" << endl;
        cout << "Please re-enter the pathway" << endl;
    }

    cout << "File opened" << endl;
}

int firstArray(ifstream& input, string strArr[])        
{
    string name;
    int i = 0;
    while (i<9 && !input.eof())
    {
        getline(input, name);
        strArr[i] = name;
        i++;
        input.ignore();
    }

    return i;
}

void secondArray(ifstream& input, int intArr[])
{
    int i = 0;

    while (i<9 && !input.eof())
    {
        input.ignore();
        input >> intArr[i];
        i++;
    }
}



Here's the output:

This program will display vote statistics for election candidates.
Please enter the pathway of the input file.
C:[pathway]
File opened
9
921


The last 2 outputs aren't correct: the txt file only has 5 items (ie 5 names & 5 numbers), so the returned count i should have been 5, and of course the name array shouldn't be outputting a number...

Edited to add: having main() output from candidatesArray gives different numbers, outputting from votesArray gives 0's. Evidently there is a problem with passing the arrays...
Last edited on
Hi The Phantom,

Post your entire code and we'll be able to help you further. Thanks!
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void welcome(ifstream&);
int firstArray(ifstream&, string[]);
void secondArray(ifstream&, int[]);

int main()
{
    ifstream inputStream;
	string candidatesArray[9];          //i suggest putting your variable declarations at the top of main
    int votesArray[9];                  //it's esier to read this of cours is a matter of preferance
	int dataCount;                                   
	
										
	for (int i=0; i<9; i++)
        candidatesArray[i]="0";   //I would put these for loops in their own function
                                  //or remove them alltogether i'm not sure they are needed
    for (int i=0; i<9; i++)
        votesArray[i]=0;
    
    

    welcome(inputStream);
    dataCount = firstArray(inputStream, candidatesArray);  
    secondArray(inputStream, votesArray);

    cout << dataCount;
    cout << candidatesArray[5];

    inputStream.close();
    return 0;
}

void welcome(ifstream& input)
{
    string fileName;

    cout << "This program will display vote statistics for election candidates." << endl;
    cout << "Please enter the pathway of the input file." << endl;
    getline(cin, fileName);

    input.open(fileName.c_str());
    while (input.fail())            //<< ---------------------------need to make an if statement or put somthing to 
    {                                                           //change the fail condition this is an infinite loop
        cout << "File failed to open" << endl;                  // if the file fails to open
        cout << "Please re-enter the pathway" << endl;
    }

    cout << "File opened" << endl;
}

int firstArray(ifstream& input, string strArr[])        
{
    string name;
    int i = 0;
    while (i<9 && !input.eof())
    {
        getline(input, name);      //this loop is inputing both names and numbers into the array
        strArr[i] = name;          //your input file alternates between names and numbers (I assume phone numbers)    
        i++;                       //this isn't being taken into account
        input.ignore();
    }

    return i;
}

void secondArray(ifstream& input, int intArr[])
{
    int i = 0;

    while (i<9 && !input.eof())
    {
        input.ignore();            //what are you trying to input here exactly?
        input >> intArr[i];        //I assum this function was supposed to input the numbers
        i++;                       //but some of the numbers were input by the other array
    }
}


Just some thoughts
Topic archived. No new replies allowed.