473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Newbie question - coding standards - how should I write this

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;
}
Jul 17 '07 #1
11 1431
Meetee
931 Expert Mod 512MB
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
Expand|Select|Wrap|Line Numbers
  1. int d;
  2. cin >> d;
  3.  
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
Jul 17 '07 #2
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream> //needed for input output
  3.  
  4. using namespace std;
  5.  
  6. //---------------------------------------------------------------------------
  7.  
  8.  
  9. int x (int, int); /* function declaration
  10. or prototype */
  11.  
  12. int main()
  13.  
  14. {
  15. int a; //declaration variables
  16. int b; //declaration variables
  17.  
  18. cout << "Type current year here: ";
  19. cin >> a;
  20.  
  21. cout << "Type year of birth: ";
  22. cin >> b;
  23.  
  24. cout << "This is your age now: ";
  25. cout << x (a, b) << endl;
  26.  
  27. return 0;
  28. }
  29.  
  30. int x (int a, int b) // function definition
  31.  
  32. {
  33. int x = (a - b);
  34.  
  35. //int d;
  36. //cin >> d;
  37.  
  38. return x;
  39. }
  40.  
Jul 17 '07 #3
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. int x (int a, int b) // function definition
  2.  
  3. {
  4. int x = (a - b);
  5.  
  6. //int d;
  7. //cin >> d;
  8.  
  9. return x;
  10. }
  11.  
And while we're at it, I wouldn't mind a simple:

Expand|Select|Wrap|Line Numbers
  1. int x(int a, int b) {
  2.    return a-b;
  3. }
  4.  
kind regards,

Jos
Jul 17 '07 #4
Break2
13
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?
Jul 17 '07 #5
Darryl
86
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.

Expand|Select|Wrap|Line Numbers
  1. #include <vcl.h> //needed for Borland
  2. #include <iostream> //needed for input output
  3.  
  4. //---------------------------------------------------------------------------
  5.  
  6. int CalculateAge (int, int); /* function declaration
  7. or prototype */
  8.  
  9. int main()
  10. {
  11.      int currentyear; //declaration variables
  12.      int birthyear; //declaration variables
  13.  
  14.      std::cout << "Type current year here: ";
  15.      std::cin >> currentyear;
  16.  
  17.      std::cout << "Type year of birth: ";
  18.      std::cin >> birthyear;
  19.  
  20.      std::cout << "This is your age now: ";
  21.      std::cout << CalculateAge (currentyear, birthyear) << endl;
  22.  
  23.      system("pause"); // Windows Only - Not like by some, but works reliably for newbies"
  24.  
  25.      return 0;
  26. }
  27.  
  28. int CalculateAge (int current_year, int birth_year) // function definition
  29. {
  30.      int age = (CurrentYear - BirthYear);
  31.      return age;
  32. }
  33.  
Jul 17 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 17 '07 #7
Break2
13
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.

Expand|Select|Wrap|Line Numbers
  1. #include <vcl.h> //needed for Borland
  2. #include <iostream> //needed for input output
  3.  
  4. //---------------------------------------------------------------------------
  5.  
  6. int CalculateAge (int, int); /* function declaration
  7. or prototype */
  8.  
  9. int main()
  10. {
  11.      int currentyear; //declaration variables
  12.      int birthyear; //declaration variables
  13.  
  14.      std::cout << "Type current year here: ";
  15.      std::cin >> currentyear;
  16.  
  17.      std::cout << "Type year of birth: ";
  18.      std::cin >> birthyear;
  19.  
  20.      std::cout << "This is your age now: ";
  21.      std::cout << CalculateAge (currentyear, birthyear) << endl;
  22.  
  23.      system("pause"); // Windows Only - Not like by some, but works reliably for newbies"
  24.  
  25.      return 0;
  26. }
  27.  
  28. int CalculateAge (int current_year, int birth_year) // function definition
  29. {
  30.      int age = (CurrentYear - BirthYear);
  31.      return age;
  32. }
  33.  
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!
Jul 17 '07 #8
JosAH
11,448 Expert 8TB
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
Jul 17 '07 #9
Darryl
86
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.
Jul 17 '07 #10
Break2
13
Thanks for that Jos and thanks for the thumbs up!

This is truly a helpfull forum.
Jul 17 '07 #11
Break2
13
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.
Jul 17 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

144
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...
6
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...
3
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....
7
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...
4
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...
19
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....
8
acoder
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...
9
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...
44
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
jinu1996
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.