Thread: Is my codeing clean

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    Is my codeing clean

    I was just wandering what my code below is like is it perfectly legal and clean? and was also wandering why I have to keep putting cin.get() under each cin statement in order to stop the screen from diapperaing before the result can be shown.

    Code:
    #include <iostream>
    
    int Number1 = Number1;
    int Number2 = Number2;
    
    int main()
    {
        
        std::cout << "Please enter the first number:\n";
        std::cin >> Number1;
        std::cin.get();
        
        
        std::cout << "Please enter the second number:\n";
        std::cin >> Number2;
        std::cin.get();
        
        std::cout << "The two numbers added together equal: " << Number1 + Number2;
        
        std::cin.get();
        return 0;
        
    }

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Other than the global variables and the two gin.get() lines that really aren't needed (the first two), yes...your code iis relatively clean...

    SO FAR!

    It's easy for a project to START OUT clean..just wait until you have a few hundred more lines...you just have to make sure you comment and separate logical functions and data and such into header files and such...and of course, avoid variable names that don't give any clues as to what the variable does.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    1.
    Don't use globals unless necessary

    2.
    The reason why you have to put a cin.get() after each cin>> is because cin>> leaves a '\n' in the buffer which your cin.get() picks up. If you want to you could just put cin.ignore(80,'\n'); before your last cin.get() and not worry about it.
    Woop?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    int Number1 = Number1;
    int Number2 = Number2;
    This most certainly is not clean - and shouldn't even compile, if I remember scoping rules correctly.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Your program is perfectly legal.

    You need to use cin.get() becuase the program will naturally continue to execute the code in procedural order within the function main() (or any other function for that matter).

    If you do this:

    int i = 3;
    i = 2;

    i equals 2 because the program continued executing all of the lines in the function. You need to tell the program to pause because it normally won't just pause after every output (that would be annoying most of the time!).

    I would add one line to your code under your include statement:
    using namespace std;

    This eliminates the need to prefix every cout or cin with "std::".

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    Changed Code

    I changed the code to see if it's now any cleaner
    Code:
    #include <iostream>
    
    int Number1 = Number1;
    int Number2 = Number2;
    
    int main()
    {
        
        std::cout << "Please enter the first number:\n";
        std::cin >> Number1;
        std::cin.ignore(80,'\n');
        
        
        std::cout << "Please enter the second number:\n";
        std::cin >> Number2;
        std::cin.ignore(80,'\n');
        
        std::cout << "The two numbers added together equal: " << Number1 + Number2;
        
        std::cin.get();
        return 0;
        
    }

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    You're still declaring your variables as global. Put them inside your main() function.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Kaelin
    I would add one line to your code under your include statement:
    using namespace std;

    This eliminates the need to prefix every cout or cin with "std::".
    Perhaps it's just a matter of style, but I generally use "using std::cout;" and the like instead of opening up the entire std namespace. That way I know exactly what names I'm pulling in from the namespace.

    ComDriver, you don't need this:
    Code:
    int Number1 = Number1;
    int Number2 = Number2;
    I'm not exactly sure what you are trying to accomplish with the assignment, but to declare the global variables, all you need is this:
    Code:
    int Number1, Number2;
    Further, there is no good reason for these variables to be global. It would work fine if the variables were declared in main.
    Code:
    int main()
    {
        int Number1, Number2;
        //rest of program
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int Number1 = 0, Number2 = 0;
     
        cout << "Please enter the first number:\n";
        cin >> Number1;
        cin.ignore(80,'\n');
        
        
        cout << "Please enter the second number:\n";
        cin >> Number2;
        cin.ignore(80,'\n');
        
        cout << "The two numbers added together equal: " << Number1 + Number2;
        
        cin.get();
        return 0;
        
    }

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    21
    And how about now?


    Code:
    #include <iostream>
    
    int main()
    {
        
        int Number1 = Number1, Number2;
        
        std::cout << "Please enter the first number:\n";
        std::cin >> Number1;
        std::cin.ignore(80,'\n');
        
        
        std::cout << "Please enter the second number:\n";
        std::cin >> Number2;
        std::cin.ignore(80,'\n');
        
        std::cout << "The two numbers added together equal: " << Number1 + Number2;
        
        std::cin.get();
        return 0;
        
    }

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    pianorain is right - I should have mentioned that it is certainly a matter of style, whether or not you want to open the namespace.

    I prefer it because I quickly get annoyed with having to place std:: in front of everything. But that's just my style

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Code:
    int Number1 = Number1, Number2;
    You cannot do this. You cannot initialize a variable to itself. Either leave it uninitialized or initialize it to a constant or a variable that already exists.

    Code:
    int Number1, Number2;

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    int Number1 = Number1
    Stop assigning variables to themselves on definition. That's undefined behaviour at best.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Code:
    int main()
    {
        
        int Number1, Number 2;
    
    ...

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Sans the space between 'Number' and '2'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  2. how to know that the buffer is not clean??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 09:56 PM
  3. prepro. clean tool / #id#def mess
    By tomsky in forum Linux Programming
    Replies: 2
    Last Post: 09-08-2005, 07:18 AM
  4. clean out a buffer
    By threahdead in forum C Programming
    Replies: 8
    Last Post: 02-23-2003, 01:04 PM
  5. make clean
    By Jaguar in forum Linux Programming
    Replies: 5
    Last Post: 12-27-2002, 06:44 PM