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:
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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...
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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
}
}