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?

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:

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
}

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());


}



}

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

What mitrmkar said.

You can prevent these errors by indenting your code.

Example what you write:

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:

int main()
{
  for (int i = 0; i < 5;i++)
  {
    for (int j= 0; j < 5;j++)
    {
      if (j==i) 
      {
        cout << "yeah";
      }
    }
  }
  return 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?

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;    

}

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?

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;    

}

Look at your struct:

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]
};

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.

Note on my last post. Your struct actually contains 6 pieces of data:

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]
};

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.

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

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 ();    

}

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

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 ();    

}

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.

Hey i've done what you've said but it stills seems to only read the one value and then the program never ends. do i need a getline sorta thing? or anything else

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())
      { 
       cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", "  << stu.exam << endl;
       cin >> stuList[size].id >> stuList[size].assignments[0] >> stuList[size].assignments[1] >> stuList[size].assignments[2] >> stuList[size].exam;
       cout << endl;
       size++;	
      }
      return true;
    }
    else
     return false;
     
  inFile.close ();    

}

Hey i've done what you've said but it stills seems to only read the one value and then the program never ends. do i need a getline sorta thing? or anything else

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())
      { 
       cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", "  << stu.exam << endl;
       cin >> stuList[size].id >> stuList[size].assignments[0] >> stuList[size].assignments[1] >> stuList[size].assignments[2] >> stuList[size].exam;
       cout << endl;
       size++;	
      }
      return true;
    }
    else
     return false;
     
  inFile.close ();    

}

Get rid of lines 12 and 15. You don't want/need stu. stu is only read into once, so it's going to be the same each time when you output it. Stick some debugging output (i.e. some cout statements saying various things) in different places in your program to see how far it gets. You need to know whether you ever return from your function, whether you get into an infinite loop, what size is, etc., so put some cout statements before your while loop, after your while loop, and inside your while loop. You'll delete them later. If your belief that it only reads one line is based on it displaying the same thing over and over, like I said, that's because you are displaying stu, which is never read into in your while loop. Also, replace cin in line 16 with inFile. That's what your problem is. However, sticking cout statements at different spots is an excellent way to narrow down where an error is. The cin in this case is your problem. It's waiting for you to type something. However, line 12 needs to go and line 15 either needs to go or be changed and moved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.