0

Possible Duplicate:
C++: Easiest way to access main variable from function?

I need to get my variable "input" from my main function in a.cpp to another function named check in b.cpp. I looked into it on Google and this forum/thingy, and I found you could do it with global variables using extern, but that's it's also bad to use those and I couldn't find an answer to what an alternative is? How should I transfer the data in the variable to the other function without using globals?


Code of how I got arguments to work. (What I'm trying to do here is a console "manager" for solutions of project Euler which I can call to solve/view via input, I started working on the code 40 mins ago.)

main.cpp

#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;
}

prob.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

back.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;
}
Community
  • 1
  • 1
Markus
  • 19
  • 2
  • 4
    Function arguments. Look into it. – Andrei Tita Dec 31 '12 at 03:29
  • By "this forum/thingy", do you mean Stackoverflow? If so, what questions have you looked at. There are these: http://stackoverflow.com/questions/9668985/c-easiest-way-to-access-main-variable-from-function, http://stackoverflow.com/questions/11783435/how-to-access-variables-defined-and-declared-in-one-function-in-another-function, http://stackoverflow.com/questions/13594515/c-pass-variable-from-function-outside-main-into-function-call-inside-main, http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c. Yours seems to be a duplicate of the first one. – jogojapan Dec 31 '12 at 04:15
  • This is another closely related one: http://stackoverflow.com/questions/6371382/getting-access-to-another-functions-variables – jogojapan Dec 31 '12 at 04:16
  • At the time when I made the original post i had no clue function arguments existed, the code is from an edit where Alok helped me. – Markus Dec 31 '12 at 04:23
  • 1
    @Aerzee: Don't edit the Q in such a way that the answer becomes useless to anyone who comes across this Q by searching. The purpose of an answer is two fold.To help the OP solve his problem and to serve as a future reference to all those who face the same problem in future. – Alok Save Dec 31 '12 at 04:28

2 Answers2

3

By passing the data as an function argument.

For example:

int doSomething(int passedVar)
{
}

int main()
{
    int i = 10;
    doSomething(i);

    return 0;
}

Note that the function definition may reside even in a different cpp file. The main only needs to see the function declaration, and the linker shall link the function definition correctly.

Usually, one would add the function declaration in a header file and include the header file in main, while providing the function definition in another cpp file.


The code you show has number of problems:

  • You do not need to declare main in the header file.
  • Your function declaration and definition of check() do not match. Your header file says it takes no argument and you define a the function definition to take one argument. Obviously, they don't match. As they stand now they are two completely different functions.

As the compiler sees it you declared one function who's definition you never provided and you defined another function in the cpp file. Thus the function declared(one with no parameters) was never defined and hence the definition not found error.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I do arguments but i'm having problems in my header and my main function... The header gives me a compiler error of too many arguments, the main.cpp on the line where im calling the "dosomething" function an error of not defined. – Markus Dec 31 '12 at 03:52
  • @Aerzee: Post the code by editing the Q. The linking error is because you are not asking the linker to link all the source files. The compilation error is because you probably have a mismatch in function declaration and definition. – Alok Save Dec 31 '12 at 03:56
  • I got it to work, but I don't think I did it in the optimal way, but ill post the code in a sec. Code added – Markus Dec 31 '12 at 03:59
  • @Aerzee: Edited to answer your query. – Alok Save Dec 31 '12 at 04:10
  • Thank you, your solution worked, it took me a while to figure out to fix the header, my fix was int check(std::string cinput); in the header and int check(string cinput){} in the cpp. – Markus Dec 31 '12 at 04:21
  • @Aerzee: Your fix should work very well :) I did not explicitly state the fix because it is more important that you understand *why?* and then the fix is pretty obvious. Since you could figure out the fix yourself, tells me you understood the *why?* – Alok Save Dec 31 '12 at 04:25
0

Andrei Tita is absolutely correct. If you have a "value" in one module (e.g. "main()" in a.cpp), and you wish to use that value in a function (e.g. "foo()" in b.cpp) ... then just pass that value as a function argument!

As your programs become more sophisticated, you'll probably start using classes (instead of functions) .

paulsm4
  • 114,292
  • 17
  • 138
  • 190