-7

How do I send value from one function to another?

I have this example:

/* 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;
}

How do I do this? The tutorial I found from the net explained showing data on main. Thanks in advance.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
sg552
  • 1,521
  • 6
  • 32
  • 59
  • 4
    Please spend some time with a good C++ text book and familiarize yourself with the basics of programming and of C++. We have a nice [list of recommended books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Kerrek SB Dec 02 '12 at 15:42
  • I appreciate your suggestion. – sg552 Dec 02 '12 at 16:22

2 Answers2

2

You can pass objects to functions as arguments. Here, fnMainMenu takes a reference to a constant std::string object as parameter and prints it out to stdout:

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

Then fnLogin() can call the function and pass it any string it likes:

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

}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks for your reply. Unfortunately I have this error now: `error C2660: 'fnMainMenu' : function does not take 1 arguments`. Why is that? Help me please. Thank you. – sg552 Dec 02 '12 at 16:22
  • @sg552 the function declaration has to match the definition: `void fnMainMenu(const std::string&);`. You need to `#include ` too. – juanchopanza Dec 02 '12 at 16:53
  • I appreciate your reply. I include `#include ` and the same error is still there? Thank you and please help me. – sg552 Dec 02 '12 at 16:58
  • @sg552 it is hard to say without seeing the code. Can you put it in pastebin or something? – juanchopanza Dec 02 '12 at 17:01
  • I paste it here: http://pastebin.com/fZRPPrCE Thank you – sg552 Dec 02 '12 at 17:14
  • @sg552 have a look [here](http://ideone.com/4sn6q0). The signature of the function *declarations* and *definitions* must match. – juanchopanza Dec 02 '12 at 17:18
  • I'll guess I got confuse with what you mean by `declaration has to match the definition` I really appreciate your help. Thanks again. – sg552 Dec 02 '12 at 17:24
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(); 

}
danial weaber
  • 846
  • 2
  • 17
  • 37
  • I appreciate your reply. This one is easy to understand. Thank you. – sg552 Dec 02 '12 at 17:45
  • `fnMainMenu(data)` returns a pointer to the char array `data`. then define `void fnMainMenu(char *s)` with a argument of a char pointer. here the pointer `s` points to the char array `data`. – danial weaber Dec 02 '12 at 17:52