|
From: Santiago P. C. <San...@yd...> - 2002-09-18 11:08:06
|
Hi John,
Please take a closer look to:
- function calling mechanism and return Values,
- variables scopes,
- passing variables by reference or by value,
In your code you are...
1st) ...Passing variables by value _NOT_BY_REFERENCE_ so you cannot modif=
y
its value inside the function you are passing the variable to.
=09int FirstNumber(int NumberOne)
=09{
=09 cout << "Enter how much money you had this morning $";
=09 cin >> NumberOne; // <=3D this NumberOne is NOT
the same as main's NumberOne
=09}
2nd) ..."Mixing" the way a variable is defined and the way that variable'=
s
content is handled.
=09int ThirdNumber(int NumberThree)
=09{
=09 // NumberThree =3D int NumberOne - int NumberTwo; // <=3D WRONG
STATEMENT
=09 // you are "trying to define" 'NumberOne' and
'NumberTwo' erroneously and in a local scope,
=09 // please have in mind that you are not referencing
main's 'NumberOne' and 'NumberTwo'
=09 cout << "You have spent $" << NumberThree << " dollars today " <<
endl;
=09}
3rd) ...defining functions to return an int value but not returning it:
=09int FirstNumber(int NumberOne)
=09{
=09 cout << "Enter how much money you had this morning $";
=09 cin >> NumberOne; // <=3D this NumberOne is NOT
the same as main's NumberOne
=09 // There must be a return statement here!!!!
=09}
4th) ..."thinking" that you are calling functions while you're NOT doing
that...
// Main Function
int main(int argc, char *argv[])
{
=20 int NumberOne;
=20 int NumberTwo;
=20 int NumberThree;
=09// Here you are "trying to REdefine" the functions, NOT calling
them
=20 int FirstNumber(int NumberOne); =09
=20 int SecondNumber(int NumberTwo);
=20 int ThirdNumber(int NumberThree);
=20 system("PAUSE");
=20 return 0;
}
There are also some "design issues" that I think you should take a look a=
t;
so I've modified your code the way I think it should look like (I've
modified some names in order to clarify the code ;oD):
#include <iostream>
#include <string>
/*
This program in designed to take input from the user,
in the form of a number and compute the output.
The program is then to display the output back to the user.
*/
// Function Definitions
int GetFirstNumber(); =20
int GetSecondNumber(); =20
void ShowThirdNumber(int iNumberOne, int iNumberTwo);
// GetFirstNumber Function
// The function gets an input from the user and returns it
int GetFirstNumber()
{
=20 int iLocalNumberOne =3D 0;
=20 cout << "Enter how much money you had this morning:" << endl ;
=20 cin >> iLocalNumberOne;
=20 return iLocalNumberOne;
}
// GetSecondNumber Function
// The function gets an input from the user and returns it
int GetSecondNumber()
{
=20 int iLocalNumberTwo =3D 0;
=20 cout << "Enter how much money you have now:" << endl;
=20 cin >> iLocalNumberTwo;
=20return iLocalNumberTwo;
}
// ShowThirdNumber Function
void ShowThirdNumber(int iNumberOne, int iNumberTwo)
{
=20 int iLocalNumberThree =3D iNumberOne - iNumberTwo;
=20 cout << "You have spent " << iLocalNumberThree << " dollars today " <=
<
endl;
}
// Main Function
int main(int argc, char *argv[])
{
=09// Retrieve first Number by calling the appropriate function and
=09// store it at int variable called 'iTheFirstNumber'
=09int iTheFirstNumber =3D GetFirstNumber();
=09// Retrieve second Number by calling the appropriate function and
=09// store it at int variable called 'iTheSecondNumber'
=09int iTheSecondNumber =3D GetSecondNumber();
=09// Call the apropriate function with the previously acquired values
=09// in order to print the result to the screen
=09ShowThirdNumber(iTheFirstNumber, iTheSecondNumber);
=20 system("PAUSE");
=20 return 0;
}
Hope this helps!!!
Regards, Santy
-----Original Message-----
From: John Ellison [mailto:j_...@ip...]=20
Sent: mi=E9rcoles, 18 de septiembre de 2002 12:00
To: dev...@li...
Subject: [Dev-C++] Newbie Needs Help
Gudday All,
=20 I have a small problem which I cant seem to find the answer to. I w=
as
hoping that someone could help. I am just beginning to learn c++, so the
code is not to complex. My problem lies in the compilation process. I am
unsure as to how to fix the compilation error.
Thanks to everybody.
Have a good one.
John
Here is the compilation error.
32 six.cpp parse error before `-'
And here is my source code :-
#include <stdio.h>
#include <iostream>
#include <string>
/*
This program in designed to take input from the user,
in the form of a number and compute the output.
The program is then to display the output back to the user.
*/
int FirstNumber(int NumberOne); // Function Definitions
int SecondNumber(int NumberTwo); // Function Definitions
int ThirdNumber(int NumberThree); // Function Definitions
// FirstNumber Function
int FirstNumber(int NumberOne)
{
=20 cout << "Enter how much money you had this morning $";
=20 cin >> NumberOne;
}
// SecondNumber Function
int SecondNumber(int NumberTwo)
{
=20 cout << "Enter how much money you have now $";
=20 cin >> NumberTwo;
}
// ThirdNumber Function
int ThirdNumber(int NumberThree)
{
=20 NumberThree =3D int NumberOne - int NumberTwo;
=20 cout << "You have spent $" << NumberThree << " dollars today " << end=
l;
}
// Main Function
int main(int argc, char *argv[])
{
=20 int NumberOne;
=20 int NumberTwo;
=20 int NumberThree;
=20 int FirstNumber(int NumberOne);
=20 int SecondNumber(int NumberTwo);
=20 int ThirdNumber(int NumberThree);
=20 system("PAUSE");
=20 return 0;
}
=20
-------------------------------------------------------------------------=
-------------
This message and any files transmitted with it are confidential and inten=
ded solely=20
for the use of the individual or entity to whom they are addressed. No co=
nfidentiality=20
or privilege is waived or lost by any wrong transmission.=20
If you have received this message in error, please immediately destroy it=
=20and kindly=20
notify the sender by reply email.
You must not, directly or indirectly, use, disclose, distribute, print, o=
r copy any=20
part of this message if you are not the intended recipient. Opinions, con=
clusions and=20
other information in this message that do not relate to the official busi=
ness of=20
Ydilo Advanced Voice Solutions, S.A. shall be understood as neither given=
=20nor endorsed by it.=20
-------------------------------------------------------------------------=
-------------
|