2014-11-04 95 views
0

我用float函數很難用返回值。每次我嘗試輸入一個返回值時,我都會收到錯誤的未知值,或者您必須輸入一個返回值。我很迷茫。帶浮點函數的C++錯誤4716

這裏是我的程序:

#include <iostream> 
#include <iomanip> 
#include <string> 

using namespace std; 

string studFirstName; 
string studLastName; 
float studExam1; 
float studExam2; 
float studExam3; 
float studScore; 
float studAvg; 

void getStudName(string& studFirstName, string& studLastName); 
void getStudExams(float& studExam1, float& studExam2, float& studExam3); 
float calStudAvg(float studExam1, float studExam2, float studExam3); 
void displayGrade(string studFirstName, string studLastName, float studavg); 

int main() 
{ 
    cout << "Enter Student's First Name:"; 
    cin >> studFirstName; 
    cout << endl; 

    cout << "Enter Student's Last Name:"; 
    cin >> studLastName; 
    cout << endl; 
} 

void getStudExams(float& studExam1, float& studExam2, float& studExam3) 
{  
    cout << "Enter score for Exam 1:"; 
    cin >> studExam1; 
    cout << endl; 

    cout << "Enter score for Exam 2:"; 
    cin >> studExam2; 
    cout << endl; 

    cout << "Enter score for Exam 3:"; 
    cin >> studExam3; 
    cout << endl; 
} 

float calStudAvg(float studExam1, float studExam2, float studExam3) 
{ 
    float studScore = (studExam1 + studExam2 + studExam3); 
    float studAvg = (studScore/3); 
    return; 
} 

void displayGrade(string studFirstName, string studLastName, float studAvg) 
{  
    cout << "Student's First Name:"; 
    getline (cin, studFirstName); 
    cout << endl; 

    cout << "Student's Last Name:"; 
    getline (cin, studLastName); 
    cout << endl; 

    cout << left << "Student's Final Grade:" 
    << right << studAvg << endl; 

    if (studAvg >= 90) 
     cout << "Grade = 'A'" << endl; 
    else if (studAvg >= 80) 
     cout << "Grade = 'B'" << endl; 
    else if (studAvg >= 70) 
     cout << "Grade = 'C'" << endl; 
    else if (studAvg >= 60) 
     cout << "Grade = 'D'" << endl; 
    else 
     cout << "Grade = 'F'" << endl; 

    system("pause"); 
} 

,如果任何人有任何想法,我真的需要幫助!謝謝

+0

return; ???你要麼返回studScore或studAvg。 – Jagannath 2014-11-04 05:38:43

+0

我試了兩次,它給了我一個致命的錯誤。 – 2014-11-04 05:40:44

+0

您抱怨編譯錯誤。這解決了編譯錯誤。您尚未顯示您是如何調用該方法的。 – Jagannath 2014-11-04 05:45:28

回答

0
float calStudAvg(float studExam1, float studExam2, float studExam3) 

{ 
float studScore = (studExam1 + studExam2 + studExam3); 

float studAvg = (studScore/3); 

return; 
} 

簡單地把返回在你的功能塊不返回任何東西。你必須明確指定你希望該函數返回的值。 像你的情況似乎你想要做的事,如: -

return studAvg ; 
從空返

除了意味着編譯器將控制返回給調用者,這意味着,你甚至可以在功能上,其返回使用空的返回類型爲void 。

此外,從數學方面來說,確保您所採取的平均分數是用相同的數字計算出來的。否則,你必須使用加權平均。

0

我不知道怎麼行事問題是你在哪裏調用這些方法?無論如何,嘗試這個,這將不會顯示任何erros,也是你的代碼系統(「暫停」)它很慢。它依賴於平臺。這是不安全的。所以請使用任何方法

#include <iostream> 
#include <iomanip> 
#include <string> 

using namespace std; 

string studFirstName; 
string studLastName; 
float studExam1; 
float studExam2; 
float studExam3; 
float studScore; 
float studAvg; 

void getStudName(string& studFirstName, string& studLastName); 
void getStudExams(float& studExam1, float& studExam2, float& studExam3); 
float calStudAvg(float studExam1, float studExam2, float studExam3); 
void displayGrade(string studFirstName, string studLastName, float studavg); 

int main() 
{ 

cout << "Enter Student's First Name:"; 
cin >> studFirstName; 
cout << endl; 

cout << "Enter Student's Last Name:"; 
cin >> studLastName; 
cout << endl; 
} 

void getStudExams(float& studExam1, float& studExam2, float& studExam3) 

{ 

cout << "Enter score for Exam 1:"; 
cin >> studExam1; 
cout << endl; 

cout << "Enter score for Exam 2:"; 
cin >> studExam2; 
cout << endl; 

cout << "Enter score for Exam 3:"; 
cin >> studExam3; 
cout << endl; 
} 

float calStudAvg(float studExam1, float studExam2, float studExam3) 

{ 
float studScore = (studExam1 + studExam2 + studExam3); 

float studAvg = (studScore/3); 

return studAvg; 
} 

void displayGrade(string studFirstName, string studLastName, float studAvg) 

{ 
cout << "Student's First Name:"; 
getline (cin, studFirstName); 
cout << endl; 

cout << "Student's Last Name:"; 
getline (cin, studLastName); 
cout << endl; 

cout << left << "Student's Final Grade:" 
<< right << studAvg << endl; 

if (studAvg >= 90) 
    cout << "Grade = 'A'" << endl; 
else if (studAvg >= 80) 
    cout << "Grade = 'B'" << endl; 
else if (studAvg >= 70) 
    cout << "Grade = 'C'" << endl; 
else if (studAvg >= 60) 
    cout << "Grade = 'D'" << endl; 
else 
    cout << "Grade = 'F'" << endl; 

}