Newbie question - coding standards - how should I write this | Newbie | | Join Date: Jul 2007
Posts: 13
| | |
I am trying to write to a program that will compute the current age of the one executing the program. It is done by asking 2 questions:
- What is the current year
- What is your birth year
After these have been entered by the user the current age of the user needs to be computed.
I accomplished it because it is not all that difficult. However I want to do it according to standards and I would like to have the function prototype above main and the actual function definition below main. I am however not able to get it to work.
This is what I have now but if I do it like this the 'cout part' is being displayed but only the text 'this is your age now'. Judging by this I guess something is wrong with the part on the next line: cout << x (a, b) << endl;
Does anybody have any suggestions?
This is what I have now:
#include <vcl.h> //needed for Borland
#include <iostream> //needed for input output
using namespace std;
//---------------------------------------------------------------------------
int x (int, int); /* function declaration
or prototype */
int main()
{
int a; //declaration variables
int b; //declaration variables
cout << "Type current year here: ";
cin >> a;
cout << "Type year of birth: ";
cin >> b;
cout << "This is your age now: ";
cout << x (a, b) << endl;
return 0;
}
int x (int a, int b) // function definition
{
int x = (a - b);
int d;
cin >> d;
return x;
}
|  | Expert | | Join Date: Dec 2006
Posts: 782
| | | re: Newbie question - coding standards - how should I write this Quote:
Originally Posted by Break2 I am trying to write to a program that will compute the current age of the one executing the program. It is done by asking 2 questions:
- What is the current year
- What is your birth year
After these have been entered by the user the current age of the user needs to be computed.
I accomplished it because it is not all that difficult. However I want to do it according to standards and I would like to have the function prototype above main and the actual function definition below main. I am however not able to get it to work.
This is what I have now but if I do it like this the 'cout part' is being displayed but only the text 'this is your age now'. Judging by this I guess something is wrong with the part on the next line: cout << x (a, b) << endl;
Does anybody have any suggestions?
This is what I have now:
#include <vcl.h> //needed for Borland
#include <iostream> //needed for input output
using namespace std;
//---------------------------------------------------------------------------
int x (int, int); /* function declaration
or prototype */
int main()
{
int a; //declaration variables
int b; //declaration variables
cout << "Type current year here: ";
cin >> a;
cout << "Type year of birth: ";
cin >> b;
cout << "This is your age now: ";
cout << x (a, b) << endl;
return 0;
}
int x (int a, int b) // function definition
{
int x = (a - b);
int d;
cin >> d;
return x;
} Your code is quite correct but I don't understand why you have taken
as I don't find find any use of it.
For more clearity use variable name different from the function name ( I m pointing to "x")
Regards
| | Newbie | | Join Date: Jul 2007
Posts: 29
| | | re: Newbie question - coding standards - how should I write this
It's because inside your function x(int, int) ask for another user input that will be stored in the int d variable, I already commented that, now it should work as below. -
-
#include <iostream> //needed for input output
-
-
using namespace std;
-
-
//---------------------------------------------------------------------------
-
-
-
int x (int, int); /* function declaration
-
or prototype */
-
-
int main()
-
-
{
-
int a; //declaration variables
-
int b; //declaration variables
-
-
cout << "Type current year here: ";
-
cin >> a;
-
-
cout << "Type year of birth: ";
-
cin >> b;
-
-
cout << "This is your age now: ";
-
cout << x (a, b) << endl;
-
-
return 0;
-
}
-
-
int x (int a, int b) // function definition
-
-
{
-
int x = (a - b);
-
-
//int d;
-
//cin >> d;
-
-
return x;
-
}
-
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Newbie question - coding standards - how should I write this Quote:
Originally Posted by eagerlearner - int x (int a, int b) // function definition
-
-
{
-
int x = (a - b);
-
-
//int d;
-
//cin >> d;
-
-
return x;
-
}
-
And while we're at it, I wouldn't mind a simple: -
int x(int a, int b) {
-
return a-b;
-
}
-
kind regards,
Jos
| | Newbie | | Join Date: Jul 2007
Posts: 13
| | | re: Newbie question - coding standards - how should I write this
Thanks guys.
@eagerlearner.
I used
int d;
cin >> d;
to keep the console open. If I now use
#include <conio>
along with
getch ()
I get an error on the line with
return x;
telling me that there is a statement missing.
Any suggestions into the best wat to accomplish the console staying open without having side effects?
| | Member | | Join Date: May 2007
Posts: 86
| | | re: Newbie question - coding standards - how should I write this
Ok since you are asking about coding standards, here are my suggestions.
1. Use meaningful names for your variables
2. Avoid "using namespace"
Regarding pausing your program, I think the best suggestion is to run Console applications from a console window, thus eliminating the need to pause the program. However, if you must, the best way to pause your program is with cin. Unfortunately there are a lot of issues that can cause cin to fail that most beginners can't handle so therefore I recommend for now just using system("pause"); on Windows machines.
Finally, since I touched on things that can cause cin to fail. In your code you are reading in an int, if the user enters a letter instead of a number, cin will fail as will all the subsequent calls to it. One way to avoid this is to input everything as strings, validate the input, then convert it to a number and process it. I'll leave that for you to investigate on your own. -
#include <vcl.h> //needed for Borland
-
#include <iostream> //needed for input output
-
-
//---------------------------------------------------------------------------
-
-
int CalculateAge (int, int); /* function declaration
-
or prototype */
-
-
int main()
-
{
-
int currentyear; //declaration variables
-
int birthyear; //declaration variables
-
-
std::cout << "Type current year here: ";
-
std::cin >> currentyear;
-
-
std::cout << "Type year of birth: ";
-
std::cin >> birthyear;
-
-
std::cout << "This is your age now: ";
-
std::cout << CalculateAge (currentyear, birthyear) << endl;
-
-
system("pause"); // Windows Only - Not like by some, but works reliably for newbies"
-
-
return 0;
-
}
-
-
int CalculateAge (int current_year, int birth_year) // function definition
-
{
-
int age = (CurrentYear - BirthYear);
-
return age;
-
}
-
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: Newbie question - coding standards - how should I write this
Are you using Visual Studio??
If so, when your execute your program, select Start Without Debuggging rather than just Start. Visual Studio assumes you are using your debugger and if you are not (Select Without Debugging), then Visual Studio will pause your program at the end of main() with "Press any key to continue".
If not, then a cin.get() at the end of main() will work. Just press enter to finish the program.
Your
int d;
cin>>d;
also works assuming you enter an integer and press enter.
| | Newbie | | Join Date: Jul 2007
Posts: 13
| | | re: Newbie question - coding standards - how should I write this Quote:
Originally Posted by Darryl Ok since you are asking about coding standards, here are my suggestions.
1. Use meaningful names for your variables
2. Avoid "using namespace"
Regarding pausing your program, I think the best suggestion is to run Console applications from a console window, thus eliminating the need to pause the program. However, if you must, the best way to pause your program is with cin. Unfortunately there are a lot of issues that can cause cin to fail that most beginners can't handle so therefore I recommend for now just using system("pause"); on Windows machines.
Finally, since I touched on things that can cause cin to fail. In your code you are reading in an int, if the user enters a letter instead of a number, cin will fail as will all the subsequent calls to it. One way to avoid this is to input everything as strings, validate the input, then convert it to a number and process it. I'll leave that for you to investigate on your own. -
#include <vcl.h> //needed for Borland
-
#include <iostream> //needed for input output
-
-
//---------------------------------------------------------------------------
-
-
int CalculateAge (int, int); /* function declaration
-
or prototype */
-
-
int main()
-
{
-
int currentyear; //declaration variables
-
int birthyear; //declaration variables
-
-
std::cout << "Type current year here: ";
-
std::cin >> currentyear;
-
-
std::cout << "Type year of birth: ";
-
std::cin >> birthyear;
-
-
std::cout << "This is your age now: ";
-
std::cout << CalculateAge (currentyear, birthyear) << endl;
-
-
system("pause"); // Windows Only - Not like by some, but works reliably for newbies"
-
-
return 0;
-
}
-
-
int CalculateAge (int current_year, int birth_year) // function definition
-
{
-
int age = (CurrentYear - BirthYear);
-
return age;
-
}
-
Thanks for that, nice one, that was helpfull.
I did however need to change some tiny things before it could be compiled. I changed "int age = (CurrentYear - BirthYear)" to "int age = (currentyear - birthyear)" and I also changed "int CalculateAge (int current_year, int birth_year)" to "int CalculateAge (int currentyear, int birthyear)".
Why did you put underscores in the function definition and why did you write the function "int age = (CurrentYear - BirthYear)" with caps, what am I missing?
Thanks!
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Newbie question - coding standards - how should I write this Quote:
Originally Posted by Break2 Thanks for that, nice one, that was helpfull.
I did however need to change some tiny things before it could be compiled. I changed "int age = (CurrentYear - BirthYear)" to "int age = (currentyear - birthyear)" and I also changed "int CalculateAge (int current_year, int birth_year)" to "int CalculateAge (int currentyear, int birthyear)".
Why did you put underscores in the function definition and why did you write the function "int age = (CurrentYear - BirthYear)" with caps, what am I missing?
Thanks! Consider them typos; nothing to worry about; you're on the right track.
kind regards,
Jos
| | Member | | Join Date: May 2007
Posts: 86
| | | re: Newbie question - coding standards - how should I write this
The capitalization was typos, I originally had them capital then decided to make them lower case and didn't change all of them.
However, the underscores was so I could use the same name but not use the same name. Technically, I could have used the same exact for both, because they are in different scopes, but that too is considered bad practice so the underscores was just a way of using a different variable name but still use the same name since they conveyed the same meaning.
| | Newbie | | Join Date: Jul 2007
Posts: 13
| | | re: Newbie question - coding standards - how should I write this
Thanks for that Jos and thanks for the thumbs up!
This is truly a helpfull forum.
| | Newbie | | Join Date: Jul 2007
Posts: 13
| | | re: Newbie question - coding standards - how should I write this Quote:
Originally Posted by Darryl The capitalization was typos, I originally had them capital then decided to make them lower case and didn't change all of them.
However, the underscores was so I could use the same name but not use the same name. Technically, I could have used the same exact for both, because they are in different scopes, but that too is considered bad practice so the underscores was just a way of using a different variable name but still use the same name since they conveyed the same meaning. Yes, later I discovered I only had to change "int age = (CurrentYear - BirthYear)" to "int age = (current_year - birth_year)" and it worked.
Thanks again for your explanation, it was real helpfull.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,392 network members.
|