Inputting text file into array and finding top 3 values

Reply

Join Date: May 2008
Posts: 5
Reputation: JoE Guana is an unknown quantity at this point 
Solved Threads: 0
JoE Guana JoE Guana is offline Offline
Newbie Poster

Inputting text file into array and finding top 3 values

 
0
  #1
May 27th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,755
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #2
May 27th, 2008
Code tags and formatting please:

[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
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:
  1. bool readStudents(string fName, studentType stuList[], int& size);
  2. {
  3. // declare an ifstream
  4. // open the ifstream using fName as the filename
  5. // if unsuccessfule, return false
  6. // intialize size to 0.
  7.  
  8. // set up loop
  9. // read data into stuList[size]
  10. // increment size
  11. // if more data exists, repeat loop
  12.  
  13. // close ifstream
  14. // return true
  15. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 5
Reputation: JoE Guana is an unknown quantity at this point 
Solved Threads: 0
JoE Guana JoE Guana is offline Offline
Newbie Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #3
May 27th, 2008
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());


}



}
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Inputting text file into array and finding top 3 values

 
0
  #4
May 27th, 2008
Originally Posted by JoE Guana View Post
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?
Hi,

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,753
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 294
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Maven

Re: Inputting text file into array and finding top 3 values

 
0
  #5
May 27th, 2008
What mitrmkar said.

You can prevent these errors by indenting your code.

Example what you write:
  1. int main()
  2. {
  3. for (int i = 0; i < 5;i++)
  4. {
  5. for (int j= 0; j < 5;j++)
  6. {
  7. if (j==i)
  8. {
  9. cout << "yeah";
  10. }
  11. }
  12. }
  13. return 0;
  14. }

Now if we indent this we can see which block of code belongs to which loop:
  1. int main()
  2. {
  3. for (int i = 0; i < 5;i++)
  4. {
  5. for (int j= 0; j < 5;j++)
  6. {
  7. if (j==i)
  8. {
  9. cout << "yeah";
  10. }
  11. }
  12. }
  13. return 0;
  14. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 5
Reputation: JoE Guana is an unknown quantity at this point 
Solved Threads: 0
JoE Guana JoE Guana is offline Offline
Newbie Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #6
May 27th, 2008
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?

  1. bool readStudents(string fName, studentType stuList[], int& size)
  2. {
  3.  
  4. ifstream inFile;
  5. studentType stu;
  6. size = 0;
  7.  
  8. inFile.open (fName.c_str());
  9.  
  10. if (inFile.is_open())
  11. {
  12. inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
  13. while(inFile)
  14. {
  15. int n = 0;
  16. size++;
  17. cout << stu.id << ' ' << stu.assignments[n] << ", " << stu.exam << endl;
  18. inFile >> stu.id >> stu.assignments[n] >> stu.exam;
  19. n++;
  20.  
  21. for( int i = 0; i < 10; i++ ) // loads the student list into the array
  22. {
  23. cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam;
  24. cout << endl;
  25. }
  26.  
  27. }
  28. return true;
  29. }
  30. else
  31. return false;
  32.  
  33. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,755
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #7
May 27th, 2008
Originally Posted by JoE Guana View Post
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?

  1. bool readStudents(string fName, studentType stuList[], int& size)
  2. {
  3.  
  4. ifstream inFile;
  5. studentType stu;
  6. size = 0;
  7.  
  8. inFile.open (fName.c_str());
  9.  
  10. if (inFile.is_open())
  11. {
  12. inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
  13. while(inFile)
  14. {
  15. int n = 0;
  16. size++;
  17. cout << stu.id << ' ' << stu.assignments[n] << ", " << stu.exam << endl;
  18. inFile >> stu.id >> stu.assignments[n] >> stu.exam;
  19. n++;
  20.  
  21. for( int i = 0; i < 10; i++ ) // loads the student list into the array
  22. {
  23. cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam;
  24. cout << endl;
  25. }
  26.  
  27. }
  28. return true;
  29. }
  30. else
  31. return false;
  32.  
  33. }
Look at your struct:
  1. struct studentType
  2. {
  3. string id; // unique id
  4. float assignments[3]; // scores for 3 assignments - in the scope of [0,15]
  5. float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100]
  6. };
Look at lines 12 and 18 of your code. Your data (see below) matches your struct and is in groups of 5. You need to read it in as such:

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
Line 12 reads in 4 pieces of data, line 18 reads in 3. You should be reading five pieces at a time.

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,755
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #8
May 27th, 2008
Note on my last post. Your struct actually contains 6 pieces of data:
  1. struct studentType
  2. {
  3. string id; // unique id
  4. float assignments[3]; // scores for 3 assignments - in the scope of [0,15]
  5. float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100]
  6. };
Your data contains 5 pieces of data.
s9000004 6 9 6 70
So they actually DON'T match. You need to CALCULATE total. Don't try to read it in from the data.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 5
Reputation: JoE Guana is an unknown quantity at this point 
Solved Threads: 0
JoE Guana JoE Guana is offline Offline
Newbie Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #9
May 27th, 2008
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



  1. bool readStudents(string fName, studentType stuList[], int& size)
  2. {
  3.  
  4. ifstream inFile;
  5. studentType stu;
  6. size = 0;
  7.  
  8. inFile.open (fName.c_str());
  9.  
  10. if (inFile.is_open())
  11. {
  12. inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam;
  13. while(!inFile.eof())
  14. {
  15.  
  16. size++;
  17. cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl;
  18. inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam;
  19.  
  20. for( int i = 0; i < 10; i++ ) // loads the student list into the array
  21. {
  22. cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam;
  23. cout << endl;
  24. }
  25.  
  26. }
  27. return true;
  28. }
  29. else
  30. return false;
  31.  
  32. inFile.close ();
  33.  
  34. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,755
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Inputting text file into array and finding top 3 values

 
0
  #10
May 27th, 2008
Originally Posted by JoE Guana View Post
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



  1. bool readStudents(string fName, studentType stuList[], int& size)
  2. {
  3.  
  4. ifstream inFile;
  5. studentType stu;
  6. size = 0;
  7.  
  8. inFile.open (fName.c_str());
  9.  
  10. if (inFile.is_open())
  11. {
  12. inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam;
  13. while(!inFile.eof())
  14. {
  15.  
  16. size++;
  17. cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl;
  18. inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam;
  19.  
  20. for( int i = 0; i < 10; i++ ) // loads the student list into the array
  21. {
  22. cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam;
  23. cout << endl;
  24. }
  25.  
  26. }
  27. return true;
  28. }
  29. else
  30. return false;
  31.  
  32. inFile.close ();
  33.  
  34. }
You misspelled cplusplus (forgot the first s) in the code tags. That's why it didn't highlight correctly.

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC