google-site-verification: googlebaca44933768a824.html vollhorst's Calculator (first c++ application :) - Old Royal Hack Forum

Announcement

Collapse
No announcement yet.

vollhorst's Calculator (first c++ application :)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    vollhorst's Calculator (first c++ application :)

    vollhorst's Calculator (C++):

    Hi,

    yesterday i started to learn c++ and now i just finished my first c++ calculator:



    Code:
    #include <iostream> 
    
    using namespace std;
    
    
    
    int main (void)
    
    {
    
    	cout << "Calculator" << endl << "by vollhorst" << endl << endl;
    
    	cout << "Please chose an operation:" << endl << endl;
    	cout << "\t 1 for + (addition)" << endl;
    	cout << "\t 2 for - (suntraction)" << endl;
    	cout << "\t 3 for * (multiplication)" << endl;
    	cout << "\t 4 for / (division)" << endl;
    	cout << "\t 5 for % (percent)" << endl << endl;
    	cout << "Operation:";
    	
    	int i1;
    	cin >> i1;
    	
    	
    	
    	cout << endl << "Please enter your first value:"; 
    	
    	double d1;
    	cin >> d1;												// First value for the operation
    
    	cout << endl << "Please enter your second value:"; 
    	
    	double d2;
    	cin >> d2;												// Second value for the operation
    	
    	double d3;
    	
    		
    	if (i1 == 1) d3 = d1 + d2;								// decision which operation should be executed + definition of the operation
    	if (i1 == 2) d3 = d1 - d2;								// decision which operation should be executed + definition of the operation
    	if (i1 == 3) d3 = d1 * d2;								// decision which operation should be executed + definition of the operation
    	if (i1 == 4) d3 = d1 / d2;								// decision which operation should be executed + definition of the operation
    	if (i1 == 5) d3 = (d1 /100) * d2;						        // decision which operation should be executed + definition of the operation
    		
    	cout << endl << endl << "Result:" << d3 << endl << endl;// output
    	
    	
    cin.get();													
    cin.get();
    
    	return(0);
    }


    What do you think about it??
    Attached Files







    #2
    Pretty messy but not bad if you just started.
    You could use case statements instead of those if statements.
    Start looking into functions and calling them next.

    -Loops are very useful too(don't really need it for this though).

    I see a ton of unneeded endls lol.

    \n inside the quotations in your cout statements work like an endl.

    you don't need 2 cin.get();


    system("pause"); or

    cin.ignore();
    cin.get();

    to keep the console open but i'm sure there are plenty of other ways.
    Last edited by Lrrpdude; 08-30-2010, 01:51 AM.
    Buy
    _______________________________________________

    Comment


      #3
      ok, thx for your suggestions






      Comment


        #4
        my version of vollhorst's Calculator (C++) ^^
        source code
        PHP Code:
        #include <iostream>
        #include <Windows.h>


        HANDLE console_output GetStdHandle(STD_OUTPUT_HANDLE);

        #define console_text_red                   SetConsoleTextAttribute(console_output, FOREGROUND_RED| FOREGROUND_INTENSITY);
        #define console_text_pink                  SetConsoleTextAttribute(console_output, FOREGROUND_BLUE | FOREGROUND_RED| FOREGROUND_INTENSITY);
        #define console_text_green                 SetConsoleTextAttribute(console_output, FOREGROUND_GREEN| FOREGROUND_INTENSITY);

        using namespace std;





        int main()
        {
            {
                
        system("title What is this :)");
                
        console_text_red
            printf
        ("Calculator\n"); 
                
        console_text_pink
            printf
        ("by vollhorst\n");
                
        console_text_green
                
                

            cout 
        << "Please chose an operation:" << endl << endl;
            
        cout << "\t 1 for + (addition)" << endl;
            
        cout << "\t 2 for - (suntraction)" << endl;
            
        cout << "\t 3 for * (multiplication)" << endl;
            
        cout << "\t 4 for / (division)" << endl;
            
        cout << "\t 5 for % (percent)" << endl << endl;
            
        cout << "Operation:";
            
            
        int i1;
            
        cin >> i1;
            
            
        system("cls");
            
            
        cout << endl << "Please enter your first value:"
            
            
        double d1;
            
        cin >> d1;                                                // First value for the operation

            
        cout << endl << "Please enter your second value:"
            
            
        double d2;
            
        cin >> d2;                                                // Second value for the operation
            
            
        double d3;
            
                
            if (
        i1 == 1d3 d1 d2;                                // decision which operation should be executed + definition of the operation
            
        if (i1 == 2d3 d1 d2;                                // decision which operation should be executed + definition of the operation
            
        if (i1 == 3d3 d1 d2;                                // decision which operation should be executed + definition of the operation
            
        if (i1 == 4d3 d1 d2;                                // decision which operation should be executed + definition of the operation
            
        if (i1 == 5d3 = (d1 /100) * d2;                                // decision which operation should be executed + definition of the operation
            
                
            
        console_text_red 
                cout 
        << endl << endl  <<  "Result:" << d3 << endl << endl;// output
            
            
        system("pause");

            return 
        0;
        }


        ??m?

        Comment


          #5
          nice :)






          Comment


            #6
            Re: vollhorst's Calculator (first c++ application :)

            Пиздец уродливый дизайн :]

            Comment

            Working...
            X