| | |
Help Reading Info in Text File Into an Array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
Hi,
I'm working an a project that must take a text file (provided to us, in.txt) that has the scores of students from different sections on different labs. You can view the assignment page at: http://www.cs.wmich.edu/~nelson/CS11...ab06/index.htm.
The problem I'm having is that I can't think of how to make a function that will read the file and put it into an array (no function in this program is supposed to be more than 24 lines so there's no way it can go into the main funciton, I was planning on making a readFile function to do this work).
Can you pass a file to a funciton as a paramater (I got an error, I didn't think it was possible, just wanted to try)? Should I read the file in main and somehow pass it to the function in some way? I'm also quite lost as far as I should go about getting the data from the text file stored correctly into the array. This is an example of how the info in the data file is stored:
The input file:
Name: “in.txt� you must use the name in.txt
First line 2 integers:
The number of sections in the class <= 10
The number of lab assignments in the class, NumLabs <= 10
For Each Section
First Line, 1 Integer:Â N The number of students in the section <= 50
For each Student
Student Id L1 L2 … LNumLabs
Student Id: 4 digit integer
Labs scores: Integers 0 to 100
Example: A small class with 3 sections and 4 labs.
The first section has 4 students, the second 5 and the third 4
I tried setting up some for loops in a test program to read info from the input file into an array (not included into my actual program because as stated above I can't figure out how to implement it into my actual program) but go no where. Could someone help me figure out how to go about this? I would be very grateful to any help.
thanks again for everything,
-Scott
Â
I'm working an a project that must take a text file (provided to us, in.txt) that has the scores of students from different sections on different labs. You can view the assignment page at: http://www.cs.wmich.edu/~nelson/CS11...ab06/index.htm.
The problem I'm having is that I can't think of how to make a function that will read the file and put it into an array (no function in this program is supposed to be more than 24 lines so there's no way it can go into the main funciton, I was planning on making a readFile function to do this work).
Can you pass a file to a funciton as a paramater (I got an error, I didn't think it was possible, just wanted to try)? Should I read the file in main and somehow pass it to the function in some way? I'm also quite lost as far as I should go about getting the data from the text file stored correctly into the array. This is an example of how the info in the data file is stored:
The input file:
Name: “in.txt� you must use the name in.txt
First line 2 integers:
The number of sections in the class <= 10
The number of lab assignments in the class, NumLabs <= 10
For Each Section
First Line, 1 Integer:Â N The number of students in the section <= 50
For each Student
Student Id L1 L2 … LNumLabs
Student Id: 4 digit integer
Labs scores: Integers 0 to 100
Example: A small class with 3 sections and 4 labs.
The first section has 4 students, the second 5 and the third 4
C++ Syntax (Toggle Plain Text)
3 4 4 8591 87 60 82 64 1881 95 63 75 94 6758 71 56 72 63 7541 65 95 84 87 5 3690 88 86 99 68 3515 78 60 98 53 1534 91 95 65 81 2459 86 69 79 50 7047 70 61 63 71 4 6204 89 98 56 69 6437 92 67 95 87 2012 67 97 66 54 8147 90 50 63 100
thanks again for everything,
-Scott
Â
•
•
Join Date: Nov 2004
Posts: 19
Reputation:
Solved Threads: 2
Hi,
I found the following article when looking for help writing and reading parameter storage files as CSV files for a current application.
Although the example is based on .NET code you should be able to get some ideas on how you can write a similar function yourself.
http://www.developer.com/net/cplus/article.php/3406181
I found the following article when looking for help writing and reading parameter storage files as CSV files for a current application.
Although the example is based on .NET code you should be able to get some ideas on how you can write a similar function yourself.
http://www.developer.com/net/cplus/article.php/3406181
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by skamen
I finally figured out the getArrayValues function and how to make it work. Logically my code I have should work but I'm not quite sure why I'm getting errors. W/ Visual C++ I get an unexpected end of file and with Linux I get a "
lab06.cxx: In function `void getArrayValues(int (*)[50], int*, int, int)':
lab06.cxx:65: error: parse error at end of input". The code I have thus far is:
Can anyone help me fix these errors?C++ Syntax (Toggle Plain Text)
/* Name: Scott Kamen Student Number: 9054 Assignment number and descrition: Lab 06 Assignment (Program to open and read a student's scores for a semester. Display a chart with the average and median scores for each section and each lab.) Lab Instructor: Huma Kamal Lab Day and Time: Wed at 6:00 */ #include <iostream> #include <fstream> #include <cstdlib> using namespace std; // Prototypes for Program void getArrayValues (int scores [] [50], int labs [], int numsections, int numlabs); int main() { // Declare Variables to Store Array Information int scores [10] [50]; int labs [7]; int numsections; int numlabs; getArrayValues (scores, labs, numsections, numlabs); return 0; } void getArrayValues (int scores [] [50], int labs [], int numsections, int numlabs) { // Declare Stream and Open Text File ifstream inStream; inStream.open("in.txt"); if (inStream.fail()) { cerr << "Can't open file!\n"; exit(1); } else { while (!inStream.eof()) { //"Garbage" Variable to Get Ride of Student ID Number int id; // Get Number of Sections and Labs from Text File inStream >> numsections >> numlabs; // Nested For Loops to Read Info from Text File into Array for (int i = 0; i < numsections; i++) { inStream >> labs[i]; for (int j = 0; j < labs[i]; j++) { inStream >> id; for(int k = 0; k < numlabs; j++){ inStream >> scores[j][i]; } } } // Close File inStream.close(); } }
thanks so much,
-Scott
thanks yet again,
-Scott
![]() |
Similar Threads
- Reading a list from txt file to array... (Pascal and Delphi)
- Code Snippet: Reading data from a text file into an array (Pascal and Delphi)
- Reading from text file into array (C++)
- cast text file to an array (C#)
- Loading A Text File into a Array into a Listbox (Visual Basic 4 / 5 / 6)
- Reading numbers from a text file (Java)
Other Threads in the C++ Forum
- Previous Thread: help with this prog
- Next Thread: Need advice using openGl with win XP
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets





