Archived

This topic is now archived and is closed to further replies.

germpest

function trouble, need alittle help understanding

Recommended Posts

my code is
  
#include <iostream>

int getage(int age);
int readage(int);

int age;

int getage(int age)
{
	std::cout << "Enter your age: \n";
	std::cin >> age;
	return age;
}

int readage(int)
{
	if (age >= 62)
	{
		std::cout << "\nYou are eligible for Social Security in the U.S.A.\n";
	}
	else
	{
		std::cout << "\nYou are not eligible for Social Security.\n";
	}
	return 0;
}

int main()
{
	getage(age);
	readage(age);
	return 0;
}  
You probably see my problem, I just can't remember exactness of functions and what to do with the variables inside them. When this is run it says that "you are not eligible..." no matter what age is entered. I'm thinking that the way I pass the variables through the functions is wrong. On a side note, what is the reason for using "std::" before each "cout, cin, etc..." instead of using "name space std;" at the top? Thanks for your help. [edited by - germpest on January 30, 2003 2:06:39 AM]

Share this post


Link to post
Share on other sites
quote:

int getage(int age)
{
std::cout << "Enter your age: \n";
std::cin >> age;
return age;
}



What this function is doing is getting the user to input an age into a copy of the parameter - not the parameter itself. You''ll need to pass in the parameter by reference so that it is modified and a copy of it is not.

int getage(int &age)
{
std::cout << "Enter your age: \n";
std::cin >> age;
return age;
}



Qui fut tout, et qui ne fut rien
Invader''s Realm

Share this post


Link to post
Share on other sites
when I do that I get an error

3-extra-1.cpp(30) : error C2668: ''getage'' : ambiguous call to overloaded function

I have no clue what''s going on

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You pass "age" as a parameter to getage, but you have made age a global variable, and you don''t have to pass them as a parameter. And that is the reason why your code doesn''t work, you confuse the parameter age in getage with the global age. Parameters are local variables which you define when you call a function with values as parameters.
So you made a global age, passed it as a parameter to getage, getage says: ok, so my local age=parameter age, although the gobal age has no value yet. Then you say what the value of the local age is with cin and you return it, although nothing is done with it. Then you call readage with the global age as parameter, which is 0 by default as you gave it no other value. Notice that 0 < 62...
Your code makes no sense at all, you don''t know what you are doing. So instead of going to a forum and asking what''s wrong with your code, actually learn more about C/C++.

Share this post


Link to post
Share on other sites
thanks for your help, your not very encouraging, I fixed the stupid code though. I came to the forum for help with understanding what I can't from the 6 stupid books I have on c++. I read and read, but it's complicated. I see you know what your talking about. I need people who know what they're talking about so that I can figure out what my mind can't do alone with paper. dosvidanya

[edited by - germpest on January 30, 2003 2:36:20 AM]

Share this post


Link to post
Share on other sites
In all fairness though, it is sometimes difficult to start out at first. As far as the problem goes, I think the problem you''re having, as the AP pointed out, is the "scope" of a variable. A variable declared within a function is lost when that function returns. A global variable, however, is not. The AP was right when he/she said that your program is getting confused between the variable ''age'' you passed to the function, and the global ''age''. For future reference, global variables are usually frowned upon, as they can be changed easily, which can lead to unforseen problems.

Anyway, hope that helps a little; good luck with your coding :D

Peon

Share this post


Link to post
Share on other sites
this was the code I changed it too, I didn''t really understand what you guys said, fully.

  
#include <iostream>

int getage();
int readage(int age);



int getage()
{
int age;
std::cout << "Enter your age: \n";
std::cin >> age;
readage(age);
return 0;
}

int readage(int age)
{
if (age >= 62)
{
std::cout << "\nYou are eligible for Social Security in the U.S.A.\n";
}
else
{
std::cout << "\nYou are not eligible for Social Security.\n";
}
return 0;
}

int main()
{
getage();
return 0;
}


I assume I did what you were talking about, thanks for your replys

Share this post


Link to post
Share on other sites
ok, even though you fixed your code i would like to point out some more errors i see that weren't pointed out.

First off, you will be told many times durring your programming career to NEVER use global variables (unless you absolutly NEED them [which is basicly never]). The reason for this is demonstrated by your begining example, it is too easy to get unexpected results because anything can change (or not chage) your data.

