View Full Version : Funcition HELP
imported_ExOdUs
07-24-2003, 07:43 PM
can some one tell me how can i make a function pass a value from one to another function and have that function print out what the user typed in the previous function?
DNAunion2000
07-24-2003, 10:49 PM
/* DNAunion*/ Maybe I've misinterpreted you, but the following seems to do what you are asking about.
void getInputButDontPrintIt()
{
int age;
cout << "Enter your age: " ;
cin >> age;
printUserInput(age);
}
void printUserInput(int userInput)
{
cout << "User entered: " << userInput << endl;
}
imported_ExOdUs
07-25-2003, 12:25 AM
NO here is my code that i have
#include <iostream.h>
calc();
rec();
main()
{
cout <<"In this program i will try and make\n"
<<"A function do a task and pass that task\n"
<<"To another function and have that function\n"
<<"Print out that task on screen" << endl << endl;
calc();
rec();
return;
}
//******
void calc(int num)
{
do{
cout <<"please enter a number from 1-80?" << endl
<< endl;
cin >> num;
cout <<"Now I will try and pass your number to a \n"
<<"function named rec()" << endl << endl;
}while((num >= 1)||(num <= 80));
return;
}
//******
void rec(int num)
{
cout <<"Here is the number you entered" <<num<<endl;
return;
}
now i have to use the (adress of opereater (&)) or the pointer operator(*). And i read over the unit and functions over and over and over and nothing i tried works. Can some show me what to do or explain to me how to use address and reference to change functions in my created functions and main to, or point me to a website that does.
stuka
07-25-2003, 10:38 AM
I think what you're wanting to do (and I may be reading wrong) is to alter a number in a called function, then print out the ALTERED number in the calling function. This wouldn't necessarily require the address-of operator or the pointer operator in C++ (though it would require a reference argument). BTW, I don't see where you've actually PASSED the input to your second function - is this all of your code, or a work in progress?
jamessan
07-25-2003, 11:50 AM
First, a posting suggestion. Enclose any code you post between the [ code] and [ /code] tags (minus the spaces in the tags). This will maintain formatting and make it easier for other people to read.
Now, a few formatting suggestions/c++ corrections. You don't have to follow them, but they make your code cleaner and easier to understand.#include <iostream>
using namespace std;In C++, it is preferred to include without the .h and use namespaces.void calc();
void rec();I guess your compiler is letting you get away with not declaring the return type, since it is void. You should always declare return type and function parameters when declaring the function prototype.int main(void)
{
...
return 0;
}main should return an int. It gives some feedback as to how the program ran, i.e. you can have it return a different number if there is an error.
As for your question, the best solution, IMO, is to pass by reference. This (http://www.geocities.com/sstutor/valref3.htm) site gives a good example of what it means to pass by reference. I think this is what you were trying to do.
DNAunion2000
07-26-2003, 12:15 PM
/*DNAunion*/ I am not sure how the program is even compiling. The signatures of the function prototypes don't match the function headers of their definitions.
...
calc();
rec();
main()
{
...
}
void calc(int num)
{
...
}
void rec(int num)
{
...
}
In the prototypes, you inform the compiler that the functions have no parameters, but in the function definitions you have then accepting one integer each.
In addition, I don't think endl can be used in the program the way it is currently setup, which would also prevent a clean compile.
DNAunion2000
07-26-2003, 12:44 PM
/*DNAunion*/ I've tried to figure out what you are trying to accomplish and have modified your code. It should get you started.
#include <iostream> // don’t include the “.h”
// You have to give the program access to cin, cout, and endl.
// You can “bring these” and a whole lot more in by using:
// using namespace std;
// or you can “use” just the ones your program uses:
using std::cin;
using std::cout;
using std::end;
// calc() does not return a value so its return type should be void.
// But apparently, you want it to accept a single integer parameter.
// Actually, I think you want it to be a reference parameter.
void calc(int &num);
// rec() does not return a value so its return type should be void.
// You apparently want to pass a single int parameter
void rec(int num);
// main() should be defined to return an integer
int main()
{
cout <<"In this program i will try and make\n"
<<"A function do a task and pass that task\n"
<<"To another function and have that function\n"
<<"Print out that task on screen" << endl << endl;
// I assume you are wanting to ‘pass a variable’ between functions.
// So we need one to serve that purpose.
int num;
calc(num); // pass the variable into the function
rec(num); // pass the variable into the function
return 0; // main() should return an int (0 if all goes well)
}
//******
void calc(int &num) // change parameter to a reference
{
do{
cout <<"please enter a number from 1-80?" << endl
<< endl;
cin >> num;
// The message you displayed here should not be
// in the loop, so I moved it outside the loop.
// The following loop-terminating logic is reversed….apparently.
// What you apparently want to do is ensure that the number
// is between 1 and 80, and if it is, end the loop.
// I've changed the relational operators to achieve this
}while((num < 1) || (num > 80));
// Moved from having been inside the loop
cout <<"Now I will try and pass your number to a \n"
<<"function named rec()" << endl << endl;
return;
}
//******
void rec(int num)
{
cout <<"Here is the number you entered" <<num<<endl;
return;
}
imported_ExOdUs
08-01-2003, 04:55 PM
Hey guys i finally got it to work thanks for your help.Here is how my code looks
code:
____________________________________________________
#include<iostream>
using namespace std;
void getval(int &num);
artisks(int &num);
void main()
{
int num;
getval(num);
artisks(num);
return;
}
//**********
void getval(int &num)
{
cout <<"Please enter a number from 1-80" << endl;
cin >> num;
if((num <= 1) || (num <80))
{
cout <<"Thank you for entering your number" << endl;
}
else
{cout << "Please enter your number again\n";}
return;
}
//********
artisks(int &num)
{
cout <<"Here is the number you entered" << num
<< endl;
return;
}
___________________________________________
sorry if it looks kinda sloopy thanks again for all your help
DNAunion2000
08-02-2003, 12:20 AM
/*DNAunion*/ A couple more comments.
#include<iostream>
using namespace std;
void getval(int &num);
// Should be consistent: adding void here
void artisks(int &num);
void main()
{
int num;
getval(num);
artisks(num);
return;
}
//**********
void getval(int &num)
{
cout <<"Please enter a number from 1-80" << endl;
cin >> num;
//First test should be: (num >= 1)
if((num <= 1) || (num <80))
{
cout <<"Thank you for entering your number" << endl;
}
else
{
// The user doesn’t get another chance since there is no loop
cout << "Please enter your number again\n";
}
// This function will return the value the user entered, even if it does
// not fall within the desired range.
return;
}
//********
// Integer parameter num doesn’t need to be a reference here
// (but it really doesn’t hurt anything to have it as a reference)
void artisks(int &num)
{
cout <<"Here is the number you entered" << num << endl;
return;
}
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.