![](/img/trans.png)
[英]C++ C4716 Beginner error. Understand the error conecpt but not sure how to make the program work
[英]C++ error 4716 with float function
我在使用float函數的返回值時遇到困難。 每次嘗試輸入返回值時,都會不斷得到錯誤未知值,否則您必須輸入返回值。 我很失落。
這是我的程序:
#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");
}
如果有人有任何想法,我真的需要幫助! 謝謝
float calStudAvg(float studExam1, float studExam2, float studExam3)
{
float studScore = (studExam1 + studExam2 + studExam3);
float studAvg = (studScore/3);
return;
}
只需將return放入功能塊中就不會返回任何內容。 您必須明確指定希望該函數返回的值。 就像您的情況一樣,您似乎想要執行以下操作:-
return studAvg ;
除此之外,空返回值還指示編譯器將控制權返回給調用者,這意味着即使在返回類型為void的函數中也可以使用空返回。
另外,從數學角度來看,請確保您要計算的平均分數是從相同的數字中計算出來的。 否則,您必須使用加權平均。
我不確切地知道您在找什么,問題是您在哪里調用了這些方法?無論如何嘗試此操作不會顯示任何錯誤,而且您的代碼系統(“暫停”)很慢。 它取決於平台。 這是不安全的。 所以請使用其他方法
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.