Jump to content

  • Log In with Google      Sign In   
  • Create Account


Integrating user input into multiple functions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Silent The Gray   Members   -  Reputation: 119

Like
0Likes
Like

Posted 04 January 2014 - 04:57 PM

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.

 



Sponsor:

#2 Vortez   Crossbones+   -  Reputation: 2688

Like
0Likes
Like

Posted 04 January 2014 - 05:00 PM

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


#3 Lactose!   GDNet+   -  Reputation: 2459

Like
3Likes
Like

Posted 04 January 2014 - 05:08 PM

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.



#4 Silent The Gray   Members   -  Reputation: 119

Like
0Likes
Like

Posted 04 January 2014 - 05:09 PM


 

 

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, 04 January 2014 - 05:14 PM.


#5 Lactose!   GDNet+   -  Reputation: 2459

Like
4Likes
Like

Posted 04 January 2014 - 05:13 PM

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.



#6 Aldacron   GDNet+   -  Reputation: 3056

Like
2Likes
Like

Posted 04 January 2014 - 11:08 PM

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.



#7 Silent The Gray   Members   -  Reputation: 119

Like
0Likes
Like

Posted 04 January 2014 - 11:38 PM

 

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS