•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,849 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,809 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 11489 | Replies: 2
![]() |
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Rep Power: 5
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
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
Location: Australia
Posts: 19
Reputation:
Rep Power: 4
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:
Rep Power: 5
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?/* 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
I fixed the end of file error (added a missing curley brace at the end of the program) but then I got an error for passing an un-initialized variable to a function so I just initialized my numsection and numlab variables to 0 (they are re-assigned new values later so it really shouldn't matter what their initial value is). Now I get no compiler error but I do get a segmentation runtime error. I can't for the life of me figure this one out. If someone could help me with why I'm getting this runtime error that would be awesome.
thanks yet again,
-Scott
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Reading numbers from a text file (Java)
- Help with a 2D array from a text file (C++)
- Need Help in Reading characters from a text file (C++)
- Inputting text file data into an array, please help! (C++)
- Having roblems using PHP to send info as text file (PHP)
Other Threads in the C++ Forum
- Previous Thread: help with this prog
- Next Thread: Need advice using openGl with win XP


Linear Mode