Public Group

# Integrating user input into multiple functions

This topic is 1842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello again all. I've been doing some more research into functions today and have come across a bit of a snag I haven't been able to just "lookup". Currently I have a very simple program that calculates the sum of two variables using two different functions:

#include <iostream>
using namespace std;

int addNum(int x, int y){
int sum = x + y;
return sum;
}

int main()
{
cout << addNum(4, 2);
return 0;
}


Instead of having x and y be fixed variables inside of the main function, I would like them to be based off of user input. For instance I could have something similar to below:

int main()
{
cout << addNum(x, y);
cout << "Pick your favorite number"; //this value would be stored in variable x
cin >> x;
cout << "Pick your second favorite number"; //this value would be stored in variable y
cin >> y;
return 0;
}


Unfortunately that doesn't seem to work. I would like to think I'm on the right track, but I haven't been able to find a good source for integrating user input into functions, if anyone has one or a few that I could reference and do some reading/research on that would be awesome. I could just be completely missing the point as well. Any help is greatly appreciated.

##### Share on other sites

I believe you mean to write

int main()
{
cout << "Pick your favorite number"; //this value would be stored in variable x
cin >> x;
cout << "Pick your second favorite number"; //this value would be stored in variable y
cin >> y;
cout << addNum(x, y);

return 0;
}


##### Share on other sites

Adding to Vortez' code...

int main()
{
int x;
int y;

cout << "Pick your favorite number"; //this value would be stored in variable x
cin >> x;
cout << "Pick your second favorite number"; //this value would be stored in variable y
cin >> y;
cout << addNum(x, y);

return 0;
}


Remember to also declare your x and y variables.

##### Share on other sites

Adding to Vortez' code...

int main()
{
int x;
int y;

cout << "Pick your favorite number"; //this value would be stored in variable x
cin >> x;
cout << "Pick your second favorite number"; //this value would be stored in variable y
cin >> y;
cout << addNum(x, y);

return 0;
}

Remember to also declare your x and y variables.

that was it, so I need to declare the variables in the main function, hmm, still learning this stuff I'll be looking into that. Thank you.

I have been going through some tutorials on this stuff for the past few days and some of it's a lot of fun, I'll be adding some other functions in this to play around with, Thank you again for the help.

Edited by Silent The Gray

##### Share on other sites

hat very well may be. I do also run into the issue of multiple functions, It keeps spitting out the message that the variables x and y haven't been declared in this scope. Which is a bit of dillemma if I'm thinking about this right, the variables won't have been defined, because I would like the user to define them. Should I believe that the issue is within that main function or is it an issue of the two functions working together, and how would I test/troubleshoot that sort of thing?

In this case, you are trying to use x and y inside main. To do this, main must know what kind of data it's dealing with, so you have to declare them in main as well.

Even though they have the same variable types (and names) as your addNum function, they are not the same variables.

Inside main, x and y will refer to the values given by the user, and inside addNum x and y will refer to parameter 1 and 2, respectively.

##### Share on other sites

that was it, so I need to declare the variables in the main function

You need to learn about scope. Where you declare a variable determines in which part of the code it is visible. When you declare an x and a y inside main, they are visible only inside main. The x and y in the parameter list of addNum are two completely different variables from the two inside main and they are visible only inside the addNum function. In both cases, the variables are declared in function scope

It's important to understand the concept of scope early on as it's very fundamental to software development. Function scope is only one of several kinds of scope. You can read more about it at Wikipedia.

##### Share on other sites

that was it, so I need to declare the variables in the main function

You need to learn about scope. Where you declare a variable determines in which part of the code it is visible. When you declare an x and a y inside main, they are visible only inside main. The x and y in the parameter list of addNum are two completely different variables from the two inside main and they are visible only inside the addNum function. In both cases, the variables are declared in function scope

It's important to understand the concept of scope early on as it's very fundamental to software development. Function scope is only one of several kinds of scope. You can read more about it at Wikipedia.

Ah, I see, thank you for the information, I'll be sure to read up on it. I've been seeing all the ways you can implement little features to your programs here and there, currently working on a small little text adventure which will include everything thus far. I haven't got it all planned out, but the actual designing/implementation of stuff seems to be very cool. Thanks again, every tidbit is helpful.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015573
×