Highschool C++ Homework Assignment

So just to start off, I really don't understand c++ much but want to learn it, and due to this I took the opportunity to be given a c++ elective course in my school, however even though it is a beginner class, my teacher isn't really explaining things in a way that I can grasp and really learn, so I need help with this assignment: Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score.

Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType.

Your program must contain at least the following functions:

a: A function to read the students’ data into the array.
b: A function to assign the relevant grade to each student.
c: A function to find the highest test score.
d: A function to print the names of the students having the highest test score.
Your program must output each student’s name in this form: last name followed by a comma, followed by a space, followed by the first name; the name must be left justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls.

Side Note: I am not necessarily asking anyone to do this entire assignment for me, I would like it if someone could give me a good start to this, or guide me on the right path to completing this. Thanks so much in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
 
using namespace std;

// declare studentType struct

// part a
inline void getData(ifstream& inFile, studentType sList[], int listSize)
{
    // function code
}

// part b
inline void calculateGrade(studentType sList[], int listSize)
{
    // function code
}

// part c
inline int highestScore(const studentType sList[], int listSize)
{
    // function code
}

// part d
inline void printResult(ofstream& outFile, const studentType sList[], int listSize)
{
    // function code
}
OK, to begin with, how about you write your main() function and create your struct?
I forgot to put it in my code, but this is in my main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "functions.cpp"

using namespace std;

const int NO_OR_STUDENTS = 20;

int main()
{
    ifstream inData;
    ofstream outData;
    studentType studentList[NO_OR_STUDENTS];

    inData.open("students.txt");
    if (!inData)
    {
        cout << "The input file does not exist. Program terminates!"
             << endl;
        return 1;
    }

    outData.open("out.txt");
    if (!outData)
    {
        cout << "Cannot open the output file. Program terminates!" 
             << endl;
        return 1;
    }

    // call getData
    // call calculateGrade
    // call printResult

    return 0;
}
Hello owmn,

I have noticed it has been a few days with no activity, so I thought I would give yu a push.

I find it helpful to break up the instructions, or specs as I call them, to get a better understanding of what is needed to be done.

I came up with this:

So just to start off, I really don't understand c++ much but want to learn it, and due to this I took
the opportunity to be given a c++ elective course in my school, however even though it is a beginner
class, my teacher isn't really explaining things in a way that I can grasp and really learn, so I
need help with this assignment:

Write a program that reads students’ names followed by their test scores.

The program should output each student’s name followed by the test scores and the relevant grade.

It should also find and print the highest test score and the name of the students having the highest test score.


*** A ****************************************************************************************
Student data should be stored in a struct variable of type studentType, which has four components:
  ● studentFName and studentLName of type string,
  ● testScore of type int (testScore is between 0 and 100)
  ● and grade of type char.
  
Suppose that the class has 20 students. Use an array of 20 components of type studentType.


*** B ****************************************************************************************
 Your program must contain at least the following functions:

  a: A function to read the students’ data into the array.
  b: A function to assign the relevant grade to each student.
  c: A function to find the highest test score.
  d: A function to print the names of the students having the highest test score.


*** C ****************************************************************************************
Your program must output each student’s name in this form: 
  ● last name followed by a comma, followed by a space, followed by the first name; the name must
be left justified.

Moreover, other than declaring the variables and opening the input and output files, the function main
should only be a collection of function calls.

Side Note: I am not necessarily asking anyone to do this entire assignment for me, I would like
it if someone could give me a good start to this, or guide me on the right path to completing this.



To start with in "main" the line #include "functions.cpp" should not be there. It is not proper to include a ".cpp" file within a ".cpp" file. This file should be compiled separately and linked to create an ".exe" file.

As part "A" of the specs say you need to create the struct. I would put this in its own header file and then include it in the ".cpp" files that need it.

You have a start on part "B" with the functions that you have started, but the "getData(...)" function is not quite the way you need to do this. From the specs it looks like you will be getting your data from a file.

It is always a great help if you post the input file so everyone will know what you are working with. It also helps to understand how to write the code.

I try to use the best name that I can to describe the variable or function name. In your case "ReadFile(...)" better describes what the function is doing whereas "getData(...)" would be better used for getting user input.

Since you are new to C++ the line using namespace std; should be avoided as it WILL get you in trouble in the future. An alternative would be:
1
2
3
4
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;


Also in "main" you create the file stream with std::ifstream inData; then open the file and check to see it it is open. Good job with the if statements. This will save you two lines of code
std::ifstream inData("students.txt");. This will not only create the file stream, but open the file.

I would also suggest opening the "ifstream" in the function where it is used. This way it is created, used and closed when the function ends. Since it only should be done once the function is the better place.

It is possible that the "ofstream" could also be done in a function, I am thinking it would work better in main.

I do not know what you are using to write you code or what IDE/compiler you are using which makes it hard for me to say anything about how you should be working with the program.

Some tips for you:

const int NO_OR_STUDENTS = 20;. I think you meant "OF" not "OR". From C++11 on the more updated way to write this would be constexpr size_t NO_OF_STUDENTS { 20 };. Where "constexpr" is the newer form of "const" and does the same thing. In simple terms you can think of "size_t" as another name of "unsigned int". The {}s or "uniform initializer" is the easiest way to initialize a variable.

An empty set of {}s will initialize any type of "int" to zero. For type "double"s it is "0.0" and for type "char" it is "\0". std::string, vectors, lists and some other containers are empty when created and do not need initialized unless you want to give it a value to start with. For a std::string it would look like { "string" } and if using it with a "char" it would be single quotes and one character.

Over time I have come to understand that structs and classes, when you get to them, the name should start with a capital letter. I believe this may have something to do with the fact that, in your case, "studentType", as the name tends to imply, is a type just like "int" or "std::string". Since it is not a built in type the capital letter helps to remind you that it is a user defined type. Also this allows you to use the same name for the variable because the variable should start with a lower case letter.

In your functions you use the variable name "sList". This works, but understand that when the function is called the scope changes, so the variable name "studentList" is no longer seen which allows you to use the same name in the function. I find this helpful to know what I am working with.

Another thing about functions is when you send an array to a function it degrades to a pointer. This is not generally a problem because you use the array name with s subscript and yo do not even notice.

I will end be saying start with section "A" followed by section "B" part "a" first. Write this code and post it. We can work on it from there.

I know that sometimes, or quite often, I change direction in what I talk about. This is because I see something and get off track just to get the thought down. I hope this is not to confusing for you.

Hope that helps,

Andy
Thank you very much for this detailed reply! I appreciate it, as it was very helpful.
Topic archived. No new replies allowed.