Newbie question - coding standards - how should I write this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Break2
    New Member
    • Jul 2007
    • 13

    Newbie question - coding standards - how should I write this

    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;
    }
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    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
    Code:
    int d;
    cin >> d;
    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
    Last edited by Meetee; Jul 17 '07, 01:02 PM. Reason: oops!! no code tags added :)

    Comment

    • eagerlearner
      New Member
      • Jul 2007
      • 29

      #3
      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.

      Code:
      #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;
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by eagerlearner
        [code=c]int x (int a, int b) // function definition

        {
        int x = (a - b);

        //int d;
        //cin >> d;

        return x;
        }
        [/code]
        And while we're at it, I wouldn't mind a simple:

        [code=c]
        int x(int a, int b) {
        return a-b;
        }
        [/code]

        kind regards,

        Jos

        Comment

        • Break2
          New Member
          • Jul 2007
          • 13

          #5
          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?

          Comment

          • Darryl
            New Member
            • May 2007
            • 86

            #6
            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.

            Code:
            #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;
            }

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              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.

              Comment

              • Break2
                New Member
                • Jul 2007
                • 13

                #8
                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.

                Code:
                #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!

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  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

                  Comment

                  • Darryl
                    New Member
                    • May 2007
                    • 86

                    #10
                    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.

                    Comment

                    • Break2
                      New Member
                      • Jul 2007
                      • 13

                      #11
                      Thanks for that Jos and thanks for the thumbs up!

                      This is truly a helpfull forum.

                      Comment

                      • Break2
                        New Member
                        • Jul 2007
                        • 13

                        #12
                        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.

                        Comment

                        Working...