GPA计算器/在循环外添加

时间:2010-09-06 03:27:46

标签: c++

问候,

我只是想在这里寻求一些帮助。这是提示:

出于研究目的,当地大学的招生官员想知道女性和男性学生在某些课程中的表现如何。用户输入要考虑的课程数量。每个课程的学生数据以GPA形式提供,后跟一个代表学生性别的字母代码:每行输入包含一个代表学生GPA的实数,后跟字母代码f(女生) ; m(男生)。 每门课程的学生人数(学生)未知,数字0.0后跟字母O表示特定课程的数据结束。

话虽如此,这是对c ++的介绍,因此;基本上不允许使用数组,字符串以及int,float,double和char之外的任何其他内容。在代码中,需要能够以任何顺序键入各种条目(男性条目后跟女性,反之亦然。)

我所遇到的问题是,在计划结束时,需要提供按照女性和男性排序的“综合学校平均分”的输出。我理解如何获得分配问题的总数,我似乎无法得到总和。任何时候我试图得到总和,第一个课程(第一次通过循环)的值不保留,所以我无法弄清楚我的生活如何做到这一点。任何提示或帮助将不胜感激。我知道代码很长,有点“野蛮”,所以请耐心等待。这是代码

//GPA calculator for Ghemri
//dealing with gpa range 0.0-4.0, set cap?
//try a do while loop

#include <iostream>



using namespace std;


int main(void)
{


 int size, counter;
 //int studentTotal= 0;
 char gender;
 double studentInfo,total,sum, avg;
 double minRange = 0.0, maxRange = 4.0;
 double maxGpa=0,gpaAvg,gpaSum;
 double femaleSum, femaleAvg, femaleTotal;
 double maleSum, maleAvg, maleTotal;
 int femaleNumber,maleNumber, gpaNumber;
 double sumFemaleAvg;// femaleGeneralAvg;//sumMaleAvg, maleGeneralAvg;


 cout << "\nPlease enter the number of courses you want considered: ";

 cin >> size;

 while(size <=0)
 {
  cout << "\nInvalid entry, number of course must be greater than zero\n";

  cin >> size;

 }

 //sumFemaleAvg+=femaleAvg;

 for(int course =1; course <= size; course++)
 {
  maleTotal = 0;
  femaleTotal=0;
  total = 0;
  femaleNumber = 0;
  maleNumber = 0;
  gpaNumber = 0;
  maxGpa= 0;
  gpaSum = 0;


  //double  doubleArray[course] = {femaleAvg};
   cout << "\nEnter student information(0.0 O to end):\t";

   cin >> studentInfo >> gender;

  while(studentInfo < minRange || studentInfo > maxRange)
  {
   cout << "\nInvalid entry, try again...\n";

   cout << "Enter student's information(0.0 O to end): \t";

   cin >> studentInfo >> gender;
  }
  if(studentInfo > maxGpa)
  {
   maxGpa=studentInfo;
  }
  if(studentInfo > 3.00)
  {
   gpaSum=studentInfo;
   gpaNumber=1;
  }

  if(gender == 'f' && studentInfo > minRange && studentInfo < maxRange)
  {
   femaleNumber=1;
   femaleSum = studentInfo;
   maleSum=0;

  }
  if(gender == 'm' && studentInfo > minRange && studentInfo < maxRange)
  {
   maleNumber=1;
   maleSum = studentInfo;
   femaleSum=0;
  }

  sum =studentInfo;
  counter = 0;
  counter++;


  while(studentInfo != 0.0 && gender != 'O')
  {

   cout << "Enter student information(0.0 O to end):\t";

   cin >> studentInfo >> gender;

   if(studentInfo > maxGpa)
   {
    maxGpa=studentInfo;
   }

   if(studentInfo < minRange || studentInfo > maxRange)
    {
     cout << "\nInvalid entry, try again...\n";

     cout << "Enter student's information(0.0 O to end): \t";

     cin >> studentInfo >> gender;
    }

   if(gender != 'm' && gender !='f'&& gender != 'O')
    {
     cout << "Invalid entry, enter m for male or f for female\n";

     cout << "Enter student's information(0.0 O to end): \t";

     cin >> studentInfo >> gender;
    }

    sum +=studentInfo;
    total+=counter;
    avg = sum/total;

    if(studentInfo > 3.00)
    {
     gpaSum+=studentInfo;
     gpaNumber++;
     gpaAvg= gpaSum/gpaNumber;
    }

    if(gender == 'f' || gender =='F')
    {
     femaleSum+=studentInfo;
     femaleNumber++;
     //femaleTotal+=femaleNumber;
     femaleAvg = femaleSum/femaleNumber;
     //sumFemaleAvg = femaleAvg;
    }

    if(gender == 'm' || gender == 'M')
    {
     maleSum+=studentInfo;
     maleNumber++;
     //maleTotal+=maleNumber;
     maleAvg = maleSum/maleNumber;
    }

   if(studentInfo == 0 && gender == 'O')
   {
    cout << "\nResults for course "<< course<<":\n";
    cout << "Female Student Average\t Male Student Average\n";
    cout << "\t";
    if(femaleNumber==0)
    {
     cout<< "N/A" << "\t\t\t";
    }
    else
    {
     cout<< femaleAvg <<"\t\t\t";//femaleAvg

    }
    if(maleNumber==0)
    {
     cout << "N/A\n";
    }
    else
    {
     cout<<maleAvg << endl;
     //sumMaleAvg = maleAvg;
    }

    cout << "\nHighest GPA: " << maxGpa<<endl;
    cout <<  "Highest average GPA for course "<< course << ": "<< gpaAvg<< endl;
   }
  }
  sumFemaleAvg = femaleAvg;

 }

 /*double genAvg[]={femaleAvg};

 result+=genAvg[course];*/

 sumFemaleAvg+=femaleAvg;

 cout<< "this is a test for the value sum " << sumFemaleAvg<<endl;
 //cout<< "this is another test " << result <<endl;

    //maleGeneralAvg = sumMaleAvg/course;
  /*cout << "the sum is " << sumFemaleAvg<<endl;
  cout << "the other sum is "<< sumFemaleAvg2<<endl;
  cout << "the other other sum is " << femaleAvg;*/

 return 0;
}

1 个答案:

答案 0 :(得分:2)

尽量避免极端重复并将常见操作纳入函数中。我现在会“忍受你”,但我真的没理由这么做。这是你作为程序员需要学习的第一件事。

看起来变量sumFemaleAvg应该在循环迭代中求和。但是,行sumFemaleAvg = femaleAvg;每次都会覆盖变量。做

  sumFemaleAvg += femaleAvg;

同样适用于您希望在多次迭代中累加的其他变量。