I need help figuring this out. What I need is this:

  1. The section number, as a string
  2. The instructor's name, also as a string
  3. The number of students in this section, as an integer.
  4. The student's scores for the test, as an array of integers. Declare this array to be of size 30. Do Not use a vector in this exercise!

provide the following member functions in your class:

  1. A constructor that takes the section and instructor's name. It should set the number of students to zero and initialize all of the array elements to zero.
  2. A readData( )function to read in student scores from a file. This function should verify that the file opened correctly, and that each piece of data was successfully read. As you read in each score, store it in the array, and increment the variable that holds the number of students in the class. Read until you encounter an end of file condition.
  3. a function that returns the number of scores stored in the array,
  4. a function that takes in integer value as a parameter, and returns the score at that position in the array of scores.
  5. a function that returns the highest score on the test,
  6. a function that returns the lowest score on the test,
  7. a function that calculates the average score on the test, and
  8. optionally a function that sorts the array (extra credit).

Your main( ) routine should work as follows:

  1. Display your student information.
  2. Creates an object of the Section class.
  3. Prompt the user to enter in a file name
  4. Read in all of the scores from the file, using the readData( ) function described above. The object should now contain the number of scores read, and the scores should be stored in the array.
  5. Optionally sort the array (extra credit).
  6. Call the function that returns the number of scores stored in the array.
  7. Using this value in a loop, display all of the scores in the array.
  8. Call the function that returns the maximum score, and display it.
  9. Call the function that returns the minimum score, and display it
  10. Call the function that returns the average score, and display it.

What I have so far is:

section.cpp

#include <iostream>
#include <fstream>
#include "section.h"

using namespace std;


int main()
{
    Section object; 
    Section::readData();
   system("PAUSE");
   cin.get();
   return 0;
}

section.h

using namespace std;

class Section
{
private:
    string section;
    string instName;
    int students;
    const int SIZE = 30;
    int array[SIZE];
    
public:
    //Section[array];
    readData();
    num_scores();
    /* high_score();
    low_score();
    avg_score();*/
};

void Section::readData()
{
            std::string filename;
        cout << "Please enter in the path to the student score file: " << endl;
        getline(cin, filename);
        
        ifstream theFile(filename.c_str());
        
            
    if (theFile.good( ) )
   {
       cout << "Test Scores are: " << endl;
       
    int number = 0;
    double average = 0;
    int score = 0;
    int count = 0;
    int max = 0;
    int min = 0;
    while (theFile >> number )
    {    
        if ( count == 0)
        {
            max = number;
            min = number;
        }
        else
        {
        if (number > max)
            max = number;
        if (number < min)
            min = number;
        }
        
        score += number;
        count++;
    cout << "\n" << number;        
    }
    average = score / count;
    cout << "\nLow score " << min << endl;
    cout << "\nHigh score " << max << endl;
    cout << "\nAverage score = " << average << endl;
    
    }
   else
   {
       cout << "\nCould not open the file...";
    
   }
   theFile.close();
   
}

Not sure where to go from here... :(

Dani AI

Generated

Two quick, practical fixes to move forward (and answers to points raised by @Ancimforever and ): keep declarations in the header and all non-inline implementations in a .cpp; construct an object in main and call its methods on that instance (do not call an instance method as if it were a static function). Write and test each method one at a time: constructor, readData, accessor methods, then statistics and optional sort.

A minimal, correct header layout (put this in section.h):

#ifndef SECTION_H
#define SECTION_H

#include <string>

class Section {
public:
    static const int SIZE = 30;
    Section(const std::string& sectionId, const std::string& instructor);
    void readData(const std::string& filename);
    int num_scores() const;
    int get_score(int index) const;
    int high_score() const;
    int low_score() const;
    double avg_score() const;
    void sort_scores();

private:
    std::string sectionId_;
    std::string instructor_;
    int students_;
    int scores_[SIZE];
};

#endif

Implementation notes (in section.cpp): initialize students_ to 0 and zero the array in the constructor; in readData open an std::ifstream, check it opened, then use a loop like while (students_ < SIZE && in >> value) scores_[students_++] = value; to avoid overrunning the array. Compute sum/min/max either while reading or in separate passes; for average use static_cast<double>(sum)/students_ and guard against division by zero. get_score should check bounds and either throw std::out_of_range or return a sentinel. In main (separate .cpp) create a Section, prompt for filename, call readData(filename), then use num_scores() and get_score(i) to print results.

Small cautions: avoid putting executable code in headers, avoid system("PAUSE"), and handle the case where the file contains more than 30 numbers (report or truncate). Test each method after you write it, as advised.

Recommended Answers

All 3 Replies

>>provide the following member functions in your class:
does your class contain all 9 required methods ? If not then you need to code them as described in the program requirements. Just start with #1 and code each one in order. You should also test each method after coding it to insure that it works correctly. Do not wait until after you coded all of them because that will just make debugging efforts more difficult.

I think I can skip 1 for now. I coded 2 but I think I put it with section.h when it may need to go in section.cpp at the bottom as a single function. Also, not sure how to read in the scores and store them in an array. Do I need to create a separate file for these functions like testSection.cpp or they go in section.cpp(main) or section.h?

I think I can skip 1 for now. I coded 2 but I think I put it with section.h when it may need to go in section.cpp at the bottom as a single function. Also, not sure how to read in the scores and store them in an array. Do I need to create a separate file for these functions like testSection.cpp or they go in section.cpp(main) or section.h?

class declaration does in .h file -- no executable code other than inline code goes in the header file. That means you have to remove void Section::readData() from section.h.

section.cpp should only contain the implementation code for the methods that are in section.h file. main() should not be in that file, but for now I suppose it is ok to leave it there. Ultimately you will want main() in some other cpp file, such as main.cpp

>>I think I can skip 1 for now.
NO. do not skip any of the requirements. Do them in the order that they are stated.

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.