[C++]Newest code.

Results 1 to 13 of 13
  1. #1
    Super Mexican Joser is offline
    MemberRank
    Jun 2008 Join Date
    Your mom's roomLocation
    1,655Posts

    [C++]Newest code.

    Made a simple little code as a project for my "teacher".
    It's a simple calculator.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        char response;
        int num1;
        int num2;
        
        cout << "Welcome to Jose's C++ Calculator.\n";
        cout << "Please enter the first number of you problem:\n";
        cin >> num1;
        cout << endl;
        cout << "You can now pick either +, -, /, or x.\n";
        cout << "Now enter the operation that you'd like to perform:\n";
        cin >> response;
        cout << endl;
        cout << "Now please enter the second number of your problem:\n";
        cin >> num2;
        cout << endl;
        
        if (response == '+'){
           cout << num1 << " + " << num2 << " = " << num1+num2 << endl ;         
           };
        if (response == '-'){
           cout << num1 << " - " << num2 << " = " << num1-num2 << endl ;          
           };                 
        if (response == 'x'){
           cout << num1 << " x " << num2 << " = " << num1*num2 << endl ;         
           };    
        if (response == '/'){
           cout << num1 << " / " << num2 << " = " << num1/num2 << endl ;         
           };  
           
         cout << "Type anything and press enter to exit." << endl;
           char f;
           cin >> f;
           return 0;
    }
    C.C and suggestions would be helpful. Thanks alot guys.
    ~Joser


  2. #2
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++]Newest code.

    1. Don't use the using keyword (at least I have good reasons why I don't use it)
    2. Concatenate strings, to prevent multiple calls to cout right after each other (bad)
    3. Use a switch for the response thingy (faster, generally, and better code structure)
    4. Put definitions on top of the function (char f)
    5. Use a space between math logic functions (easier to identify from a distance when looking at the code)
    6. Use meaningful variable names (helps A LOT, together with commenting)

    Rest looks good :)

    I would do it this way:

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        char f, operation;
        int number1, number2;
        
        std::cout << "Welcome to Jose's C++ Calculator.\nPlease enter the first number of you problem:\n";
        std::cin >> number1;
        std::cout << "\nYou can now pick either +, -, /, or x.\nNow enter the operation that you'd like to perform:\n";
        std::cin >> operation;
        std::cout << "\nNow please enter the second number of your problem:\n";
        std::cin >> number2;
        std::cout << '\n' << number1 << ' ' << operation << ' ' << number2 << " = ";
        
        switch(operation)
        {
            case '+':
            {
                std::cout << (number1 + number2);
                break;
            }
            case '-':
            {
                std::cout << (number1 - number2);
                break;
            }
            case '*':
            {
                std::cout << (number1 * number2);
                break;
            }
            case '/':
            {
                std::cout << (number1 / number2);
                break;
            }
        }    
           
        std::cout << "\nType anything and press enter to exit.\n";
        std::cin >> f;
        return 0;
    }
    But use your own indentation style or general coding style :).

  3. #3
    Super Mexican Joser is offline
    MemberRank
    Jun 2008 Join Date
    Your mom's roomLocation
    1,655Posts

    Re: [C++]Newest code.

    Thanks alot for the advice Daevius!
    Helped me alot. ^^

  4. #4
    Ragezone OG FrostElite is offline
    MemberRank
    Sep 2008 Join Date
    United StatesLocation
    1,880Posts

    Re: [C++]Newest code.

    printf is much less confusing than cout

  5. #5
    Super Mexican Joser is offline
    MemberRank
    Jun 2008 Join Date
    Your mom's roomLocation
    1,655Posts

    Re: [C++]Newest code.

    Quote Originally Posted by FrostElite View Post
    printf is much less confusing than cout
    printf ?? Can you link me to something please?

  6. #6
    Learning. lordvladek is offline
    MemberRank
    Mar 2006 Join Date
    872Posts

    Re: [C++]Newest code.

    Printf is C's cout <<.

    printf("Lalala");

    Just as scanf is C's cin >>.

  7. #7
    Super Mexican Joser is offline
    MemberRank
    Jun 2008 Join Date
    Your mom's roomLocation
    1,655Posts

    Re: [C++]Newest code.

    Quote Originally Posted by lordvladek View Post
    Printf is C's cout <<.

    printf("Lalala");

    Just as scanf is C's cin >>.
    Ahhh I see.
    But im using C++ soo...

    Can I stilll use that?? Or not??

  8. #8
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++]Newest code.

    Yes you can. C++ is a subset of C, C with some extra functionality (well...some? A lot :-)). Sometimes it's best to use the C way, but then you really go for efficiency at close scale, for now, just do it the C++ way :).

  9. #9
    Super Mexican Joser is offline
    MemberRank
    Jun 2008 Join Date
    Your mom's roomLocation
    1,655Posts

    Re: [C++]Newest code.

    Quote Originally Posted by Daevius View Post
    Yes you can. C++ is a subset of C, C with some extra functionality (well...some? A lot :-)). Sometimes it's best to use the C way, but then you really go for efficiency at close scale, for now, just do it the C++ way :).
    Thanks for the advice. I understand what you mean.
    Youve helped alot man Thanks alot ^^

  10. #10
    Λκαяυz føяeνeя Roamer is offline
    MemberRank
    Jun 2008 Join Date
    215Location
    3,203Posts

    Re: [C++]Newest code.

    Heres a personal calculator i made,
    <form name="Calc">
    <table border=4 bordercolorlight="#FF9900" bordercolordark="#FF6633">
    <tr>
    <td>
    <div align="center"> <font size="+1">
    <input type="text" name="Input" size="16">

    </font></div>
    </td>
    </tr>
    <tr>
    <td height="111">
    <div align="center">
    <input type="button" name="one" value=" 1 " onClick="Calc.Input.value += '1'">
    <input type="button" name="two" value=" 2 " onClick="Calc.Input.value += '2'">
    <input type="button" name="three" value=" 3 " onClick="Calc.Input.value += '3'">
    <input type="button" name="plus" value=" + " onClick="Calc.Input.value += ' + '">

    <br>
    <input type="button" name="four" value=" 4 " onClick="Calc.Input.value += '4'">
    <input type="button" name="five" value=" 5 " onClick="Calc.Input.value += '5'">
    <input type="button" name="six" value=" 6 " onClick="Calc.Input.value += '6'">
    <input type="button" name="minus" value=" - " onClick="Calc.Input.value += ' - '">
    <br>
    <input type="button" name="seven" value=" 7 " onClick="Calc.Input.value += '7'">
    <input type="button" name="eight" value=" 8 " onClick="Calc.Input.value += '8'">
    <input type="button" name="nine" value=" 9 " onClick="Calc.Input.value += '9'">

    <input type="button" name="times" value=" x " onClick="Calc.Input.value += ' * '">
    <br>
    <input type="button" name="clear" value=" Clear " onClick="Calc.Input.value = ''">
    <input type="button" name="zero" value=" 0 " onClick="Calc.Input.value += '0'">
    <input type="button" name="div" value=" / " onClick="Calc.Input.value += ' / '">
    </div>
    </td>
    </tr>
    <tr>
    <td>
    <div align="center">
    <input type="button" name="DoIt" value=" = " onClick="Calc.Input.value = eval(Calc.Input.value)">

    </div>
    </td>
    </tr>
    </table>

    <div align="right"><br>
    </div>
    <p>&nbsp;</p>
    </form>

  11. #11
    Account Upgraded | Title Enabled! Dinosauru is offline
    MemberRank
    Mar 2008 Join Date
    Canada?Location
    610Posts

    Re: [C++]Newest code.

    Quote Originally Posted by Daevius View Post
    2. Concatenate strings, to prevent multiple calls to cout right after each other (bad)
    Isn't this for preventing multiple calls to cout right after each other?
    Code:
    using namespace std;

  12. #12
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++]Newest code.

    Quote Originally Posted by Roamer View Post
    Heres a personal calculator i made,
    That's HTML, not C++.

    Quote Originally Posted by Dinosauru View Post
    Isn't this for preventing multiple calls to cout right after each other?
    Code:
    using namespace std;
    No, the using keyword is to make code more readable, to prevent having to say std:: each time. However, for nested namespaces I suggest you define a new namespace (as simple as 'namespace new_name = current_name;') to be a nested one.

    The difference of calling cout instead of using the '<<' operator, is just a little overhead. However, using endl too often can be more overhead. Each endl makes the buffer to be drawn on screen directly (drawing on screen is intensitive!), while it's better, CPU wise, to wait till the buffer is filled (or till a certain time limit is reched...).

    It's all just minimal, but it's good practise :)

  13. #13
    Super Mexican Joser is offline
    MemberRank
    Jun 2008 Join Date
    Your mom's roomLocation
    1,655Posts

    Re: [C++]Newest code.

    Thanks for the help Deavius. I'll try loops and stings next time ^^



Advertisement