Thread: Advice on refining mediocre code?

  1. #1
    Registered User Vendredi's Avatar
    Join Date
    Mar 2015
    Posts
    3

    Question Advice on refining mediocre code?

    As many posters here are, I am new to C++ programming; and to get to the point, I was tasked by my professor to create a "Who wants to be a Millionaire" game (Standard console text, 0% UInterface)
    Before you ask, this is NOT a demand for a completed project or code. I have already finished the game to the best of my knowledge.
    Now the reason why I am here; I feel that my lessons in class are proving to be more of a chore than a learning experience, over and over I am tasked to retread Do-while loops and If-else-if series's. It's my second semester now in programming and I feel that there is so much I am not being taught (yet, but its just taking forever to get there).

    In any case; This is my code, that should work with (and probably only with) Turbo C++. I have no idea why we still use this outdated software but here it is nonetheless, finished. and I am here to ask the expert programmers among this board for advice, expert simplification on syntax's. I understand I could have taken a shortcut here, or there than making things so simple.

    The strange symbols are ASCII art btw.

    Code:
     #include<iostream.h>#include<conio.h>
    
    
    int main()
    {
    beginning:
    clrscr();
    int num,question;
    
    
    for(int e=0;e<90;e++){
    cout<<"";
    }
    cout<<"\n\n\n\n\n";
    for(int z=50;z>10;z--){
    cout<<"зз";}
    for(int zx=22;zx>6;zx--){
    cout<<"   л ";  }
    for (int i=0;i<40;i++){
    cout<<"зз";}
    cout<<"\n";
    cout<<"\n";
    cout<<"\n\t     лл       лл                    ллллл ллллл              ";
    cout<<"\n\t     лл       лл                    ллллллллллл               ";
    cout<<"\n\t     лл  ллл  лл                    ллл ллл ллл              ";
    cout<<"\n\t     ллллллллллл HO WANTS TO BE A   ллл     ллл  ILLIONAIRE            ";
    cout<<"\n\t     лллл   лллл                    ллл     ллл [Drive-To edition]";
    cout<<"\n\t\t\t\t   ";
    cout<<"\n\t\t\t\t     \t";
    cout<<"\n\t\t\t\t     \t";
    cout<<"\n\t\t\t\t       ";
    cout<<"\n\t\t\t\t       ";
    cout<<"\n\t\t\t\t \t\t\t \n \t\t  [Programmed by:                           ]";
    cout<<"\n\n\n";
    for(int zd=50;zd>10;zd--){
    cout<<"зз";}
    for(int zxd=22;zxd>6;zxd--){
    cout<<"   л ";  }
    for (int id=0;id<40;id++){
    cout<<"зз";}
    cout<<"\n \t\t\t   [PRESS THE ENTER BUTTON] ";
    getch();
    againz:
    clrscr();
    for (int u=0;u<320;u++){
    cout<<"л";            }
    cout<<"\n\n===============================================================================\n";
    cout<<"\n\n\t\t\t       INSTRUCTIONS\n";
    cout<<"\n  1. Each sucessful choice will give you the NEXT valuable cash prize\n   2. While the first wrong answer prevents you from gaining anymore points!\n    3. You may use LOWER or UPPER case letters!\n";
    cout<<"\n\t\t\t  [PRESS THE ENTER BUTTON]\n\t\t";
    getch();
    clrscr();
    int points=0;
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                                    ";
    cout<<"\n     лл Џ "<<points<<"KЎ                                                        бЪЪб  ";
    cout<<"\n     ллллллллллл                                                          ";
    cout<<"\n     ллллБББлллл                                                          ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                 [10]  [9]  [8]  [7]  [6]  [5]  [4]  [3]  [2]  [1] \n";
    cout<<"\n Every correct answer moves the car above closer to the bank. \n One mistake and the car stops collecting points!\n The game does not end until question 10. \n Get 2 cars to the bank to win the game!\n     ";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    getch();
    int helper;
    clrscr();
    cout<<"\n лRead the help file again? (1) Play the game (2) or Exit the program? (3) - ";
    cin>>helper;
    if (helper==1){
    goto againz;}
    else if (helper==2){
    ;                  }
    else{
    return 0;}
    
    
    
    
    
    
    
    
    clrscr();
    int lose=0,finals=0,mox=0;
    char choice;
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                                    ";
    cout<<"\n     лл Џ "<<points<<"KЎ                                                        бЪЪб  ";
    cout<<"\n     ллллллллллл                                                          ";
    cout<<"\n     ллллБББлллл                                                          ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                                              [1] \n";
    cout<<"\t Which insect shorted out an early supercomputer\n \t   and inspired the term 'Computer bug'?\n\n";
    cout<<"\tA: Stag beetle\tB:Roach\n\tC: Giraffe\tD:Moth\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if ((choice=='d')|| (choice=='D')) {
        points+=100; }
    else{
        lose+=1;
    
    
        mox+=1;
        }
    
    
    clrscr();
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                  бЪЪб          ";
    cout<<"\n     лл Џ "<<points<<"KЎ                                                          ";
    cout<<"\n     ллллллллллл                                                          ";
    cout<<"\n     ллллБББлллл                                                          ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                                          [2]  [1] \n";
    cout<<"\tWhich of the following men does not have\n\t a chemical element named after him?\n\n";
    cout<<"\tA: Albert Einstein\tB:Niels Bohr\n\tC: Isaac Newton\t\tD:Enrico Fermi\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    int max=0;
    if (lose==1){
        ;}
    else if ((choice=='c')|| (choice=='C')) {
        points+=100;
        }
    else {
        lose+=1;
        }
    clrscr();
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                                    ";
    cout<<"\n     лл Џ "<<points<<"KЎ                                                          ";
    cout<<"\n     ллллллллллл                                                          ";
    cout<<"\n     ллллБББлллл                                 бЪЪб                 ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                                      [3]  [2]  [1] \n";
    cout<<"\tAccording to the Population Reference Bureau, what is the approximate\n\t number of people who have ever lived on Earth?\n\n";
    cout<<"\tA: 50 Billion\tB:100 Billion\n\tC: 1 Trillion\tD:5 Trillion\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='b')|| (choice=='B')) {
        points+=100;    
        }
    else{
        lose+=1;}
    clrscr();
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                                    ";
    cout<<"\n     лл Џ "<<points<<"KЎ                                                          ";
    cout<<"\n     ллллллллллл                         бЪЪб                             ";
    cout<<"\n     ллллБББлллл                                                  ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                                     [4]  [3]  [2]  [1] \n";
    cout<<"\tWhich of these zodiac signs is not represented\n\t by an animal that grows horns?\n\n";
    cout<<"\tA: Taurus\tB: Capricorn\n\tC: Cancer\tD: Aries\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='c')||(choice=='C')) {
        points+=100;    
        }
    else{
        lose+=1;    }
    clrscr();
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                                    ";
    cout<<"\n     лл Џ "<<points<<"KЎ              бЪЪб                                        ";
    cout<<"\n     ллллллллллл                                                      ";
    cout<<"\n     ллллБББлллл                                                  ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                                [5]  [4]  [3]  [2]  [1] \n";
    cout<<"\tThe young of which creature is known as a squab?\n\n";
    cout<<"\tA: Octopus\tB: Pidgeon\n\tC: Raven\tD: Eagle\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='b')|| (choice=='B')) {
        points+=100;
        }
    else{
        lose+=1;
        }
    clrscr();
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                                    ";
    cout<<"\n     лл Џ "<<points<<"KЎ              бЪЪб                             бЪЪб       ";
    cout<<"\n     ллллллллллл                                                      ";
    cout<<"\n     ллллБББлллл                                                  ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                           [6]  [5]  [4]  [3]  [2]  [1] \n";
    cout<<"\tComplete the title of the James Bond film Sky...\n\n";
    cout<<"\tA: High\t\tB: Bullet\n\tC: Falling\tD: Fall\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='d')|| (choice=='D')) {
        points+=100;
        
        }
    else{
        lose+=1;}
    clrscr();
    cout<<"\n      ллллллллл                                                           ";
    cout<<"\n     лл                                                      бЪЪб          ";
    cout<<"\n     лл Џ "<<points<<"KЎ              бЪЪб                                        ";
    cout<<"\n     ллллллллллл                                                      ";
    cout<<"\n     ллллБББлллл                                                  ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                      [7]  [6]  [5]  [4]  [3]  [2]  [1] \n";
    cout<<"\tIn basketball, a shot made outside of the arc\n\t is worth how many points?\n\n";
    cout<<"\tA: 1\tB: 3\n\tC: 2\tD: 4\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='b')|| (choice=='B')) {
        points+=100;
        }
    else{
        lose+=1;    }
    clrscr();
    cout<<"\n      ллллллллл                                    бЪЪб                   ";
    cout<<"\n     лл                                                                ";
    cout<<"\n     лл Џ "<<points<<"KЎ              бЪЪб                                        ";
    cout<<"\n     ллллллллллл                                                      ";
    cout<<"\n     ллллБББлллл                                                  ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                                 [8]  [7]  [6]  [5]  [4]  [3]  [2]  [1] \n";
    cout<<"\tAccording to Arthurian legend, where did Sir Bedivere\n\t throw the Excalibur when King Arthur died?\n\n";
    cout<<"\tA: The Lake\tB: The Mountain\n\tC: The forest\tD: Through Arthur\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='a')|| (choice=='A')) {
        points+=100;
        }
    else{
        lose+=1;}
    clrscr();
    cout<<"\n      ллллллллл                                                       ";
    cout<<"\n     лл                                                                ";
    cout<<"\n     лл Џ "<<points<<"KЎ     бЪЪб                                             ";
    cout<<"\n     ллллллллллл                                                      ";
    cout<<"\n     ллллБББлллл                бЪЪб                           ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                            [9]  [8]  [7]  [6]  [5]  [4]  [3]  [2]  [1] \n";
    cout<<"\tHow many consoles has Sony Playstation\n\tsold over 2015?\n\n";
    cout<<"\tA: 15 Million\tB: 50 Million\n\tC: 20,000\tD: 20 Million\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='d')|| (choice=='D')) {
        points+=100;
        }
    else{
        lose+=1;}
    clrscr();
    cout<<"\n      ллллллллл                                                       ";
    cout<<"\n     лл                                                                ";
    cout<<"\n     лл Џ "<<points<<"KЎ                                                      ";
    cout<<"\n     ллллллллллл  бЪЪб                                                ";
    cout<<"\n     ллллБББлллл      бЪЪб                                     ";
    cout<<"\n-------------------------------------------------------------------------------";
    cout<<"\n                       [10]  [9]  [8]  [7]  [6]  [5]  [4]  [3]  [2]  [1] \n";
    cout<<"\tThe last letter of the Greek Alphabet is...?\n\n";
    cout<<"\tA: Sigma\tB: Omega\n\tC: Alpha\tD: Centauri\n";
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t   [TYPE [A] or [B] or [C] or [D] only!] ";
    cin>>choice;
    if (lose>0){
        ;
    }
    else if ((choice=='a')|| (choice=='A')) {
        points+=100;
        }
    else{
        lose+=1;}
    cout<<"\t\t\t[Tallying score press ENTER]";
    getch();
    clrscr();
    
    
    cout<<"Let's see your final score... ";
    getch();
    cout<<"\nDid you win 1,000,000$ ?";
    getch();
    cout<<"\n\t           ллллл                  ллллллл   ллллллл        ";
    cout<<"\n\t          лллллл                  ллллллллллллллллл       ";
    cout<<"\n\t         ллллллл                  ллллл ллллл ллллл        ";
    cout<<"\n\t            лллл                  ллллл  ллл  ллллл        ";
    cout<<"\n\t            лллл                  ллллл       ллллл       ";
    cout<<"\n\t            лллл                  ллллл       ллллл           ";
    cout<<"\n\t            лллл                  ллллл       ллллл            ";
    cout<<"\n\t            лллл                  ллллл       ллллл       ????    ";
    cout<<"\n\t            лллл                  ллллл       ллллл      ?  ???    ";
    cout<<"\n\t            лллл                  ллллл       ллллл          ??     ";
    cout<<"\n\t            лллл                  ллллл       ллллл         ??       ";
    cout<<"\n\t            лллл                  ллллл       ллллл       ??       ";
    cout<<"\n\t            лллл                  ллллл       ллллл              ";
    cout<<"\n\t           ллллл                  ллллл       ллллл            ";
    cout<<"\n\t         ллллллллл                ллллл       ллллл     ??       ";
    getch();
    if (points==1000){
    cout<<"л\n\t\t\t  $ ONE MILLION DOLLARS $ бЪЪб";
    getch();
    cout<<"\n \t    Congratulations on reaching the highest score possible. \n\t\t\t   Thank you for playing! :)";}
    else{
        cout<<"л\n\t\t\t      You only won "<<points<<"K! бЪЪб";
        getch();
        cout<<"\n\t You did not win the game, but you managed to bring home "<<points<<"K\n\t\t\t     Better luck next time!";}
    
    
    getch();
    clrscr();
    cout<<"\n     _____________________________________________________________________       ";
    cout<<"\n    |                                                                      |     ";
    cout<<"\n    |  =================================================================== |     ";
    cout<<"\n    | |\\\\\=-=-=-=-=-=-=-=-{Final programming exam}-=-=-=-=-=-=-=-=/////| ||||     ";
    cout<<"\n    | |///\)===============------------------------================(/\\\\| ||||     ";
    cout<<"\n    | |////\                ))---PLAY FOR UP TO --((                /\\\\| ||||     ";
    cout<<"\n    | |)|//(================/ ONE  Million DOLLARS \================)|//(| ||     ";
    cout<<"\n    | |{===}=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-{===}| |     ";
    cout<<"\n    | ==================================================================== |     ";
    cout<<"\n    |______________________________________________________________________|     ";
    cout<<"\n\n\t\t бЪЪб Would you like to play again? ";
    cout<<"\n\t    (1) RETURN TO TITLE SCREEN (2) READ INSTRUCTIONS (3) EXIT  л ";
    int champ;
    cin>>champ;
        if (champ == 1)
        {
            goto beginning;
        }
    
    
        else if (champ == 2)
        {
            goto againz;
        }
    
    
        else if (champ == 3)
        {
            return 0;
        }
    
    
    
    
    getch();
    }

  2. #2
    Registered User Vendredi's Avatar
    Join Date
    Mar 2015
    Posts
    3
    So here are my questions:
    1. When converting this Console text to GUI, how would I go about doing that? What would be my first steps?
    2. Instead of having static images of these ASCII symbols, how could I simulate movement? This was particularly troublesome.
    3. Why is it that when I try to follow basic tutorials on youtube in doing C++ stuff, I can't emulate the code directly into my obsolete IDE? I was told that the libraries do not exist in the older one, in that case; How do I make it work?
    4. What should I spend time to learn on that might improve this code wall that I have clearly not used? Arrays. Switches, Inheritance, classes, reference etc. (We haven't learned any of these in class, so I have no idea how to inject them)
    5. ...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Before you proceed further, you need to:
    • Indent your code properly.
    • Write functions that do one thing and do it well. Basically, break up your main function into smaller functions that handle different parts of what the main function does.
    • Make use of loops (and/or functions) instead of using goto with labels.


    I presume that this:
    Code:
    #include<iostream.h>#include<conio.h>
    is actually:
    Code:
    #include<iostream.h>
    #include<conio.h>
    Note that <iostream.h> is pre-standard; the standard header is <iostream>, but due to your unusual circumstances you may need to stick to <iostream.h>. You can try using <iostream>, then place using namespace std; after including the headers and before the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2014
    Posts
    121
    Quote Originally Posted by laserlight View Post
    I presume that this:
    It's a bug that happens when you post code here.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to learn more, you could read the book Accelerated C++. Granted, most of the stuff probably won't work in that outdated pre-standard IDE, but if you want to learn real C++ that's used in the real world, you're going to have to dump Turbo C++ - there's no way around that.
    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
    May 2014
    Posts
    121
    Why do a lot of professors insist on using old and outdated software and language standards? Is it because they don't want to put any effort in staying up to date? I would've complained about the course to my university if I had a professor that taught a programming course like this.

  7. #7
    Registered User
    Join Date
    Jul 2014
    Location
    Calgary, Canada
    Posts
    29
    Quote Originally Posted by MOS-6581 View Post
    Why do a lot of professors insist on using old and outdated software and language standards? Is it because they don't want to put any effort in staying up to date?
    At research universities, professors get promoted based on published research papers, and the ability to bring in research money -- *not* on teaching ability.

    Also, preparing lectures is vastly more time consuming than most people realize. I taught a few courses at a university a number of years back, and it regularly took me 8-10 hours to put together a one-hour lecture. From talking to senior professors, I learned that this was not unusual.

    That being the case, the temptation to re-use existing notes year after year is very difficult to resist, especially when you are desperately trying to get two more papers into review before your next grant application is due.

    As regards the OP's question: The only suggestion I can make is that you study the legacy code and learn exactly what it is that you hate about it. Then you can purge all elements of it from your own coding style once you are done with this course.

    Other programmers will thank you for this, as legacy code is a burden on all of us, and writing brand-new legacy code is unforgivable. I work in an industry where this is common (we still have people who write only in Fortran) and it is very frustrating.

  8. #8
    Registered User Vendredi's Avatar
    Join Date
    Mar 2015
    Posts
    3
    Quote Originally Posted by Grumpulus View Post
    At research universities, professors get promoted based on published research papers, and the ability to bring in research money -- *not* on teaching ability.

    Also, preparing lectures is vastly more time consuming than most people realize. I taught a few courses at a university a number of years back, and it regularly took me 8-10 hours to put together a one-hour lecture. From talking to senior professors, I learned that this was not unusual.

    That being the case, the temptation to re-use existing notes year after year is very difficult to resist, especially when you are desperately trying to get two more papers into review before your next grant application is due.

    As regards the OP's question: The only suggestion I can make is that you study the legacy code and learn exactly what it is that you hate about it. Then you can purge all elements of it from your own coding style once you are done with this course.

    Other programmers will thank you for this, as legacy code is a burden on all of us, and writing brand-new legacy code is unforgivable. I work in an industry where this is common (we still have people who write only in Fortran) and it is very frustrating.
    Thanks Laserlight, My code must look terrible..

    I'm itching to do just that; I'll also take Elysia up on the offer and read the book mentioned until I finish this course. Something tells me that the value of those few dollars I'm going to hand over for that book is going to be worth much more than this course has for me wholesomely, so far in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code architecture: A call for advice
    By Median in forum Tech Board
    Replies: 36
    Last Post: 10-15-2014, 08:45 AM
  2. Replies: 3
    Last Post: 10-18-2012, 05:36 AM
  3. Problem with code. Need advice.
    By sdaniels1288 in forum C++ Programming
    Replies: 1
    Last Post: 05-31-2011, 04:01 AM
  4. Refining pointer knowledge but stumped
    By Imanuel in forum C Programming
    Replies: 4
    Last Post: 07-17-2010, 10:02 AM
  5. Help with Refining the set_union function in C++
    By hay_man in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 12:05 AM

Tags for this Thread