Page 1 of 2 12 LastLast
Results 1 to 25 of 38

Thread: My C++ programs

  1. #1
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default My C++ programs

    Hey, guys. I've started learning C++. (Just started tonight, actually)

    Anyway, I've made a little program, and I though I might share it, get some CnC, and will add on to it, and post the updates here.

    At the moment, when you run the executable, and you enter in the numbers, after you press enter twice, it closes. I'm looking into a fix for this. If you have one, lemme know, please!



    Files:

    Area finder. You type in the parameters, and it gives you the answer. It's a glorified multiplication calculator.


    Source: http://paste.villavu.com/show/3806/
    Source: http://pastebin.com/FHS32ujV

  2. #2
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

    Try not to do this:
    cout << "\n" << endl;
    cout << "\n" << endl;

    You also don't need a endl on the end of every cout. Just put a \n at the end of your last one.

  3. #3
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Nice I also just started learning c++

    Like yesterday and today were my first lectures, something I whipped up:

    C++ Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;

    void Add()
    {
         double a, b, c;
         cout << "Enter the first number" << endl;
         cin >> a;
         cout << "Enter the second number" << endl;
         cin >> b;
         c = a + b;
         cout << a;
         cout << " + ";
         cout << b;
         cout << " is equal to: ";
         cout << c << endl;
         Sleep(5000);
    }  

    void Subtract()
    {
         double a, b, c;
         cout << "Enter the first number" << endl;
         cin >> a;
         cout << "Enter the second number" << endl;
         cin >> b;
         c = a - b;
         cout << a;
         cout << " - ";
         cout << b;
         cout << " is equal to: ";
         cout << c << endl;
         Sleep(5000);
    }

    void Multiply()
    {
         double a, b, c;
         cout << "Enter the first number" << endl;
         cin >> a;
         cout << "Enter the second number" << endl;
         cin >> b;
         c = a * b;
         cout << a;
         cout << " * ";
         cout << b;
         cout << " is equal to: ";
         cout << c << endl;
         Sleep(5000);
    }

    void Divide()
    {
         double a, b, c;
         cout << "Enter the first number" << endl;
         cin >> a;
         cout << "Enter the second number" << endl;
         cin >> b;
         c = a / b;
         cout << a;
         cout << " / ";
         cout << b;
         cout << " is equal to: ";
         cout << c << endl;
         Sleep(5000);
    }  

    int main()
    {
        int input;

        cout << "What operation to perform (Add(1), Subtract(2), Multiply(3), Divide(4))" << endl;
        cin >> input;
        switch ( input ) {
        case 1:
             Add();
             break;
        case 2:
             Subtract();
             break;
        case 3:
             Multiply();
             break;
        case 4:
             Divide();
             break;
        default:        
             cout << "Error, bad input, quitting\n";
             break;
       
        }
       
    }

    Edit: I see you Brandon, any pointers?
    Last edited by Gucci; 01-12-2013 at 07:13 PM.
    Current Project: Retired

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    @OP and guy above, you're both missing error checking so if someone presses enter or doesn't enter anything, your program will get bad input and break.. If they enter letters, it will also break..

    For now, pretty good for starters. Here's a tip. When I use strings and my std::cout statement ends in a string, I tend to use \n for new line.

    If my statement doesn't end with a string, I use std::endl.

    See below:

    C++ Code:
    #include <iostream>

    void Welcome()
    {
        std::cout<<"Welcome to Vinyl Scratch's AreaFinder!\n";  //Ends in quotes..
        std::cout<<"It's my first C++ program, so I wanted to see if you guys liked it,\nand if I did well.\n\n\n"; //String..
    }


    int main()
    {
        Welcome();
        unsigned long Width = -1, Height = -1;

        std::cout<<"Enter Width: ";
        std::cin>> Width;

        if (!std::cin.good())   //If we get bad input.. Print an error and return 1.
        {
            std::cout<<"Error.. Invalid Input\n";
            return 1;
        }

        std::cout<<"\nEnter Height: ";
        std::cin>> Height;

        if (!std::cin.good())
        {
            std::cout<<"Error.. Invalid Input\n";
            return 1;
        }

        std::cout<<"The Area is: "<<Width * Height<<std::endl;  //Doesn't end in a string.. Used std::endl.
        std::cin.get();

        return 0;  //Everything went fine. Return 0..
    }
    Last edited by Brandon; 01-12-2013 at 04:41 AM.
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    ^ could you explain the if loop? And what's std?
    Current Project: Retired

  6. #6
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    Yeah, What Gucci said.

    But I understand most of it. I didn't know what that /n did, so I just used endl.

  7. #7
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    std is a standard namespace.

    If you don't do using namespace std; then you need to type std:: before everything within that namespace.

    Ex:

    C++ Code:
    using namespace std;

    cout<<"Foo"<<endl;

    string meh = "Testing";

    Without the using, it'd be:

    C++ Code:
    std::cout<<"Foo"<<std::endl;

    std::string Meh = "Just Testing";

    So on and so forth. A namespace basically stops confusion/ambiguity and groups stuff together.

    Lets say you have two classes called "string". Lets say that the first string class resides in a namespace std and that the second one resides in namespace foo.


    If you do something like:

    C++ Code:
    using namespace std;
    using namespace foo;

    string Meh = "Testing std";
    string Bleh = "Testing foo";

    The above will give an error because the compiler will not know which string you are referring to. Why? Because you're using both namespaces and both of them have the same class.

    Now if you did:

    C++ Code:
    std::string Meh = "Testing std";
    foo::string Bleh = "Testing foo";

    The compiler now knows that the first line was referring to the string class within the namespace std group. The second line refers to the string class which is within the foo namespace/group.

    That's all namespaces are used for. I tend to use the scope operator.. aka :: a lot because I once wrote my own library and had a lot of classes named the same thing as the ones already there.. Namespaces saved my ass so I use them lots lol. There usually is almost always no point to using them as much as I do, but it makes good practice I guess.

    For beginning though, it's fine to ignore them. You usually tend to use them only if you think there is ambiguity or confusion.


    Now for the cin.good(). Lets say you asked the user to enter a number.. But no, our user is a jerk and tries to break everything and crash our program. They decide to enter a letter or nothing at all. This sets a badbit flag in cin. Aka the Input stream. cin.good() lets you know if that flag is NOT set. I checked if the flag is set by doing:

    If (Not good input) then show a message.

    That's all.
    Last edited by Brandon; 01-12-2013 at 05:18 AM.
    I am Ggzz..
    Hackintosher

  8. #8
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    OHH. I understand, now! Thanks, Brandon. I'll start work on a new on tomorrow!

  9. #9
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

    Going to make an Algebra Calculator for the fun of it later.

  10. #10
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    If somebody enters a bad value aka letter/ not 0-4 then won't this:

    Code:
    default:         
             cout << "Error, bad input, quitting\n";
             break;
    Output an error? Also how do you quote C++ code as C++ Code?
    Current Project: Retired

  11. #11
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Gucci View Post
    If somebody enters a bad value aka letter/ not 0-4 then won't this:

    Code:
    default:         
             cout << "Error, bad input, quitting\n";
             break;
    Output an error? Also how do you quote C++ code as C++ Code?
    Your switch case is fine.. But imagine something like this:

    C++ Code:
    #include <iostream>

    int main()
    {
        int X = -1;

        std::cout<<"Enter a number: ";
        std::cin>> X;

        switch(X)
        {
            case 0: std::cout<<"\n0 Entered"; break;
            case 1: std::cout<<"\n1 Entered"; break;
            case 'a': std::cout<<"\na Entered"; break;
            default: std::cout<<"Default.."; break;
        }

        return 0;
    }

    Anything invalid that is entered will default to 0. Ex: If the user enters a, it will print: "0 Entered". So if your case statement counted 0, it'd be broken. Also note that it doesn't go to the 'a' case-statement Since your code doesn't count 0, it is fine.

    To do Cpp highlighting:

    Highlight=C++
    /Highlight

    Put that in square brackets:

    [.Highlight=C++]
    [./Highlight]

    Without the dots.
    Last edited by Brandon; 01-12-2013 at 06:47 AM.
    I am Ggzz..
    Hackintosher

  12. #12
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Ah I see what you mean, if I was counting 0 as a possible input for the case then if someone used anything not defined in the case it would execute this:

    C++ Code:
    case 0:
      The procedure ();
      Break;
    Current Project: Retired

  13. #13
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    Oh, I see why it wouldn't work! I'll change it now.

  14. #14
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    If you haven't already, you might want to try my userscript for making the font in code-related tags monospaced (which I forgot to apply, when trying to read Brandon's examples ).

    EDIT: Vinyl Scratch, \n means to create a newline character. This is sometimes coupled with \r, especially when writing plain-text to files.
    Last edited by Daniel; 01-12-2013 at 01:33 PM.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  15. #15
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

  16. #16
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Vinyl Scratch View Post
    Thanks, Daniel!


    So, what does \r mean?

    \r means carriage return. This one tells the carret/cursor to go to the beginning of the line. Can be used to replace text.
    \n means new line.

    The difference? \r\n is new line on windows. \r is new line on macs. \n is new line on linux.

    Well this is true afaik/learned. For compatibility reasons, when coding, I always use \r\n. If on macs, it will ignore the \n. On windows it uses both. On linux, it ignores the \r.

    I think that's how it work or that's what I learned when I first started as well.

    Also, for compatibility reason as well, Sockets require you to do \r\n so that no matter what system you're on, it registers new lines and separates the header from the body in HTML files as well as it signifies the end of a socket command during the request phase.


    In a nutshell, it's best to just use \r\n but if you know that you're only going to be using windows/nix, using \n alone is pretty safe.


    In Simba, a new line is \r\n which translates to #13#10 in bytes. You know this because those are the ascii values for them: http://www.asciitable.com/

    Basically, \r goes down one line and to the beginning of the new line and \n just goes down one line with the caret in the same position. This happens only on some old systems though. I believe they both basically do the same thing now except it's platform specific.
    Last edited by Brandon; 01-12-2013 at 07:22 PM.
    I am Ggzz..
    Hackintosher

  17. #17
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    Thanks, Brandon. I'll just start using \r\n, to be on the safe side.

    Working on my updated program right now!

  18. #18
    Join Date
    Mar 2006
    Posts
    795
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    are u programming c++ in windows if so how im pretty noob when it comes to visual i learnt in ubuntu and its really easy to compile using terminal and g++ commands



    Dont Steal..

  19. #19
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Quote Originally Posted by SubiN View Post
    are u programming c++ in windows if so how im pretty noob when it comes to visual i learnt in ubuntu and its really easy to compile using terminal and g++ commands
    I've been using g++ or Dev C++, if you want a link to download g++ pm me
    Current Project: Retired

  20. #20
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    I'll be sure to do that once I get my Linux Distro.


    UPDATE! YAYY.

  21. #21
    Join Date
    Mar 2006
    Posts
    795
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    hmm this always bothered me does visual use an inbuilt g++ compiler or is it different if it is different what are the differences



    Dont Steal..

  22. #22
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    I couldn't tell you, I'm still new to C++. You'd have to ask Brandon that.

  23. #23
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by SubiN View Post
    hmm this always bothered me does visual use an inbuilt g++ compiler or is it different if it is different what are the differences
    Why would Visual Studio use G++..?

    And the GCC/G++ Windows counterpart is called MinGW.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  24. #24
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Gucci View Post
    I've been using g++ or Dev C++, if you want a link to download g++ pm me
    I thought the latest dev-C++ was since 07 or something.. 4.990/5.0 beta? Then.. I looked it up, turns out there is a new developer who started to continue it this year :l
    Didn't even know this was happening till I saw your post.

    I hope you meant this version of it: http://orwelldevcpp.blogspot.ca/


    I personally use Codeblocks. Dev-C++ was back in the day and was my favourite IDE for a year or two. The best IDE that existed too. Now, my favourite is definitely without a doubt, Codeblocks.

    http://www.codeblocks.org/downloads/26


    I'm going to download that Dev-C++ and see how it is now. If it's as good as back then, I'm switching lol.

    Visual Studio 2012 isn't bad either. It's actually pretty good. Though, it doesn't support half as many C++-x11 features as Mingw IDE's (Codeblocks/Dev-C++) or Clang/Intel..

    VS2012's only advantage is the C++x11 Regex.. That's it. Other than that, your backend compiler should be g++/Mingw 4.7.2+.


    Difference between VS2012 and Mingw? Well.. Two different compilers/backends. Mingw supports way more crossplatform stuff and more languages as well as the latest features whereas VS2012 is pure crap atm if it isn't Visual-C++ which is the .Net framework..


    EDIT: I'm going to have to stick with codeblocks.. Dev-C++ just doesn't live up to its name for me anymore :c
    Last edited by Brandon; 01-15-2013 at 06:15 AM.
    I am Ggzz..
    Hackintosher

  25. #25
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Yeah that's the one
    Current Project: Retired

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •