A beginner's code, need suggestion

This is a discussion on A beginner's code, need suggestion within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to programming and I just want to know if the code written below is good, average ...

  1. #1
    Registered User sumitsatsangi's Avatar
    Join Date
    Jan 2012
    Posts
    5

    Question A beginner's code, need suggestion

    Hi,

    I am new to programming and I just want to know if the code written below is good, average or bad as a professional programmer.

    Thanks in advance.

    Regards,
    Sumit


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a,rem, rev = 0;
    
        cout<<"Please enter a number: ";
        do
        {
            cin>>a;
            if(a>10)
                break;
            cout<<"Please enter another number higher than 10: ";
        } while (a<10);
    
        if(a>=10)
        {
            while (a!=0)
            {
                rem = a % 10;
                rev = rev*10 + rem;
                a = a/10;
            }
            cout<<endl<<endl<<"Reverse is "<<rev<<endl<<endl;
        }
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Well I won't give you a grade or anything but ...

    unless a could change somewhere after the input loop the if block following it is unnecessary, and

    a professional would worry about what happens if the input would fail.

  3. #3
    Registered User sumitsatsangi's Avatar
    Join Date
    Jan 2012
    Posts
    5
    First of all, thanks for the help.

    Well, I put the if block specifically there to test a situation where a user tries to input numbers below 10 many times (probably just for fun or whatever)

    And I didn't understand the input failure part. Could you elaborate.

    Thanks

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Quote Originally Posted by sumitsatsangi View Post
    First of all, thanks for the help.

    Well, I put the if block specifically there to test a situation where a user tries to input numbers below 10 many times (probably just for fun or whatever)

    And I didn't understand the input failure part. Could you elaborate.

    Thanks
    Just so that we're talking about the same code, I mean the if block that starts on line 18. There is no need to check that because of the input loop condition.

    And what I mean is basically what should happen if the user types something that is nothing like a number. For example 1234 can be converted to a number by the cin object but onetwothreefour cannot.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    couldn't the while portion be put behind the first IF statement, leaving the second if block out completely? (to do the same thing I mean)

    Sry... also just learning this stuff, so I was wondering.

    Like this:
    Code:
        do
        {
            cin>>a;
            if(a>=10)
            {
    			while (a!=0)
    			{
    				rem = a % 10;
    				rev = rev*10 + rem;
    				a = a/10;
    			}
    			cout<<endl<<endl<<"Reverse is "<<rev<<endl<<endl;
    			break;
            }
            else
            {
            	cout<<"Please enter another number higher than 10: ";
            }
        } while (a<10);

  6. #6
    Registered User sumitsatsangi's Avatar
    Join Date
    Jan 2012
    Posts
    5
    Thanks nGAGE... looks better... BTW, none of my codes are my homework... I took home tuition for C++ in order to understand programming concepts. Now they are over and I am working on my own, exploring new programs (ofcourse of middle levels) and hoping to enhance my learning curve.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    I'm only a beginner, so don't take my suggestion as professional feedback or something. It's been only a week since i've started reading the tutorials on this site, but I gladly try to look at things I didn't write and rewrite/rearrange it, if possible. Guess it helps with the learning better.
    At this point, I've rewritten my own little (learning-)project like 4 times, every time in a different way, applying new things learned. Probably all not making much programming sense :P
    It's pretty hard to understand good programming practice and logic at the beginning, or when to apply what methods. At least, for me.

    Anyways,... Wish you enjoyment with your further projects.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by nGAGE View Post
    It's pretty hard to understand good programming practice and logic at the beginning, or when to apply what methods. At least, for me.
    One learns by doing. In the beginning, it's hard. But gradually, it gets easier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-15-2012, 07:06 PM
  2. Replies: 7
    Last Post: 08-21-2011, 09:10 PM
  3. Problem with beginner code... please help
    By rkrajnov in forum C Programming
    Replies: 2
    Last Post: 09-26-2010, 12:47 AM
  4. A little suggestion before positing your code
    By audinue in forum C Programming
    Replies: 11
    Last Post: 07-09-2008, 10:57 AM
  5. Suggestion - code forum.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-19-2003, 08:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21