Functions and Function Calling
Jun 17, 2009 at 1:04am UTC
Ok im trying to write a mini program where a function called check is used to display 3 arguments as is in my code so far. Im trying to work out how to display the values passed to the functions when it is called? Does this make sense?
So far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
using namespace std;
int check (int num1, double num2, double num3)
{
cout << "the numbers entered are: " << endl;
cout << num1;
cout << num2;
cout << num3;
return 0;
}
int main ()
{
int num1;
double num2;
double num3;
cout << "Please enter 3 numbers. " << endl;
cin >> "\n Please enter number: " >> num1;
cin >> num2;
cin >> num3;
int check (int num1, double num2, double num3);
system ("pause" );
return 0;
}
Jun 17, 2009 at 2:29am UTC
The main error in your program would be in line 26:
cin >> "\n Please enter number:" >> num1;
This should be
1 2
cout << "\n Please enter number:" ;
cin >> num1;
Also a more correct function type would be void, because we are using a function that does not need to return anything (If you do not understand this read on a bit in whatever tutorial you are in and you will see why)
1 2 3 4 5 6 7 8 9
void check (int num1, double num2, double num3)
{
cout << "the numbers entered are: " << endl;
cout << num1;
cout << num2;
cout << num3;
}
and the call to the function in main() would be:
void check (int num1, double num2, double num3);
Jun 17, 2009 at 6:29am UTC
Ok so i have revised the code and come up with:
(there are still errors can someone set me straight with this im using bloodshed dev c++)
THanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
using namespace std;
void check (int num1, double num2, double num3)
{
cout << "The 1st number entered was: " << num1 << endl;
cout << "The 2nd number entered was: " << num2 << endl;
cout << "The 3rd number entered was: " << num3 << endl;
return 0;
}
int main ()
{
cout << "\n Please enter 3 numbers. " << endl;
cout << "Please enter the 1st number: " << endl;
cin >> num1;
cout << "Please enter the 2nd number: " << endl;
cin >> num2;
cout << "Please enter the 3rd number: " << endl;
cin >> num3;
void check (int num1, double num2, double num3);
system ("pause" )
return 0;
)
Jun 17, 2009 at 6:33am UTC
i worked out the problem. I had "void check (xxxx)" where i needed a value returned so i made it "double check (xxxx)". Here is the fix:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// Pg 244 Ex 6.1 Qu 2.
#include <iostream>
using namespace std;
double check (int , double , double )
{
int num1;
double num2;
double num3;
cout << "The 1st number entered was: " << num1 << endl;
cout << "The 2nd number entered was: " << num2 << endl;
cout << "The 3rd number entered was: " << num3 << endl;
return 0;
}
int main ()
{
int num1;
double num2;
double num3;
cout << "\n Please enter 3 numbers. " << endl;
cout << "Please enter the 1st number: " << endl;
cin >> num1;
cout << "Please enter the 2nd number: " << endl;
cin >> num2;
cout << "Please enter the 3rd number: " << endl;
cin >> num3;
double check (int , double , double );
system ("pause" );
return 0;
}
Last edited on Jun 17, 2009 at 6:39am UTC
Jun 17, 2009 at 6:38am UTC
Now if you compile this, im having problems displaying the inputted numbers. Does this have to do with the way i've called 'check' function back in 'main'?
Jun 17, 2009 at 12:22pm UTC
Line 45 declares a function, it does not call it.
You call a function with actual parameters (values), not their types.
Jun 17, 2009 at 1:08pm UTC
Thanks alot for your help with this. I have finally worked it out. See line 38 below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
using namespace std;
double check (int num1, double num2, double num3)
{
cout << "The 1st number entered was: " << num1 << endl;
cout << "The 2nd number entered was: " << num2 << endl;
cout << "The 3rd number entered was: " << num3 << endl;
return 0;
}
int main ()
{
int num1;
double num2;
double num3;
cout << "lease enter 3 numbers. " << endl;
cout << "Please enter the 1st number: " << endl;
cin >> num1;
cout << "Please enter the 2nd number: " << endl;
cin >> num2;
cout << "Please enter the 3rd number: " << endl;
cin >> num3;
check (num1,num2,num3);
system ("pause" );
return 0;
}
Topic archived. No new replies allowed.