| | |
Inputting text file into array and finding top 3 values
![]() |
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
Hey guys,
i'm new here and i'm also new to C++. I have a task where I have the user input a file name and then the program loads it into an array of type struct then finds the top 3 values and outputs them. I am so stuck right now and don't know what to do?
the text files they give me have this text in it
s9000004 6 9 6 70
s7000001 10 10 12 75
s8000002 9 7 10 65
s8000004 6 9 6 50
s7000002 9 7 10 45
s7000004 6 9 6 50
s0000002 9 7 10 35
s0000004 6 9 6 50
s0000001 10 15 15 75
which is the student ID and three scores for assignments and the final exam score. Basically the program adds up the scores and finds the top three students and outputs them.
This is what the output should look like
Enter the filename of the student data file: in2.txt
There are 9 students in total.
Top Student List
s0000001 85.00
s7000001 77.00
s8000002 65.00
Press any key to continue . . .
and this is my code so far
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int TOP_NUM = 3;
struct studentType
{
string id; // unique id
float assignments[3]; // scores for 3 assignments - in the scope of [0,15]
float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100]
};
bool readStudents(string fName, studentType stuList[], int& size);
// pre: none
// post: read students id and scores into the one-dimentional array of struct: stuList
// The actual size of the array is returned by reference paramenter size
// this function returns false if an error occurred when opening or reading the specified file, otherwise returns true
void printStudents(const studentType stuList[], int size);
// pre: none
// post: print the id, and the total score in array StuList
// size stores the the number of elements in stuList
// declare other functions if necessary
int main()
{
studentType studentList[30]; // the array used for storing the information of students from a file
int numOfStudents = 0; // numOfStudents is the actual number of students in the data file
string fileName; // it stores the file name of the data file
// declare other variables if necessary
cout << "Enter the filename of the student data file: " << flush; // don't modify it
cin >> fileName; // don't modify it
cout << endl; // don't modify it
if (!readStudents(fileName, studentList, numOfStudents)) // don't modify it
cout << "Input file error." << endl; // don't modify it
else // don't modify it
{ // don't modify it
// write your code here for processing
// don't modify the rest code
if (numOfStudents >= 2)
cout << "There are " << numOfStudents << " students in total. " << endl << endl; // don't modify it
else // don't modify it
cout << "There is " << numOfStudents << " student in total. " << endl << endl; // don't modify it; there is 1 student
cout << "Top Student List" << endl; // don't modify it
// write your code to diplay top students
} // don't modify it
cout << endl; // don't modify it
system("pause"); // don't modify it
return 0; // don't modify it
}
void printStudents(const studentType stuList[], int size);
{
}
bool readStudents(string fName, studentType stuList[], int& size);
{
}
I'm not sure where to go from here. I'm not sure what to write in the functions?
can anybody help me?
i'm new here and i'm also new to C++. I have a task where I have the user input a file name and then the program loads it into an array of type struct then finds the top 3 values and outputs them. I am so stuck right now and don't know what to do?
the text files they give me have this text in it
s9000004 6 9 6 70
s7000001 10 10 12 75
s8000002 9 7 10 65
s8000004 6 9 6 50
s7000002 9 7 10 45
s7000004 6 9 6 50
s0000002 9 7 10 35
s0000004 6 9 6 50
s0000001 10 15 15 75
which is the student ID and three scores for assignments and the final exam score. Basically the program adds up the scores and finds the top three students and outputs them.
This is what the output should look like
Enter the filename of the student data file: in2.txt
There are 9 students in total.
Top Student List
s0000001 85.00
s7000001 77.00
s8000002 65.00
Press any key to continue . . .
and this is my code so far
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int TOP_NUM = 3;
struct studentType
{
string id; // unique id
float assignments[3]; // scores for 3 assignments - in the scope of [0,15]
float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100]
};
bool readStudents(string fName, studentType stuList[], int& size);
// pre: none
// post: read students id and scores into the one-dimentional array of struct: stuList
// The actual size of the array is returned by reference paramenter size
// this function returns false if an error occurred when opening or reading the specified file, otherwise returns true
void printStudents(const studentType stuList[], int size);
// pre: none
// post: print the id, and the total score in array StuList
// size stores the the number of elements in stuList
// declare other functions if necessary
int main()
{
studentType studentList[30]; // the array used for storing the information of students from a file
int numOfStudents = 0; // numOfStudents is the actual number of students in the data file
string fileName; // it stores the file name of the data file
// declare other variables if necessary
cout << "Enter the filename of the student data file: " << flush; // don't modify it
cin >> fileName; // don't modify it
cout << endl; // don't modify it
if (!readStudents(fileName, studentList, numOfStudents)) // don't modify it
cout << "Input file error." << endl; // don't modify it
else // don't modify it
{ // don't modify it
// write your code here for processing
// don't modify the rest code
if (numOfStudents >= 2)
cout << "There are " << numOfStudents << " students in total. " << endl << endl; // don't modify it
else // don't modify it
cout << "There is " << numOfStudents << " student in total. " << endl << endl; // don't modify it; there is 1 student
cout << "Top Student List" << endl; // don't modify it
// write your code to diplay top students
} // don't modify it
cout << endl; // don't modify it
system("pause"); // don't modify it
return 0; // don't modify it
}
void printStudents(const studentType stuList[], int size);
{
}
bool readStudents(string fName, studentType stuList[], int& size);
{
}
I'm not sure where to go from here. I'm not sure what to write in the functions?
can anybody help me?
•
•
Join Date: Jan 2008
Posts: 3,755
Reputation:
Solved Threads: 491
Code tags and formatting please:
[code=cplusplus]
// paste code here
[/code]
How are these scores calculated? There must be some weighted average and these are percentages, but what is the test worth and what are the three assignments worth?
Just looked at the code and apparently the test is worth 100 and each assignment is worth 15. Please put that in the writeup in the future.
Start with the read function:
[code=cplusplus]
// paste code here
[/code]
•
•
•
•
s7000001 10 10 12 75
s8000002 9 7 10 65
s0000001 10 15 15 75
Top Student List
s0000001 85.00
s7000001 77.00
s8000002 65.00
Just looked at the code and apparently the test is worth 100 and each assignment is worth 15. Please put that in the writeup in the future.
Start with the read function:
C++ Syntax (Toggle Plain Text)
bool readStudents(string fName, studentType stuList[], int& size); { // declare an ifstream // open the ifstream using fName as the filename // if unsuccessfule, return false // intialize size to 0. // set up loop // read data into stuList[size] // increment size // if more data exists, repeat loop // close ifstream // return true }
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
Hey thanks for the help
I made this function i think it'll do the job. But i keep getting an expected declaration before "}". What do you guys think?
bool readStudents(string fName, studentType stuList[], int& size)
{
ifstream inFile;
studentType stu;
size = 0;
inFile.open (fName.c_str());
if (inFile.is_open())
{
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
while(inFile)
{
size++;
cout << stu.id << ' ' << stu.assignments[0] << ", " << stu.exam << ' ' << stu.total << endl;
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
}
return true;
}
else
return false;
inFile.close (fName.c_str());
}
}
I made this function i think it'll do the job. But i keep getting an expected declaration before "}". What do you guys think?
bool readStudents(string fName, studentType stuList[], int& size)
{
ifstream inFile;
studentType stu;
size = 0;
inFile.open (fName.c_str());
if (inFile.is_open())
{
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
while(inFile)
{
size++;
cout << stu.id << ' ' << stu.assignments[0] << ", " << stu.exam << ' ' << stu.total << endl;
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
}
return true;
}
else
return false;
inFile.close (fName.c_str());
}
}
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
Hey
I made this function i think it'll do the job. But i keep getting an expected declaration before "}". What do you guys think?
First of all, I repeat what VernonDozier already stated;
Use code tags and formatting please:
see here
http://www.daniweb.com/forums/misc-explaincode.html
Then about the errors ...
bool readStudents(string fName, studentType stuList[], int& size)
{
ifstream inFile;
studentType stu;
size = 0;
inFile.open (fName.c_str());
if (inFile.is_open())
{
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
while(inFile)
{
size++;
cout << stu.id << ' ' << stu.assignments[0] << ", " << stu.exam << ' ' << stu.total << endl;
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
}
return true;
}
else
return false;
// the close() function takes no parameters,
// the name of the file is already known by the ifstream object,
// so just simply ...
inFile.close ();
}
} // <- This brace must be removed What mitrmkar said.
You can prevent these errors by indenting your code.
Example what you write:
Now if we indent this we can see which block of code belongs to which loop:
You can prevent these errors by indenting your code.
Example what you write:
C++ Syntax (Toggle Plain Text)
int main() { for (int i = 0; i < 5;i++) { for (int j= 0; j < 5;j++) { if (j==i) { cout << "yeah"; } } } return 0; }
Now if we indent this we can see which block of code belongs to which loop:
C++ Syntax (Toggle Plain Text)
int main() { for (int i = 0; i < 5;i++) { for (int j= 0; j < 5;j++) { if (j==i) { cout << "yeah"; } } } return 0; }
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
Ok thanks guys for all the help sorry to be such a n00b at all this. I've got it to sorta work now, but it seems to only do one line and not load all the values of the text document into the stuList[] array.
Can anybody see a fix for this?
Can anybody see a fix for this?
C++ Syntax (Toggle Plain Text)
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total; while(inFile) { int n = 0; size++; cout << stu.id << ' ' << stu.assignments[n] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[n] >> stu.exam; n++; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; }
•
•
Join Date: Jan 2008
Posts: 3,755
Reputation:
Solved Threads: 491
•
•
•
•
Ok thanks guys for all the help sorry to be such a n00b at all this. I've got it to sorta work now, but it seems to only do one line and not load all the values of the text document into the stuList[] array.
Can anybody see a fix for this?
C++ Syntax (Toggle Plain Text)
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total; while(inFile) { int n = 0; size++; cout << stu.id << ' ' << stu.assignments[n] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[n] >> stu.exam; n++; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; }
C++ Syntax (Toggle Plain Text)
struct studentType { string id; // unique id float assignments[3]; // scores for 3 assignments - in the scope of [0,15] float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100] };
•
•
•
•
s9000004 6 9 6 70
s7000001 10 10 12 75
s8000002 9 7 10 65
s8000004 6 9 6 50
s7000002 9 7 10 45
s7000004 6 9 6 50
s0000002 9 7 10 35
s0000004 6 9 6 50
s0000001 10 15 15 75
Thank you for formatting. A word of caution though. What looks good in your IDE may not look good when you post here. This happens a lot when people mix tabs and spaces. If your IDE (or whatever you are working in) has 4 spaces for a tab and you interchange spaces with tabs, what lines up in your IDE/word processor may not line up here, where a tab is something like 8 spaces. Solution? Use tabs or spaces, but not both. Or, another way is to format however you like in your IDE. Then, before posting here, look in your IDE's preferences for formatting, and often there is an option to change all spaces to tabs or vice-versa. If you do that, regardless of whether the spaces per tab default is here at Daniweb and on your IDE, things will line up.
•
•
Join Date: Jan 2008
Posts: 3,755
Reputation:
Solved Threads: 491
Note on my last post. Your struct actually contains 6 pieces of data:
Your data contains 5 pieces of data.
So they actually DON'T match. You need to CALCULATE total. Don't try to read it in from the data.
C++ Syntax (Toggle Plain Text)
struct studentType { string id; // unique id float assignments[3]; // scores for 3 assignments - in the scope of [0,15] float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100] };
•
•
•
•
s9000004 6 9 6 70
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
Hey guys. The program is still having issues. It seems to only get the first line from the text file and i dont think its putting it into the array.
Any help would be appreciated
Any help would be appreciated
C++ Syntax (Toggle Plain Text)
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; while(!inFile.eof()) { size++; cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; inFile.close (); }
•
•
Join Date: Jan 2008
Posts: 3,755
Reputation:
Solved Threads: 491
•
•
•
•
Hey guys. The program is still having issues. It seems to only get the first line from the text file and i dont think its putting it into the array.
Any help would be appreciated
C++ Syntax (Toggle Plain Text)
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; while(!inFile.eof()) { size++; cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; inFile.close (); }
Do you want the for-loop? You really don't know how many pieces of data this file has ahead of time. You need to read five pieces of data in, then check whether there is more. You are actually, in this case, trying to read in 12 lines from a file that only has nine lines. You read in (or at least try to read in) two lines before the for loop, then ten lines in the for loop. The two lines you read in before the for loop, you read into stu, but you never put into the array. You increment size exactly once, so it ends up with a value of 1, so your main program thinks only one line has been read in.
Get rid of the for loop. Keep the code inside of it, but replace i with size. Get rid of the code that reads into the stu variable. Keep the while loop structure in line 13 (may have to change it slightly, but it's in the right place). One line is read for every trip through that while loop. size is incremented exactly once inside that while loop.
![]() |
Other Threads in the C++ Forum
- Previous Thread: problem with base class
- Next Thread: Editing and deleting data in a text file
| Thread Tools | Search this Thread |
api array based binary bitmap build c++ c++intmain() c/c++ char class classes client code coding compile console conversion count counttheoccurenceofanintegerinthe10inputs delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption environment error file forms fstream function functions game givemetehcodez graph gui homeworkassignment homeworkhelp homeworkhelper i/o iamthwee ifstream input int integer java jni lib linkedlist linker loop looping loops map math matrix memory multiple multipledimensionarray news node numbers output parameter pointer problem program programming project python radix random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video vtk win32 windows winsock wordfrequency wxwidgets






