I am having problems with reading a file into an array. This is my code. This is
ID: 3918895 • Letter: I
Question
I am having problems with reading a file into an array.
This is my code.
This is what I get when I run my program.
But this is my text file I am reading.
I tried everything and it seems to be reading in the last digit of the file. I want to read in their names line by line into an array and ultimatly also read in the scores line by line.
1 E/7 Programming Assignment 6.cpp Defines the entry point for the console application. 2 #include #include #include #include "stdafx.h" "string" "iostream"' "fstream" 6 7 8 9 10 using namespace std; //read and store data into two arrays 12 Ebool GetBowlingData(string fileName, string nameArray[]) fstream myfile; 14 15 16 17 18 19 20 21 : myfile.open(fileName); ?: if (!myfile . fail()) : //loop until the last line of the file : : while (!myfile . eof()) 1//populate the arrays from the fil 23 24 25 26 27 28 29 30 int n 0; myfile nameArray[n :return true; return falseExplanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int n=0;
bool GetBowlingData(string filename,string nameArray[])
{
fstream file;
file.open(filename);
if(!file.fail())
{
while(!file.eof())
{
file>>nameArray[n];
n++;
}
return true;
}
return false;
}
int main()
{
string fileBowlingScores,bowlersNames[20];
int bowlersScore[20],averageScore[20];
cout<<"Please"<<endl;
cin>>fileBowlingScores;
GetBowlingData(fileBowlingScores,bowlersNames);
for(int i=0;i<n;i++)
{
cout<<bowlersNames[i]<<endl;
}
return 0;
}
This will get each and every word in the file(even the numbers are considered as a word as it is a text file) into the array yiou provided. Now you will have to change the change the string into numbers. I didnt know how you want that as a 1d array or 2d array. Make sure you give all the necessary details in your next question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.