I want to set std::cin to the function value

I want to streamline my code by having the user's input be = to the value of the function's parameter without having to declare a larger scope variable. Something like this:

1
2
  std::cout<<"Enter value: ";
  std::cin>>function_example();


Obviously, that doesn't work and I know it doesn't work but I'm using it as an example to show you what I'm trying to do. I have declared a variable for the function's parameter when I declared the function, but I don't want that variable to be outside the scope of that particular function. Is this possible?
Would you be a little more clear on what the start/end goals are please? I am a bit confused. Are you trying to return a value from a function and output it? You lost me at assign a value to user input and function parameters when you declare a function but don't want any variables outside of the function scope, how can you do this without having a variable to pass (other than default initialization)?
The end goal is to ask the user to choose from 3 "classes" (RPG game). Once Chosen, I want that input (std::cin>>user_input) to be putting the value into the parameter for the function.

1
2
3
4
5
6
7
8
9
10

//function example 

void Class_selection(int user_selection)
{
//function output goes here
}




But it's called to action here:

1
2
std::cout<<"Enter an example value for me to place into the variable: int user_selection";
std::cin>>Class_selection();


So basically, instead of this:
1
2
3
4
5
int user_selection_function_passer;
std::cout<<"Enter an example value . . . ";
std::cin>>user_selection_function_passer;

Class_selection(user_selection_function_passer);


Now, instead of that. . . have it where the cin line automatically places the user's selection into the (user_selection) parameter of the function. Because I'm having to create another variable, pass that variable into the function's variable. Is there no way to have what the user types go there automatically?

But now I've confused myself, so . . . sigh.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int from_user( const char* prompt = "Enter an example value . . . " )
{
    std::cout << prompt ;
    int value ;
    std::cin >> value ;
    return value ;
}

void Class_selection( int user_selection = from_user() )
{
    // ...
}

int main()
{
    Class_selection() ;
}
I don't see why you don't want to create an extra 4-8 bytes :P Your alternative would be to get the input inside the function and don't use a parameter I suppose.
Thank you JL

Quick question . . . is there anyway to set a breaking point in a function where it will 'jump over' to run through a different function, and then when it's done with that 2nd function, jump back to the same spot in the 1st function that it left? Basically like a placeholder. So something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void function_examp1()
{
//do stuff
cout<<"blah blah";
//now I want to jump to another function, but have a spot to 'come back to':


another_function();
//placeholder

}

void another_function()
{
//do other stuff
//finish stuff
//now I want to complete this function and go back to first function's placeholder

}


Is that possible? Kinda' like a bookmark of sorts. . . like a function pause with the ability to go back to the function and skip down to where a placeholder is.
Last edited on
There is, it is labels and goto. But, if you are needing to use them you should probably rethink your design. Unless you are talking about simply calling a function inside of another function then yes, it will continue after where you called it. For example you can do something like
1
2
3
4
5
6
7
8
9
10
void function1()
{
    int a = function2();
    std::cout << a << std::endl;
}

int function2()
{
    return 10;
}
Last edited on
Well, the idea was to have a function that is accessible by multiple places, but that function has a simple use etc. for each time it's called, but used so much so that retyping would be too repetitive.

Since you suggest that's bad design, what other ways are there to achieve such a thing?
Giblit -- Thank you. That's exactly what I was looking for!

That's awesome!

So, if function2 was completely multi-use (from many other functions), can I get it to jump back to the function place it was at last?

You say goto but I'm thinkin less 'in function' and more of its own function to be used by everything everywhere.
> ljump back to the same spot in the 1st function that it left? Basically like a placeholder.

We don't have to do anything special. A function always returns to the point immediately after the call to the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

bool bar(int=0) ;

int foo()
{
    std::cout << "foo::label 1\n" ;

    bar() ;
    std::cout << "foo::label 2\n" ;

    std::cout << "foo::label 3\n" ;

    if( bar() )
    {
        std::cout << "foo::label 4\n" ;
        // ...
    }

    else
    {
        std::cout << "foo::label 5\n" ;
        // ...
    }

    bar() ;
    std::cout << "foo::label 6\n" ;
}

bool bar( int v )
{
    std::cout << "bar\n" ;
    return v ;
}

int main()
{
    foo() ;
}

http://rextester.com/VBUEU71479
1
2
3
4
5
6
7
8
9
void fun()
{
	cin >> //	
}

int main() {
	//cin >>
    fun();	
}
Oh wow! I didn't know that JL. Thank you so much. That's exactly what I was looking for.

Thanks to all of you.
Topic archived. No new replies allowed.