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.

user1745511
  • 67
  • 1
  • 5
  • 7
  • Hmmm, might be better to read a book, if your professor is so useless. Here's very good list, including books for beginners http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. A professor who doesn't like to explain things to students, who would have thought it. – john Oct 26 '12 at 20:45

2 Answers2

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.

Mark Stevens
  • 2,366
  • 14
  • 9
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.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • ... or object with an `operator[]` defined. – Seth Carnegie Oct 26 '12 at 20:37
  • Actually still not quite right, a POD type can have an `operator[]` defined. – Seth Carnegie Oct 26 '12 at 20:42
  • @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." – Nikos C. Oct 26 '12 at 20:48
  • 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". – Seth Carnegie Oct 26 '12 at 20:53
  • 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 :) – Nikos C. Oct 26 '12 at 21:09