简体   繁体   中英

How do I get an array to compare its value to a double?

Help! sorry but my instructor simply won't help me, I've emailed her several times and asked her in person but she won't help me..

The point of this program is to use 3 arrays to store the first name, last name, and a grade average. I've put this in a loop to work with five students. At the end of the program I am supposed to call a function that takes the grade average, and turns it into a letter grade (A, B, C, D, F) however I just don't understand the syntax I guess. You can't compare the value of an array to the value of a double, so how is this possible to do?

Note: I have to use 3 arrays and a function to return a letter grade, it is required or she will fail me. This is also my first post ever on here so I'm sorry if I messed this up in some way. Below is my code.

// Zachary Law      Array.cpp

#include <iostream>
using namespace std;
#include <string>
#include <iomanip>

char function1(double Favg, int i);

int main()
{   

    int x, i;
    string Fname[10];
    string Lname[10];
    double Favg[10];
    i=0;
    x=1;

    cout << "We will be working with five students." << endl << endl;
    while (x<=5)
    {
        cout << "Enter first name of student " << x << ": " << endl;
        cin >> Fname[i];

        cout << "Enter last name of student " << x << ": " << endl;
        cin >> Lname[i];

        cout << "Enter grade average of student " << x << ": " << endl;
        cin >> Favg[i];
        i++;
        x++;
    }

    x=0;
    cout << "Name:" << setw(20) << "Final Grade";
    i=5;

    while (x<=5)
    {
        cout << Lname[i] << ", " << Fname[i] << setw(15) << function1(Favg, i);
        cout << endl;
        i--;
        x++;
    }
    return 0;
}

char function1(double Favg, int i)
{
    if (Favg >= 90.00)
        return 'A';
    else if (Favg >= 80.00)
        return 'B';
    else if (Favg >= 70.00)
        return 'C';
    else if (Favg >= 60.00)
        return 'D';
    else if (Favg < 60.00)
        return 'F';
}​

应该为数组Favg分配一个初始值,或者可以将其作为函数参数传递。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM