从其他函数C ++中获取函数中的变量

时间:2015-12-20 02:29:55

标签: c++ function

我正在尝试将变量从一个函数传递到另一个函数。我尝试过这种方法,但它对我不起作用:

int c (){
    int x1,x2,y2,y1;
    system("cls"); 
    cout<<"Insert Value"<<endl
    cin>>x1;

    return x1;
}

int cd()
{
     int a;
     a=c();
     cout<<"X1: "<<a;
}

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。

首先,在cout函数中的c()语句后,您缺少分号。

此外,您已指出函数cd()应返回int,但您没有返回任何内容。

最后,除非您明确调用它们,否则这些函数不会开始执行。

试试这个:

#include <iostream>

using namespace std;

int c (){
    int x1,x2,y2,y1;

    cout<<"Insert Value"<<endl;
    cin>>x1;

    return x1;
}

int cd(){
     int a;
     a=c();
     cout<<"X1: "<<a;
     return a;

}

int main()
{
    int x=cd(); //call the function to create the side effects

    return 0;
}