1

I am trying to take a variable use it for a size declarator into an array. I am having trouble getting the data into the array. What will happen then is that I will divide the array into an average and return a char value. The only problem I have is with inputting data into the array. Thanks in advance here is the code.

#include <iostream>

using namespace std;

int average(int&);

char grade(int);

int main()
{
    int array = {};
    int numgrades;
    int total = 0;

    cout << "Enter the number of grades" << endl;
    cin >> numgrades;

    for (int i = 0; i < numgrades; i++) {
        cout << "Enter a numeric grade between 0 - 100" << endl;
        cin >> i;
        total += i;

    }

    int average = total / numgrades;


    char result = grade(average);


    cout << "The grade is " << result << "." << endl;

    return 0;
}

char grade(int avg)
{
    if (avg >= 90) {
        return 'A';
    }
    else if (avg >= 80 && avg < 90) {
        return 'B';
    }
    else if (avg >= 70 && avg < 80) {
        return 'C';
    }
    else if (avg >= 60 && avg < 70) {
        return 'D';
    }
    else
    {
        return 'F';
    }

}
1
  • 2
    You don't have an array... Consider reviewing how to declare and use an array. Also, think about what the variable i is and what cin >> i; does. Commented Jun 24, 2018 at 2:41

1 Answer 1

3

You have some critical problems in your code, this is what will do the work:

cin >> numgrades;

int *array = new int[numgrades] ;

for (int i = 0; i < numgrades; i++) {
    cout << "Enter a numeric grade between 0 - 100" << endl;
    cin >> array[i];
    total += array[i];

}

The important parts are creating the array dynamically, using the new statement and passing the wished size. Also your problem was in overriding i by the cin >> i line, instead you need to save the value to the array at i, like this cin >> array[i].

For more ways work with dynamic data and arrays in C++ I recommend you read about std::vector.

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

Comments

Your Answer

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