Question

I am having problems with reading a file into an array.

This is my code.

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 false

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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.

Add a comment
Know the answer?
Add Answer to:
I am having problems with reading a file into an array. This is my code. This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • Hi I was hoping to get some help with my code for this problem. I don't...

    Hi I was hoping to get some help with my code for this problem. I don't know how to correctly read a txt file and the difference between using string and cstring. My code is at the bottom please help thanks. 1. Create the following “data.txt” file. Each record consists of customer's name and their balance: Maria Brown 2100.90 Jeffrey Jackson 200.20 Bernard Smith 223.10 Matthew Davenport 1630.20 Kimberly Macias 19100.90 Amber Daniels 2400.40 The records in the file will...

  • The input file is already there. the file is too long, don't have to send it. using virtualbox ubuntu We want to cre...

    The input file is already there. the file is too long, don't have to send it. using virtualbox ubuntu We want to create a command line terminal where user can run the snap commands with three different options: -thanos, -ironman and -holk. The terminal must keep listening to user command. If the user type:  snap -thanos: all the text line in the text file “input.txt” that does not contain the word “thanos” must be randomly removed. The remaining text...

  • So my test.h file has: #include <iostream> #include <fstream> using namespace std; class test { private:...

    So my test.h file has: #include <iostream> #include <fstream> using namespace std; class test { private:    int data[]; public:    test(string filename); }; My test.cpp file has: #include "sort.h" test::test(string filename) {    ifstream inFile;    inFile.open(filename);    int iter = 0;    while(!inFile.eof())    {        cin >> data[iter];        iter++;    }    int numberOfNumbers = iter;    for (iter = 0; iter < numberOfNumbers; iter++)        cout << data[iter] << " ";   ...

  • Need to check if File is successfully opened? C++ It works when I input the right...

    Need to check if File is successfully opened? C++ It works when I input the right txt file. But, it keeps stating, File successfully open." even I put in a non-existing file. Here are the specifications: Develop a functional flowchart and then write a C++ program to solve the following problem. Create a text file named first.txt and write your first name in the file. You will be reading the name of the file from the keyboard as a string,...

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • Using Java, I am trying to read in a file with the format outlined below. "AARON,...

    Using Java, I am trying to read in a file with the format outlined below. "AARON, ELVIA J"   WATER RATE TAKER   WATER MGMNT   "$87,228.00" "AARON, JEFFERY M"   POLICE OFFICER   POLICE   "$75,372.00" "AARON, KARINA"   POLICE OFFICER   POLICE   "$75,372.00" "AARON, KIMBERLEI R"   CHIEF CONTRACT EXPEDITER   GENERAL SERVICES   "$80,916.00" "ABAD JR, VICENTE M"   CIVIL ENGINEER IV   WATER MGMNT   "$99,648.00" "ABARCA, ANABEL"   ASST TO THE ALDERMAN   CITY COUNCIL   "$70,764.00" "ABARCA, EMMANUEL"   GENERAL LABORER - DSS   STREETS & SAN   "$40,560.00" "ABBATACOLA, ROBERT J"   ELECTRICAL MECHANIC  ...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT