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;
}
11 1431
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
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;
-
}
-
- 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
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?
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;
-
}
-
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.
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!
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
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.
Thanks for that Jos and thanks for the thumbs up!
This is truly a helpfull forum.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Natt Serrasalmus |
last post by:
After years of operating without any coding standards whatsoever, the
company that I recently started working for has decided that it might be a
good idea to have some. I'm involved in this...
|
by: Andrea Williams |
last post by:
Responding to this link in an old thread:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/cpconnetframeworkdesignguidelines.asp
According to that naming...
|
by: ct-86 |
last post by:
http://www.cdbook.cn/book.asp?id=2393
Organizational and Policy Issues
1
0. Don't sweat the small stuff. (Or: Know what not to standardize.)
2
1. Compile cleanly at high warning levels.
4
2....
|
by: Robert Seacord |
last post by:
The CERT/CC has just deployed a new web site dedicated to developing
secure coding standards for the C programming language, C++, and
eventually other programming language.
We have already...
|
by: AzizMandar |
last post by:
C++ Event Coding Questions
I have done some simple programs in C++ and read a lot of good C++
books (Including The C++ Programing Language, and C++ Primer) I am
trying to understand and...
|
by: auratius |
last post by:
http://www.auratius.co.za/CSharpCodingStandards.html
Complete CSharp Coding Standards
1. Naming Conventions and Styles
2. Coding Practices
3. Project Settings and Project Structure
4....
|
by: acoder |
last post by:
It's strange, isn't it? After so many years of standards, you would've thought that you could write a simple piece of script that would work in all browsers. Alas, this is not always the case. With...
|
by: dom.k.black |
last post by:
Can anyone recommend a good existing C++ coding standard - parctical,
pragmatic and sensible?
A company I joined recently are moving from C to C++, they are very
much into coding standards. But...
|
by: Andy Dingley |
last post by:
On 15 May, 04:55, Prisoner at War <prisoner_at_...@yahoo.comwrote:
Or in another universe, where things are understood and site code is
stable and reliable, beginners don't even think about...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |