Make it cheap and easy

Sorry for the bad title, I didn't know what to name my topic.

Anyway... I want to know if it's possible (and how it's possible) to send an input to a function without declaring it in a temporary variable?
Messy question... let's make an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

void myFunc(char input)
{
    // Do something...
}

int main()
{
    char input;    // I want to get rid of this temporary variable
    cin >> input;  // and send wathever input directly to my function.

    myFunc(input);
}


example:
1
2
3
4
int main()
{
    myfunc(input = cin/getline/whatever); // This is just an example
}


If you need more information about what I want to accomplish, please let me know and I will try to clarify my question even more.

Thanks in advance
Last edited on
I don't believe so. The process of declaring variables serves as a way to make sure that memory usage does not overlap.
I don't think you can (without cheating and creating a helper function, which might be good for you though ):

1
2
3
4
5
6
7
8
9
char getinput() {
    char input;
    std::cin >> input;
    return input;
}

//....

myFunc(getinput());
But it is possible to use a combined function? Like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
float myFunc1(float value1, float value2)
{
    return value1 * value2;
}

float myFunc2(float value)
{
    cout << "The value is " << value;
}

int main()
{
    myFunc1 ( myFunc2 ( 5.5 , 2.5 ) );
}


Am I correct? I would love to be able to short down my input variable in a simular way...

But if it's not possible, then there's nothing to do
Last edited on
Why don't you try it?
I'm sorry, I did not see your answer until after i posted my reply post.

Thank you, I will test your 'cheat' function ;)
closed account (o3hC5Di1)
I don't think you can, because of the typing of the variables.
cin << needs to know what kind of variable it is extracting the data to, so it knows how much memory is available for it.

Possibly you can define a template function object and overload the << operator in that, but that would take more coding than you would just write the simple variable declaration probably.

As for you're 2nd post:

1
2
3
4
int main()
{
    myFunc2 ( 5.5*2.5 );  //this does the same
}


It's not about how short your code is - it's about how optimal it works the memory (and cpu) and how readable it is.

All the best,
NwN
Last edited on
@NwN
Absolutely, your example does exactly the same as mine. And it's 'cheaper'. But the question was if it possible to combine the functions as they are, not it's content.
I guess it's possible, since you didn't tell me otherwhise?

The conclusion brings me another question... Is the memory needed for the temporary variable for the cin >> data discharged after it's been used, or do I need to put an extra line of code to restore the used memory?

I hope I'm not confusing anyone, I'm a bit confused myself =)
Last edited on
closed account (o3hC5Di1)
The memory for char input; is released at the end of the function.
In this case it's main() - so at the end of the program.

To my knowledge, you can only free dynamically allocated memory:
http://cplusplus.com/doc/tutorial/dynamic/

Yes, combining the functions as such is possible, the inner function will be evaluated first and its return value will then be used by the outer function.

Hope that helps.

All the best,
NwN
closed account (zb0S216C)
You cannot remove any variable/object from the stack whenever you wish, because that'll violate the concept of the stack. To overcome this, programmers create an anonymous scope to discard temporary variables/objects. In a way, it's controlling the lifetime of a variable/object.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    {
        // We're inside the anonymous scope.
        char temp(0);
        std::cin >> temp;
        myFunc(temp);
    }

    // 'temp' is no longer on the stack; it's non-existent.
}

Wazzak
Last edited on
As to the OP's first question, you can overload the function, such that it has a parameter-less version:


1
2
3
4
5
6
7
8
9
10
11
12
13

myfunc(char x)
{
        ...
};

myfunc()
{
        char x;
        cin >> x;
        ....
};


x will go out of scope as soon as myfunc() exits. But, if anything, I'd say this makes the code more expensive...
Last edited on
closed account (o3hC5Di1)
@Framework - Thanks for that! That's a nifty little trick :D

All the best,
NwN
@NwN
Thank you for the Dynamic Memory tutorial. I had no knowlege of this but now I do =)

@Framework, wohtp
Thanks a lot!

Now I have got a lot of new ways to approach the issue, but it's hard knowing what's the best solution and what to use.

Everyone seems to have their own favorite way of handling the issue. Guess I will try to combine them =)
Last edited on
All you need is an input function that will return the entered value. Standard C++ stream input functions usually return a reference to a stream. However some C functions as for example getchar return the entered value. So you can write


1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>

void myFunc(char input)
{
    // Do something...
}

int main()
{
    myFunc( std::getchar() );
}  
Last edited on
@vlad from moscow
Thank you! That's what I was looking for.
By the way you could use a C++ feature.

myFunc( std::cin.get() );
Last edited on
By the way you could use a C++ feature.
myFunc( std::cin.get() );


In what way is this different the first example I posted? Everyone told me this was inoperable...
myfunc(input = cin/getline/whatever); // This is just an example

Thank you vlad! You rock =)
Last edited on
Topic archived. No new replies allowed.