可能重复:
C++:从函数访问主变量的最简单方法?
我需要将我的变量“输入”从 a.cpp 中的主函数获取到另一个名为 check in b.cpp 的函数。我在谷歌和这个论坛/东西上调查了它,我发现你可以使用全局变量来做到这一点extern
,但使用这些也很糟糕,我找不到替代方案的答案?我应该如何在不使用全局变量的情况下将变量中的数据传输到其他函数?
我如何让论点起作用的代码。(我在这里尝试做的是一个控制台“管理器”,用于解决欧拉项目的解决方案,我可以通过输入调用它来解决/查看,我在 40 分钟前开始处理代码。)
主文件
#include <iostream>
#include <windows.h>
#include "prob.h"
using namespace std;
int check(string x);
int main()
{
string input = "empty";
clear();
cout << "Welcome to the Apeture Labs Project Euler Console! (ALPEC)" << endl << endl;
cout << "We remind you that ALPEC will never threaten to stab you" << endl;
cout << "and, in fact, cannot speak. In the event that ALPEC does speak, " << endl;
cout << "we urge you to disregard its advice." << endl << endl;
cin >> input;
cin.get();
check(input);
cout << input << endl;
cin.get();
return 0;
}
概率.h
#ifndef PROB_H_INCLUDED
#define PROB_H_INCLUDED
int main();
int clear();
int check();
int back();
int P1();
int P2();
int P3();
int P4();
#endif // PROB_H_INCLUDED
返回.cpp
#include <iostream>
#include <windows.h>
#include "prob.h"
using namespace std;
int clear()
{
system( "@echo off" );
system( "color 09" );
system( "cls" );
return 0;
}
int check( string x )
{
if( x == "help" );
if( x == "empty" )
{
cout << "And.... You didn't enter anything..." << endl << endl;
}
else
{
cout << "Do you have any clue what you are doing? " << endl << endl;
}
return 0;
}