将值从一个函数传递到另一个函数

时间:2012-12-02 15:40:08

标签: c++

如何将值从一个函数发送到另一个函数?

我有这个例子:

/* Include files */
#include <iostream>
#include <string>
#include <limits>
#include <sqlca.h>
#include <sqlcpr.h>
#include <iomanip>
#include <conio.h> // ntuk password masking


/* Declaration of functions and constants used */
#include "Functions.h"

using namespace std;

void fnMainMenu();

void fnLogin()
{
char data[6] = "hello";
fnMainMenu(); // call Main Menu and I want to pass "hello"

}

void fnMainMenu()
{   
cout << "I want to display hello here?";
}

int main()
{   

    fnLogin();
    return 0;
}

我该怎么做?我从网上找到的教程解释了在main上显示数据。提前谢谢。

2 个答案:

答案 0 :(得分:2)

您可以将对象作为参数传递给函数。这里,fnMainMenu将对常量std::string对象的引用作为参数并将其打印到stdout:

void fnMainMenu(const std::string& msg)
{
  std::cout << msg << "\n";
}

然后fnLogin()可以调用该函数并将其传递给任何喜欢的字符串:

void fnLogin()
{
  std::string s = "hello";
  fnMainMenu(s); // call Main Menu and I want to pass "hello"

}

答案 1 :(得分:1)

#include <iostream>
using namespace std;

void fnMainMenu(char *s)
{
cout << s;
}


void fnLogin()
{
 char data[]="hellow";
fnMainMenu(data); // call Main Menu and I want to pass "hello"
}



int main(){
 fnLogin(); 

}