1

this is my first question. I'm really new to programming and I'm struggling when it comes to discerning from well structured code from WTF codemonkey code. Is there anything I should have done different when creating this ubber simple program? My main concern is regarding the function.

        #include <iostream>
        using namespace std;

        void enter_numbers ( int & iNum1, int & iNum2 ) {
        cout << "Enter first number" << endl;
        cin >> iNum1;
        cout << "Enter second number" << endl;
        cin  >> iNum2;
        }

        float calc_avg ( int iNum1, int iNum2){
            float fRes;
            fRes =(float)(iNum1 + iNum2)/2;
            return fRes;
        }

        void show_avg ( float fRes ) {
        cout << "Average is: " << fRes;
        }

        void main () {
        int iNum1;
        int iNum2;
        enter_numbers ( iNum1, iNum2);
        float fRes = calc_avg (iNum1, iNum2);
        show_avg ( fRes );
        fflush(stdin);
        getchar ();

       } 
MV_81
  • 111
  • 1
  • 9
  • What is *"the function"* you are concerned about? – IInspectable Nov 17 '13 at 21:15
  • For beginners, you will be more than happy with this code. And for the question, Yes there is. You should pass the parameters as a constant reference since you are not modifying them. – CroCo Nov 17 '13 at 21:15
  • Usually people use double instead of float. You can google it to find the reason (precision). – TT_ stands with Russia Nov 17 '13 at 21:16
  • fRes =(float)(iNum1 + iNum2)/2; will return an int assigned to float. – goldcode Nov 17 '13 at 21:17
  • Creating so many functions is unnecessary and hinders readability. – A.B. Nov 17 '13 at 21:17
  • @goldcode Why int? What about the cast? – TT_ stands with Russia Nov 17 '13 at 21:18
  • @goldcode I would think that `(iNum1 + iNum2)` will be cast to `float`, which then will be divided by `2` and assigned to `float fRes` as... well, `float`. No? – lapk Nov 17 '13 at 21:18
  • 1
    @A.B. Those are utility functions, every one is responsible for its own task. Seems OK to me. – TT_ stands with Russia Nov 17 '13 at 21:19
  • IInspectable, according the way I'm being taught float calc_avg would be a function and the rest of the "modules" are procedures. Hope this makes it cleares for everyone. TY. – MV_81 Nov 17 '13 at 21:20
  • 1
    This question probably belongs to [Code Review](http://codereview.stackexchange.com), especially see Code Review's ["What topics can I ask about here?" page](http://codereview.stackexchange.com/help/on-topic). I don't think it contains a problem specific enough to be on-topic for StackOverflow. – dyp Nov 17 '13 at 21:41

3 Answers3

1
  1. You should not fflush an input stream, as described here: http://www.gidnetwork.com/b-57.html
  2. Your main function should have int as return type and return 0, for successful execution: What should main() return in C and C++?

In rest, your code seems fine. Note that for a beginner you should be happy, it is pretty good as it is. I just mentioned a few improvements you may want to made and pointed some sources for further documentation, as i guess you want to learn more.

Community
  • 1
  • 1
Paul92
  • 8,827
  • 1
  • 23
  • 37
  • 4
    C++ compilers are obliged to insert `return (0);` in `main` if programmer omits it. – lapk Nov 17 '13 at 21:22
1

Every corporate culture - or any other group of people working on code together - is going to have a set of coding style guidelines, variable and function naming conventions, and so on and so forth. There are going to be a vast plethora of opinions you'll hear (probably really soon =D) about how to make code readable, and the following issues are mostly religious:

  • Hungarian notation (variable names like lpctszName) vs plain English. My opinion is that there's a time and a place for it. Look up "Apps Hungarian" vs "Systems Hungarian".

  • Indentation. We all agree (pretty much - dissenters?) that the bodies of a compound statement (e.g. function definitions) should be indented. So do that. However, there are really heated debates about which line the brace goes on. I like the opening brace at the end of the line. Look up "One true indent style".

  • ReadableVariable vs readableVariable vs readable_variable and so on.

The bottom line is that, like any other body of text, code has an audience. It's the people you're working with, or who take over maintenance of your code, or who test it. In personal projects, it's you, some weeks down the road when you don't remember what variables were what. Your coding style should be consistent with what that audience knows.

Other than that, if there are no exceptions or compile-time errors, your code is good. In the future, working code goes on the code review StackExchange, though, and conceptual questions go to the programmers StackExchange. Welcome to StackOverflow =D.

sqykly
  • 1,586
  • 10
  • 16
0

Just write num1, num2, res and not iNum1, iNum2, fRes. The type is already visible as part of the declaration.

Marcel Krüger
  • 879
  • 8
  • 16