0

I am a beginner at C++ am having a lot of trouble trying to make functions to execute each task. I think I could handle doing each task in the main function but I don't know how to break them into separate functions (eg: I don't know how to break reading of the .txt file and displaying it into separate functions.)

Also, for 'scores' I keep getting an error message that says "subscript needs array or pointer type" but I don't know what that means.

Note: I have not completely finished the program so the curve, displaycurve, and averagecurve do not have a function made yet, which I will do later.

//this program reads data from a .txt file, displays the scores, finds the average score, finds the highest score, and displays the curve

#include <iostream>
#include <fstream>

using namespace std;

//function prototypes

void readscores (int); // read exam scores into an array from examscores.txt
void displayscores (int); // display scores in row of four scores
double average (const double scores [], int); //??? calculate average score and display
double maxscore (const double[], int); // find max and display
double curve (const double []); //find the "curve" based on the highest scores
double displaycurve (const double []);// display curves in rows of four
double averagecurve (const double []); //calculate the average curved score and display

int main ()
{
    const int array_size = 30; //array size
    double scores[array_size];//array of 30 elements
    int count = 0;
    ifstream inputfile;


    //open file
    inputfile.open("ExamScores.txt");

    //read scores
    while (count < array_size && inputfile >> scores[count])
        count ++;
    //display scores
    cout << "The numbers are:";
    for (count = 0; count < array_size; count++)
        displayscores(scores[count]);

    //calculate the average
    cout << "The average is:";
    average (scores, array_size);

    //find the max score and display

    cout << "The maximum score is:";
    maxscore (scores, array_size);


    return 0;
}

void displayscores (int num)
{
    cout << num << " ";
    }

double average (const double scores, int array_size)
{double total = 0;
    double average;

    for (int count = 0; count < array_size; count ++)
total += scores[count];
    average = total /array_size;
}

double maxscore (const double scores, int array_size)
{double max;

max = scores [0];

for (int count = 1; count < array_size; count++)
{if (scores[count] > max)
max  = scores[count];
}
return max;
}

These are the numbers or scores in the .txt file:

67 64 83 81 72 75 85 81 56 88 71 80 90 58 78 74 84 64 72 69 78 87 84 72 83 68 62 88 70 75 

I apologize if my coding is completely wrong, I am still trying to wrap my head around the basic concepts as the professor doesn't like to explain what he is teaching and the semester is already halfway done.

1
  • Hmmm, might be better to read a book, if your professor is so useless. Here's very good list, including books for beginners stackoverflow.com/questions/388242/…. A professor who doesn't like to explain things to students, who would have thought it. Commented Oct 26, 2012 at 20:45

2 Answers 2

2

You've correctly specified the parameters in the forward declaration:

double average ( double scores[], int array_size);

But in the implementation you left off the "[]", so the function doesn't know it's getting an array.

Change:

double average ( double scores, int array_size)

To:

double average ( double scores[], int array_size)

And likewise on the other functions that are supposed to take an array parameter.

Sign up to request clarification or add additional context in comments.

Comments

0
const double scores

is a double variable. Yet you're trying to do:

max = scores[0];

The [0] is called a "subscript" and it doesn't have any meaning for a scalar type (like scores) if it's not an array or a pointer.

Your maxscrore() function should probably use the following signature instead:

double maxscore (const double scores[], int array_size)

Same goes for all other functions where you made the same mistake.

5 Comments

... or object with an operator[] defined.
Actually still not quite right, a POD type can have an operator[] defined.
@SethCarnegie Wow. Didn't know that. I just searched for an example, but can't find anything. When I try to define one, the compiler tells me that "overloaded 'operator[]' must have at least one parameter of class or enumeration type."
A POD type can be a class. For example, class C { void operator[](int) { } }; is a POD type with a operator[]. The definition of POD type is "a type that has no virtual functions or user-defined destructor, no base classes, no user-defined assignment operator, and contains only other POD types".
I was under the impression that a struct or class with non-static members isn't a POD. Turns out I was wrong. The non-static rule only applies to data members, not member functions. Thanks for clearing it up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.