Thread: Advice to make my programming better(beginner programmer)

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    34

    Advice to make my programming better(beginner programmer)

    Hello I am a fairly new at programming and have little experience with programming beyond the tutorials on this website(which I think I am on the 15th) and I have been messing around with a few programs that they have had in the the tutorials and expanding them and I was just wondering if there is any bad habits, things I could do to make my programs more organized, or anything else that I should try to fix now before I get to use to programming like that. One of the programs I have expanded on was in the tutorial about typecasting(lesson 11) and was showing how you could turn numbers into ASCII characters. I expanded on the program by giving an option to search for a certain number's character or to show all of the ASCII numbers. Here is what the program looks like
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int allchar()//prints all ASCII characters
    {
        int x = 0;//sets x to 0; allows for multiple printings of the ASCII characters
      while(x < 256) {
        cout<< x <<". "<< (char)x <<" ";
        x=x+1;
        //Note the use of the int version of x to
        // output a number and the use of (char) to
        // typecast the x into a character
        // which outputs the ASCII character that
        // corresponds to the current number
      }
      return 0;
    }
    int picked_char()//User inputs the number to find which character it is
    {
        cout<<"Please enter in the characters number you want to find.\n";
        int character;
        cin>>character;
        cin.ignore();
        character=character;
        cout<<character<<". "<< (char)character <<" ";
        return 0;
    
    
    }
    
    
    
    
    int main()
    {
        int again=1;//runs program again
        while(again == 1){
    
    
            int choice;//user input for list of ascII or certain one
            int letter;//uesr input for which character to search
            int again_repeat = 1;//makes sure correct input in the while(again_repeat)
            int repeat=1;//repeats while(again ==1) statement till correct input is put in
            int program_repeat;//input to control if program repeats
            while(repeat == 1){
                cout<<"Do you want a list of ASCII characters(press 1)\n";
                cout<<"or search for a certain one(press 2?\n";
                cout<<"\n";
                cin>>choice;
                cin.ignore();
                if(choice == 1){
                    allchar();
                    cout<<"\n";
                    cout<<"\n";
                    choice = 0;
                    repeat = 0;
    
    
                }
                else if (choice == 2){
                    picked_char();
                    cout<<"\n";
                    cout<<"\n";
                    choice = 0;
                    repeat = 0;
                }
                else{
                    cout<<"Invaild Entry. Please re-enter choice.\n";
                    cout<<"\n";
                    repeat = 1;
                }
            }
            cout<<"If you want to search for another ASCII character press 1.\n";
            cout<<"If you want to exit this program press 2.\n\n";
            while(again_repeat == 1){
                cin>>program_repeat;
                cin.ignore();
                cout<<"\n\n";
                if(program_repeat==1){
                    again=1;
                    again_repeat = 0;
                }
                else if(program_repeat==2){
                    again_repeat = 0;
                    again=0;
                }
                else{
                    cout<<"Invaild Entry. Please re_enter choice.\n";
                }
            }
        }
        return 0;
    }
    If any of you have any suggestions on things that might improve my programming now so I will not develop bad programming habits. Oh also sorry if the variables have names that might not make sense could not think of other names for them in the moment.
    Last edited by thadis_4; 12-11-2011 at 06:12 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indentation is a little inconsistent. Not a biggie, but prettier code makes everyone happy.
    Break your while loop into smaller parts which you can in turn put into functions which you call in the while loop. Easier to read, easier to maintain.
    Instead of using variables to control the loop, use break and continue.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Yeah I tried using functions before but I had problems with the variables that are needed in multiple different functions and having the variable that I changed locally to change and be used in different functions from where I changed it. I also am confused a little with the return function saying they never seemed to work as I expected so I think I am setting them up wrong. I also know of break but I did not know if using break would break for all the while loops or just the one I typed it in. I do not recall learning about the continue though would you mind explaining when to use that?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This does nothing (where character is declared as an int):
    Code:
    character=character;
    What were you imagining it would do?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by thadis_4 View Post
    Yeah I tried using functions before but I had problems with the variables that are needed in multiple different functions and having the variable that I changed locally to change and be used in different functions from where I changed it.
    Pass by reference.

    I also am confused a little with the return function saying they never seemed to work as I expected so I think I am setting them up wrong.
    Don't know what that means...

    I also know of break but I did not know if using break would break for all the while loops or just the one I typed it in. I do not recall learning about the continue though would you mind explaining when to use that?
    break breaks out of the current loop only.
    continue jumps to the beginning of a loop (current loop).

    By all means, go through some tutorials on functions. They are necessary and should be a basic tool.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    To IMalc:
    Honestly I do not know why I put that there. Wondering why I did put that there because even I can see that putting that is just redundant.

    To Elysia:
    Thanks for explaining the continue function. I have been using the tutorial on this site but think I accidentally skipped the one that had breaks in it. I tried looking for the one that has continue but I was not able to find it one this site. Currently am waiting to get the book "C++ Primer Plus" in the mail. Until get that though are there any specific web sites you think I should look at or should I just look for one on google?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am not aware of any specific sites. However, this site has some good stuff and more coming.
    But remember: no tutorial can beat a good book. They will cover more and they will cover it more in depth.
    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.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Yeah that's my problem with this site. It covers the stuff but when I read it I do not really understand how to use it or the applications of the concepts just partial theoretical knowledge of the concepts. Hope I get my book tomorrow.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The best way to learn is to put knowledge to use. Make some programs that you think can benefit from these concepts and experiment.
    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.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by thadis_4 View Post
    Currently am waiting to get the book "C++ Primer Plus" in the mail. Until get that though are there any specific web sites you think I should look at or should I just look for one on google?
    Manasij recommended this the other day:

    C++ Annotations | Free Development software downloads at SourceForge.net

    I haven't read that much of it but I plan to; it looks better than most of the C++ books I have seen, and it's free online.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    To Elysia:
    Yeah that is what I have been trying to do when I am able to. So far I have this program I made and also another that will convert either Celsius, Fahrenheit or Kelvin into any of the others. Hopefully next week I am able to experiment more saying I will be on break by then.

    To MK27:
    Thanks for the link I will check it out. Hopefully it will help me though do not have time yet to check it out really.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice for new C programmer re Windows
    By willsteele in forum Windows Programming
    Replies: 5
    Last Post: 11-25-2010, 02:04 PM
  2. Novice Programmer Humbly Requests Job Seeking Advice
    By steals10304 in forum General Discussions
    Replies: 7
    Last Post: 10-20-2009, 12:12 PM
  3. Fortran programmer new to c++. Looking for general advice.
    By Duff-Man in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2009, 12:33 AM
  4. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 08:15 AM