Second, your functions are all messed up! You should really spend some time learning the differences between passing parameters by value and by reference (although at your stage you probably didn't get deep into the reference and pointer stuff yet). Also you need to learn the differences between function 'types'. You have set all your functions return types as int's. With the previous fixes to your code (passing the value as reference) you should make your functions 'void', or else have them return int's but call them with an assignment statement... age = getAge();

I will give you 2 examples of your code (but fixed). the first shows the original problem without using references, and the other sample will use references. Note that neither example uses global variables...

No references...

      
#include <iostream>

int getage();
void readage(int);

int getage()
{
int tempAge; //used to receive age from cin


std::cout << "Enter your age: \n";
std::cin >> tempAge;

return tempAge; //send the integer back to be used where the function is called

}

void readage(int theAge)
{
if (theAge >= 62)
{
std::cout << "\nYou are eligible for Social Security in the U.S.A.\n";
}
else
{
std::cout << "\nYou are not eligible for Social Security.\n";
}

return; //not really needed for void functions but it looks nice

}

void main()
{
int age;
age = getage();
readage(age);
return;
}


using pass by refrence...

        
#include <iostream>

void getage(int&);
void readage(int);

void getage(int &theAge)
{
std::cout << "Enter your age: \n";
std::cin >> theAge;
}

void readage(int theAge)
{
if (theAge >= 62)
{
std::cout << "\nYou are eligible for Social Security in the U.S.A.\n";
}
else
{
std::cout << "\nYou are not eligible for Social Security.\n";
}

return; //not really needed for void functions but it looks nice

}

void main()
{
int age;
getage(age);
readage(age);
return;
}


ps. You should expect negative reponses from some people in this forum if you ask 'dumb' beginner questions. Most of the people here are quite experienced and get tired of hearing questions like "How to i make a rpg that is massivly multiplayer all by myself", or "Which programming language is the best". If you stick with c/c++, once you get a good grasp of the language you will realize that the type problem you just had is due to your lack of know-how which can only be solved by reading and studying and practicing the material in you book(s). You did say yourself that you "can't remember exactness of functions and what to do with the variables inside them". Well you isolated the problem so use the index to look up fuctions and variables Note: I am not trying to be mean, just giving you a heads up for future incidents.


[edited by - IllMind on January 30, 2003 3:26:58 AM]

[edited by - IllMind on January 30, 2003 3:29:26 AM]

Share this post


Link to post
Share on other sites
Thank you IllMind, your first code example cleared things up, and reminded me of the different types of functions I should be using. Also, by your tone I knew you weren't trying to be mean. Back to the book.

oh, did anyone understand my question about using "std::" infront of cout, cin, etc... instead of just putting "using namespace std;" at the top? is there anything I should know to derail me from it's use? thanks again

[edited by - germpest on January 30, 2003 3:42:50 AM]

Share this post


Link to post
Share on other sites
IllMind, maybe you are experienced, but do you realise that this is the begginners forum? Usually that means that begginners ask dumb questions because..well.....they are begginners!

Think before you act.

BTW, im a begginner, and i have learnt 8 days of ''learn C++ in 21 days'' in 2 hours, and i know most of it THOROUGHLY.

I think the reason for this is because i learnt Qbasic a while back, and i dont need to spend time learning new definitions like ''variables''.

So my suggestion Germpest, is to learn something like Qbasic, which is the simplist way to learn programming, then re-read C++, and you will have an understanding of what these ''pros'' are talking about.

It sodent matter how complicated the language is, if you know the definitions you''ll run a breeze with it.

Thats my 2 cents.

If God made us, who Made God?
If you cant make it, let it make you.
"I play violent Computer games, I could snap at any moment!"

Share this post


Link to post
Share on other sites
In reference to the std:: vs using namespace std;
with the using namespace std, you are telling that compiler to look in the std namespace for functions. By using std:: you are stated to look in the std namespace for this specific function.

BoyWunder

Share this post


Link to post
Share on other sites
You could have also put the ''std::'' lines after your include statement, then just used cin and cout. Like this:

#include <iostream>

using std::cin;
using std::cout;

int main()
int age;
{

cin >> "Enter your age: " >> age;
cout << "\nYour age is: " << age;
return 0;

}

Share this post


Link to post
Share on other sites
awesome, I didn''t know you could use
using std::cin & using std::cout

that''s cool.

could you do it like this?

using std::cin, std::cout;

Share this post


Link to post
Share on other sites
quote:
Original post by germpest
could you do it like this?
using std::cin, std::cout;


Try it. If it doesn''t compile, you can''t do it. If it does compile, you can.

I don''t like "can I do this?" questions very much, because it''s often simply a matter of making a test application, which you can usually do in a few minutes.

I''ll tell you anyway though, just cause I''m in that kind of mood: No. Don''t believe me? Try it

Share this post


Link to post
Share on other sites