如何获得平均成绩并进行搜索?

时间:2017-11-11 20:34:58

标签: c++ arrays search

我已经在这个项目上工作了好几天了,但我把大部分工作都解决了,但是我遇到了三个问题。我试图从ifstream文件计算平均等级,程序成功读取它,但是当我将数组从一个函数传递到另一个函数时,它只显示0和F,因为我试图显示百分比和字母等级。

我的第二个问题是如何让数组只打印出已填充的元素。因此,如果我创建一个10的数组并且仅填充前三个元素,那么我怎么才能仅将前三个元素打印出来?

我的另一个问题是,当我尝试进行搜索时,即使我输入正确的名称,也会一直说找不到记录。该记录正在按姓氏搜索。

任何指针或帮助将不胜感激。谢谢!

我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;

void greeting(string[], string[], double[], double[], double[], int);
int getinfo(string[], string[], double[], double[], double[], int);
int calculateaverageandgrade(string[], string[], double[], double[], double [], int);
void sortandsearch(string[], string[], double[], double[], double[], int);
void goodbye(string[], string[], double[], double[], double[]);

int main() {

string fname[10] = { "" };
string lname[10] = { "" };
double grade[10] = { 0 };
double grade2[10] = { 0 };
double grade3[10] = { 0 };
const int arraysize = 10;


greeting(fname, lname, grade, grade2, grade3, arraysize);
getinfo(fname, lname, grade, grade2, grade3, arraysize);
calculateaverageandgrade(fname, lname, grade, grade2, grade3, arraysize);



system("pause");
return 0;
}


void greeting(string fname[], string lname[], double grade[], double grade2    [], double grade3[], int arraysize) {

int choice = int();
char cont = 'y';

while (cont == 'y' || cont == 'Y')
{

    cout << setw(60) << "Main Menu" << endl;
    cout << setw(66) << "======================" << endl;
    cout << "\t\t\t\tToday we will be calculating grades from a file" << endl << "\t\t\t\tthat you can provide for us using notepad." << endl <<  "\t\t\t\tBe sure to update your file every time you run the program" << endl;

    cout << endl;
    cout << setw(64) << "Reading file...." << endl;
    cout << setw(66) << "======================" << endl;
    cout << endl << endl << endl;
    cout << setw(65) << "1. Calculate Grades." << endl;
    cout << setw(63) << "2. Search Grades" << endl;
    cout << setw(68) << "2. Exit from the Program." << endl;

    cout << endl;

    cout << setw(70) << "Please Enter your Selection: ";
    cin >> choice;

    if (cin.fail())
    {
        cout << "Invalid selection" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
    }
    else
    {
        if (choice == 1)
        {
        calculateaverageandgrade(fname, lname, grade, grade2, grade3, arraysize);
        }
        else if (choice == 2) {
            sortandsearch(fname, lname, grade, grade2, grade3, arraysize);
        }
        else if (choice == 3)
        {
            goodbye(fname, lname, grade, grade2, grade3);
        }
        else
        {
            cout << "Invalid Selection!" << endl;
        }
    }
    cout << "\nDo you want to continue (Y/N)....";

    cin >> cont;
}

}





int getinfo(string fname[], string lname[],double grade[], double grade2[], double grade3[], int arraysize) {


system("cls");
ifstream in;
in.open("data.txt");

int i = 0;


while (!in.eof()) {

    in >> fname[i] >> lname[i] >> grade[i] >> grade2[i] >> grade3 [i];
    ++i;
}

arraysize = i;
return arraysize;


}

int calculateaverageandgrade(string fname[], string lname[], double grade[], double grade2[], double grade3[], int arraysize) {

system("cls");
const int i = 10;

double total[i];

for (int a = 0; a < arraysize; ++a) {

    cout << "Name:" << setw(20) << "Score: " << setw(20) << "Grade: " << endl;;
    cout << fname[a] << " " << lname[a];
    total[a] = grade[a] + grade2[a] + grade3[a];
    total[a] = total[a] / 3;
    cout << setw(20) << total[a] << endl;

    if (total[a] >= 90) {
        cout << setw(40) << "A" << endl;
    }
    else if (total[a] >= 80) {
        cout << setw(41) << "B" << endl;
    }
    else if(total[a] >= 70) {
        cout << setw(41) << "C" << endl;
    }
    else if (total[a] >= 60) {
        cout << setw(41) << "D" << endl;
    }
    else {
        cout << setw(41) << "F" << endl;
    }


}


system("pause");
return total[i];
}

void sortandsearch(string fname[], string lname[], double grade[], double grade2[], double grade3[], int arraysize) {

    system("cls");
    string nameSearch = string();
    int i = int();
    bool flag = false;
    char key = char();

    system("cls");
    cout << "\nEnter Name to be Searched: ";
    cin >> nameSearch;

    while (i<arraysize)
    {
        if (lname[i] == nameSearch)
        {
            cout << "Record Found --- ";
            cout << "Record # " << i + 1 << " contains " << nameSearch << "'s information." << endl;
            flag = true;
        }
        ++i;
    }
    if (flag == false)
    {
        cout << "\nRecord Not Found!" << endl;
    }
    cout << "\n\n\nPress any Key to Continue...";
    key = _getch();
}



void goodbye(string fname[], string lname[], double grade[], double grade2[], double grade3[]) {

system("cls");
cout << setw(70) << "Thanks for using my program!" << endl;
system("pause");
exit(1);

}

0 个答案:

没有答案