2011-03-11 47 views
0
#include <iostream> 
#include <stack> 
#include <string> 

using namespace std; 

int main() 
{ 
    string name; 
    double gpa; 
    double high = 0; 

    stack<string>names; 

    for (int i=0; i<7; i++) 
    { 
     cout << " Enter student's name and gpa " << endl; 
     cin >> gpa ; 
     cin >> name ; 
    } 

    { 
     if (gpa > high) 
     { 
      high = gpa; 
      names.push(name); 
     } 
     else if (gpa=high) 
     { 
      high = gpa; 
      names.push(name); 
     } 
    } 

    cout << "Highest gpa is"<< high << "Names with highest gpa "<< ??? <<< endl; 
    system("pause"); 

    return 0; 
} 

有这些名称和GPA到这些名称的列表,7是确切的。我有算法获得最高的GPA,并且我知道有一些while循环条件用于获取具有最高GPA的人员的姓名。如何显示与某个变量相关联的名称?...请说明此部分

从问题2人有最高的GPA ...我将如何显示这两个这些名称与最高GPA分开?我似乎无法得到那个可以将名称与GPA联系在一起的部分。

帮助将不胜感激。

编辑:有没有更容易的方法在代码中使用while循环内的某个循环?在我使用if (gpa < high)声明的地方,我是否错过了一些获得GPA最高的名称?

+0

您在'else if'中的条件已损坏。 – 2011-03-11 03:11:55

+0

你的意思是什么意思?这意味着如果gpas等于最高gpa,我想用该gpa显示名称。但是还有其他正确的代码吗? – Surya 2011-03-11 03:15:15

+0

错了。再读一遍。 – 2011-03-11 03:15:55

回答

-1

试试这个:

#include <iostream> 
#include <stack> 
#include <string> 
using namespace std; 
int main() 
{ 
    string name; 
    double gpa; 
    double high = 0; 
    stack<string>names; 
    for (int i=0;i<7;i++) { 
     cout << " Enter student's name and gpa " <<endl; 
     cin >> gpa; 
     cin >> name; 
     if (gpa > high) { 
      high = gpa; 
      names.clear(); 
      names.push(name); 
     } 
     else if (gpa == high) { 
      names.push(name); 
     } 
    } 
    cout << "Highest gpa is "<< high << " held by:" << endl; 
    for (stack<string>::const_iterator it(names.begin()); it != names.end(); ++it) { 
     cout << *it << endl; 
    } 
    system("pause"); 
    return 0; 
} 
+0

哇哇哇......最后有什么布莱恩?​​?? – Surya 2011-03-11 03:35:14

+0

是否有一些更简单的代码,即时通讯丢失...在if(gpa Surya 2011-03-11 03:37:32

+2

Matt:我会用'std :: copy(names.begin(),names.end(),std :: ostream_iterator (std :: cout,“\ n”))替换最后的'for'循环。 ;'。 – 2011-03-11 03:37:45

相关问题