Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 处理多个函数和参数_C++ - Fatal编程技术网

C++ 处理多个函数和参数

C++ 处理多个函数和参数,c++,C++,大家好 我对程序设计和C++很有新意。 我的程序有点问题。我希望函数culcAverageYearMark使用函数getMarks中的变量,但无论我做了什么尝试,它总是抛出任何错误,比如“在'int'之前预期的主表达式”。我已经尽了我所能尝试了一切,但没有成功 #include <iostream> using namespace std; void studentDetails() { string name, surName, schoolName; co

大家好

我对程序设计和C++很有新意。 我的程序有点问题。我希望函数culcAverageYearMark使用函数getMarks中的变量,但无论我做了什么尝试,它总是抛出任何错误,比如“在'int'之前预期的主表达式”。我已经尽了我所能尝试了一切,但没有成功

#include <iostream>

using namespace std;

 void studentDetails()
 {
    string name, surName, schoolName;
    cout << "Enter your name: \n";
    cin >> name;
    cout << "Enter your surname: \n";
    cin >> surName;
    cout << "Enter your school name: \n";
    cin >> schoolName;
}


void getMarks()
{
    int english, mathematics, lifeOrientation, history, computerLiteracy, geography;


    cout << "Enter your mark for English: \n";
    cin >> english;

     while (!(english >0 && english <= 100))
     {
         cout << "Invalid mark \n";
         cout << "Please enter your mark for English: \n";
         cin >> english;
     }

    cout << "Enter your mark for Mathematics: \n";
    cin >> mathematics;
    while (!(mathematics >0 && mathematics <= 100))
     {
         cout << "Invalid mark \n";
         cout << "Please enter your mark for Mathematics: \n";
         cin >> mathematics;
     }
    cout << "Enter your mark for Life Orientation: \n";
    cin >> lifeOrientation;
     while (!(lifeOrientation >0 && lifeOrientation <= 100))
     {
         cout << "Invalid mark \n";
         cout << "Please enter your mark for lifeOrientation: \n";
         cin >> lifeOrientation;
     }
    cout << "Enter your mark for History: \n";
    cin >> history;
     while (!(history >0 && history <= 100))
     {
         cout << "Invalid mark \n";
         cout << "Please enter your mark for History: \n";
         cin >> history;
     }
    cout << "Enter your mark for Computer Literacy: \n";
    cin >> computerLiteracy;
     while (!(computerLiteracy >0 && computerLiteracy <= 100))
     {
         cout << "Invalid mark \n";
         cout << "Please enter your mark for Computer Literacy: \n";
         cin >> computerLiteracy;
     }
    cout << "Enter your mark for Geography: \n";
    cin >> geography;
    while (!(geography >0 && geography <= 100))
     {
         cout << "Invalid mark \n";
         cout << "Please enter your mark for Geography: \n";
         cin >> geography;
     }

}

void calcAverageYearMark();

int main()
{
studentDetails();
getMarks();
calcAverageYearMark();



    return 0;
}

void calcAverageYearMark()
{
    getMarks(int english, int  mathematics, int lifeOrientation, int history, int computerLiteracy, int geography)

     float average = (english + mathematics + lifeOrientation + history + computerLiteracy + geography)/6;

}
#包括
使用名称空间std;
void studentDetails()
{
字符串名称、姓氏、学名;
姓名;
姓氏;
学校名称;
}
void getMarks()
{
int英语、数学、生活取向、历史、计算机素养、地理;
英语;
而(!(英语>0&&英语数学;
而(!(数学>0&&数学生活取向;
而(!(lifeOrientation>0&&lifeOrientation历史记录;
而(!(历史>0&&history;
而(!(计算机素养>0&&计算机素养地理;

而(!(地理>0&&geography你遇到的问题确实有两个不同的原因

首先,您的函数
get_marks()
有一个
void
返回类型。这意味着它在运行时不返回任何值。如果你想从中获取值,你需要给它一个返回类型。如果你需要,它需要返回一个数组引用或一个指针。如果你不知道它们是什么,那么你应该重新考虑如何构造它我们的代码使您不会从一个函数中获得多个值

另一个大问题是,您调用的
get_marks
不正确。您定义它的方式不带任何参数,因此您将遇到编译问题,因为您试图在它不需要任何参数的情况下传递参数。您首先也没有正确地传递参数。您不应该在此之前放置数据类型e.辩论

下面是一个简单的程序重写,应该可以得到您想要的结果:

#包括
使用名称空间std;
void studentDetails()
{
字符串名称、姓氏、学名;
姓名;
姓氏;
学校名称;
}
int getMark(常量字符串和类)
{
int英语、数学、生活取向、历史、计算机素养、地理;

cout 0&&val是这是一个范围问题

如果您想在一个函数中计算某个值,那么获取该值并将其发送到您想使用此代码示例的另一个函数

int calculationA()
{
  int a;
  //do calulation to a
  return a;
}

int calculateB(int b)
{
  //b was sent from the main method 
  //we can use it here
  return b;
}


int main()
{
 int x = calculationA();
 int output = calculationB(x);
 //output variable now contains the caclulations after going through both methods

 return 0;
}

如果不将变量设置为全局变量或将其作为参数发送,则无法访问该变量。您遇到了所谓的作用域错误

在定义时调用getMarks函数并传递参数,而不使用任何参数,并且在调用该变量时传递未声明的变量。强烈建议您可以再次阅读一本好的教科书,了解这些概念和更多内容。