显示函数值?

时间:2018-07-23 05:08:47

标签: c++ function

#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;
}

完成所有功能后,我很难理解这一点,如何显示用户输入的值?我尝试过参数,但是我对此不太了解。任何帮助,将不胜感激。我最了解的是,在函数完成后,我只是不知道如何使用这些值。

1 个答案:

答案 0 :(得分:1)

如果要打印用户输入的值,则在阅读它们时需要在功能中打印它们。

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

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

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

cout << testGrades();