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:
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?
#include <iostream>
int from_user( constchar* 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.
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.
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;
}
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?