samedi 26 janvier 2019

How do I make my program pass data to my array correctly? (Homework)

I've set my array size to 20 (I set it to 19 assuming it's counting 0). I set my for loop to only run so long as gradeCount <= to gradeCounted yet it will keep running no matter how many times I enter data. If I enter 3 grades without pressing enter between each one, such as "23 23 23" it will return "Enter Grade" 3 times in a row, rather, for as many grades as I enter, separated by spaces. I don't understand why it's not passing data into the array and ending the for loop properly. I'm sure my code is an ugly mess, sorry.

Also, when entering code into stackoverflow, it said to indent the code 4 spaces to format? I couldn't initially indent the code with the code button and there was no {} button either. What am I missing? It was only after a notification to fix it that I was able to. Thanks for your time, I don't want to be a pain in the ass for you guys.

//This program asks user how many grades there are, inputs grades, and  displays median of said grades.

#include <iostream>
using namespace std;

//Variables
////////////////////const int limitGrades = 20; //Array "boxes"? //Ignore this //for now.
int gradeCounted; //Number of grades from user.
const int SIZE = 19;

//Array
float grades[19]; //Max grades that can be entered.

//Functions
void gradeTaker()
{
    cout << "You may input up to 20 grades. \n";
    cout << "First enter the number of grades you have: \n";
    cin >> gradeCounted;
    //requests how many grades there are and stores them in array
    for (int gradeCount = 0; gradeCount <= gradeCounted + 1; gradeCount++)
    {
        for (float &grade : grades)
        {
            cout << "Enter grade: \n";
            cin >> grade;
        }
    }
};

int main()
{
    gradeTaker();

    cout << "grades so far";
    for (int grade : grades)
        cout << grade << endl;

    system("pause");
}

Aucun commentaire:

Enregistrer un commentaire