Thread: feeling proud.

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    21

    feeling proud.

    i know this is jack poop to all you expert programmers out there, but I just wanted to share my last 45 minutes.. the real reason I feel good about this code i wrote is because i did the whole thing without referencing back to a tutorial, or an example (shows I'm learning something) its definitely the longest code I've written off the top of my head (even though it is SUPER short compared to an actual useful code).
    also I'd like to thank the people on the forums, because I was struggling with the concept of using functions besides main, but now I've got it.. I would gladly take any tips on improving this code or tips in general! ahhh my first REAL written program. it DOES feel good

    I am having trouble thinking of a way for it to loop back to main after either option is finished.. (besides exit)


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int calculator ();
    int bottles_of_beer ();
    int exit ();
    ///////////////////////CALCULATOR FUNCTIONS
    double add(double x, double y)
    {
        return x + y;
    }
    double sub(double x, double y)
    {
        return x - y;
    }
    double multiply(double x, double y)
    {
        return x * y;
    }
    double divide (double x, double y)
    {
        return x / y;
    }
    
    
    ////////////////////////MAIN
    int main ()
    {
        int choice;
        cout<<"This is a menu. please choose an option. (enter 1-3)\n";
        cout<<"1.calculator\n";
        cout<<"2.display 99 bottles of beer.\n";
        cout<<"3.exit.\n";
        cin>> choice;
        cin.ignore();
        if (choice == 1)
        {
        cout<< calculator();
        
        }
        
        else if (choice == 2)
        {
             cout<< bottles_of_beer();
        }
        
        else if (choice == 3)
        {
             int exit;
        }
                   
    }
    
    
    ////////////////////// CALCULATOR FUNCTION
    int calculator ()
    {
         int x=0;
         int y=0;
         int choice;
         
         cout<< "please enter 2 numbers (separated by a space)\n";
         cin>> x >> y;
         cin.ignore();
         cout<< "would you like to- \n";
         cout<< "1.add\n";
         cout<< "2.subtract\n";
         cout<< "3.multiply\n";
         cout<< "4.divide\n";
         cin>> choice;
         cin.ignore();
         
         if (choice == 1)
         {
                    cout<< "answer is " << add (x, y);
         }
         
         else if (choice == 2)
         {
              cout<< "answer is " << sub (x, y);
         }
         
         else if (choice == 3)
         {
              cout<< "answer is " << multiply (x, y);
         }
         
         else if (choice == 4)
         {
              cout<< "answer is " << divide (x, y);
         }
         
         cin.get();
         
    }
    ///////////////////////99 BOTTLES OF BEER FUNCTION
    int bottles_of_beer ()
    {
        int bottle_amount = 99;
        while (bottle_amount > 0)
        {
              if(bottle_amount == 2)
              {
                   cout<< bottle_amount << " bottles of beer on the wall, " << bottle_amount << " bottles of beer!\n";
                   cout<< "take one down, pass it around! " << --bottle_amount << " bottle of beer on the wall!\n\n";
                   continue;
                   }
              else if (bottle_amount == 1)
              {
               cout<< bottle_amount << " bottle of beer on the wall, " << bottle_amount << " bottle of beer!\n";
        cout<< "take one down, pass it around! " << --bottle_amount << " bottles of beer on the wall!\n\n";
        continue;
    }
                                
        cout<< bottle_amount << " bottles of beer on the wall, " << bottle_amount << " bottles of beer!\n";
        cout<< "take one down, pass it around! " << --bottle_amount << " bottles of beer on the wall!\n\n";
    }
    cin.get();
    }
    ///////////////////EXIT FUNCTION
    int exit()
    {
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Very nice.

    I would gladly take any tips on improving this code or tips in general!
    This would be a good opportunity to replace the "if-else" chains with "switch" statements:

    Code:
    switch(choice)
    {
        case 1:
            // execute choice 1
            break;
        case 2:
            // execute choice 2
            break;
        case 3:
            // execute choice 3
            break;
        default:
            // optional code if input is not detected above
            break;
    }

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    When you've implemented a system where a literal has a clear association consider using tables.

    "Table Driven Development" for the win.

    Soma

    Code:
    #include <limits>
    #include <iostream>
    
    double Add
    (
        double fLHS
      , double fRHS
    )
    {
        return(fLHS + fRHS);
    }
    
    double Empty
    (
        double fLHS
      , double fRHS
    )
    {
        return(0.0);
    }
    
    typedef double (*OperationCallback) (double, double);
    
    struct SOperation
    {
        const OperationCallback mCallback;
        const char * mName;
    };
    
    static const SOperation kOperations[] =
    {
        {Add, "Add"}
      , {Empty, "Quit"}
    };
    
    static const int kChoices = (sizeof(kOperations) / sizeof(*kOperations));
    
    static const int kQuit = (sizeof(kOperations) / sizeof(*kOperations)) - 1;
    
    void ShowMenu
    (
        std::ostream & fOut
    )
    {
        for(int cChoice(0); cChoice < kChoices; ++cChoice)
        {
            fOut << cChoice << "): " << kOperations[cChoice].mName << '\n';
        }
    }
    
    int main()
    {
        using namespace std;
        int sSelection(0);
        do {
            double sLHS;
            double sRHS;
            do {
                //Flush(cin);
                ShowMenu(cout);
            } while(!(cin >> sSelection) || (sSelection < 0) || (sSelection > kQuit));
            if(sSelection != kQuit)
            {
                do {
                    //Flush(cin);
                    cout << "Please enter operands (separated by a space): _\b";
                } while(!(cin >> sLHS >> sRHS));
                cout << kOperations[sSelection].mCallback(sLHS, sRHS) << '\n';
            }
        } while(sSelection != kQuit);
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your line 51 does not do what you think it does. It just declares a variable called 'exit' and does not give it a value.
    At it is, you don't even need to check for a case 3.
    It would be a good idea to return a value from main though, say EXIT_SUCCESS.
    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
    0 is returned implicitly, however, so there is no need for an explicit return.
    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
    Jul 2012
    Posts
    21
    Thanks Matticus, Switch statements is next on my list of things to do!!

    Phantomotap- I'm still pretty early on in learning (if you couldn't tell), and while i appreciate you taking the time to write all that code, not ONE bit of it made sense to me.. sorry mate, I'm just not on that level yet.

    also it is a fact that main will return 0 by default, whether it is written or not.

    Your line 51 does not do what you think it does. It just declares a variable called 'exit' and does not give it a value.
    At it is, you don't even need to check for a case 3.
    It would be a good idea to return a value from main though, say EXIT_SUCCESS.
    I spotted a typo on my line 51, I wasn't aware that exit was a keyword for C++, and line 51 should actually read
    Code:
    int exit();
    it was meant to just be a function that returned 0 so it would close the screen. would i have been able to just give the command 'exit' for option 3?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mike Beal
    line 51 should actually read
    Code:
    int exit();
    That would still be incorrect. It should have been a function call:
    Code:
    exit();
    or rather:
    Code:
    return exit();
    But I am nervous naming a function exit in the presence of a using directive for namespace std at file scope since there is std::exit (though std::exit takes an argument while your exit does not). Since all your exit function does is return 0, you might as well directly return 0;
    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

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    I see you're working your way through the Jumping into C++ book! ME TOO! Stick with it man! I'm only on chapter 11 lol, but so far its tons of fun! Best of luck to you, and Nice Code, I know the exact feeling you're having!

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    He is working his way through a book guys, hasn't reached the chapter on switches, or tables, honestly this is great for how far he is in the book...

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    He is working his way through a book guys, hasn't reached the chapter on switches, or tables, honestly this is great for how far he is in the book...
    Who said it wasn't?

    Soma

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    Quote Originally Posted by rTeapot View Post
    I see you're working your way through the Jumping into C++ book! ME TOO! Stick with it man! I'm only on chapter 11 lol, but so far its tons of fun! Best of luck to you, and Nice Code, I know the exact feeling you're having!
    very nice!! thanks! yea i figured "hey, alex guarantees I'll learn C++, or my money back so why the heck not!?" lol it is very fun indeed, it is however kinda frustrating when you have a great idea in mind, but still so much to learn before you could pull it off.. PATIENCE AND DETERMINATION!! glad to hear I'm not the only beginner here
    cheers

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Keep those great ideas written down somewhere. As you improve, and are looking for programs to practice with, you can revisit those ideas. My own greatest challenge when learning the basics of programming was thinking of what I could program for practice that was interesting while being within my capability. If you have a list of ideas on hand, this will be less of a difficulty.

  13. #13
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    good advice thanks. it will be very exciting when i can code the things I WANT to code, i feel like it will be more enjoyable coding it as well, knowing the final product i have in mind

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am feeling down
    By codewriter in forum General Discussions
    Replies: 6
    Last Post: 05-11-2012, 12:04 PM
  2. Not very proud of this Code.
    By caroundw5h in forum C Programming
    Replies: 17
    Last Post: 03-27-2004, 10:45 AM
  3. Very proud of this code.
    By caroundw5h in forum C Programming
    Replies: 10
    Last Post: 01-22-2004, 01:50 AM
  4. One True Proud Canadian
    By DarkViper in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 06:39 PM
  5. Is there something you are proud/glad to not know?
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 10-31-2002, 07:40 AM