-7

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

我有这个例子:

/* 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 上显示数据。提前致谢。

4

2 回答 2

2

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

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"

}
于 2012-12-02T15:43:23.690 回答
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(); 

}
于 2012-12-02T17:32:55.340 回答