显示函数值

Displaying Functions values?

本文关键字:函数 显示      更新时间:2023-10-16
#include <iostream>
using namespace std;
int hwGrades();
int testGrades();
int projectGrade();

int main(){
hwGrades();
testGrades();
projectGrade();
//I want the average of each function displayed here

}
int hwGrades(){
int hw1,hw2,hw3, hwAverage;
cout << "Enter the grades of your homework." << endl;
cout << "Homework 1: ";
cin >> hw1;
cout << "Homework 2: ";
cin >> hw2;
cout << "Homework 3: ";
cin >> hw3;
hwAverage = (hw1 + hw2 + hw3) / 3;
return hwAverage;
}
int testGrades(){
int test1, test2, test3, testAvgerage;
cout << "Enter the grades for the tests." << endl;
cout << "Test 1: "; 
cin >> test1;
cout << "Test 2: ";
cin >> test2;
cout << "Test 3: ";
cin >> test3;
testAvgerage = (test1 + test2 + test3)/3;
return testAvgerage;
}
int projectGrade(){
int project;
cout << "What did you get on your project: ";
cin >> project;
return project;
}

我很难理解这一点,在所有功能完成后,我如何显示用户输入的值?我试过参数,但我没有很好地掌握它。任何帮助都将不胜感激。我基本上理解其他情况,我只是不知道如何在函数完成后使用这些值。

如果你想打印用户输入的值,你需要在读取它们的函数中打印它们。

如果您引用从testGrades()等函数返回的计算值,您可以将返回值存储在变量中,执行类似的操作

int result = testGrades();
cout << result;

或者您可以直接输出函数的resturn值

cout << testGrades();