I'm new to these forums,and really new to C++,but here is what i need to know.I made a simple calculator in C++ and i was wondering what i need to be able to keep useing it without opening the program everytime...

// simple calc
// Chris Wilson
#include <iostream.h>
int main()
{
    float first;
    float second;
    float oper;
    float output; 
 
 
 
    cout << "|--------------------|\n";
    cout << "|-----Chris_Calc-----|\n";
    cout << "|--------------------|\n";
    cout << "|---------by:--------|\n"; 
    cout << "|----Chris_Wilson----|\n"; 
    cout << "|--------------------|\n"; 
 
    cout << "Would you like to:\n";
    cout << "1)add\n";
    cout << "2)subtract\n";
    cout << "3)multiply\n";
    cout << "4)divide\n";
    cin  >> oper;
    if (oper==1)
    {
                cout << "enter a number to add\n";
                cin  >> first;
                cout << "enter another number to add\n";
                cin  >> second;
                cout << "you get\n";
                output = first + second;
                cout << output <<endl;
    }
    if (oper==2)
    {
                cout << "enter a number to subtract\n";
                cin  >> first;
                cout << "enter another number to subtract\n";
                cin  >> second;
                cout << "you get\n";
                output = first - second;
                cout << output <<endl;
    }
    if (oper==3)
    {
                cout << "enter a number to multiply\n";
                cin  >> first;
                cout << "enter another number to multiply\n";
                cin  >> second;
                cout << "you get\n";
                output = first * second;
                cout << output <<endl;
    }        
    if (oper==4)
    {
                cout << "enter a number to divide\n";
                cin  >> first;
                cout << "enter another number to divide\n";
                cin  >> second;
                cout << "you get\n";
                output = first / second;
                if (first > second)
                cout << output <<endl;
    }
 
 
    system("pause");
    return 0;
}

Recommended Answers

All 3 Replies

The easiest way would be to put a big loop around the main part. You could have your menu contain an extra option to quit, and just keep doing calculations until they enter that option.

I'm new to these forums,and really new to C++,but here is what i need to know.I made a simple calculator in C++ and i was wondering what i need to be able to keep useing it without opening the program everytime...

// simple calc
// Chris Wilson
#include <iostream.h>
int main()
{
    float first;
    float second;
    float oper;
    float output; 
 
 
 
    cout << "|--------------------|\n";
    cout << "|-----Chris_Calc-----|\n";
    cout << "|--------------------|\n";
    cout << "|---------by:--------|\n"; 
    cout << "|----Chris_Wilson----|\n"; 
    cout << "|--------------------|\n"; 
 
    cout << "Would you like to:\n";
    cout << "1)add\n";
    cout << "2)subtract\n";
    cout << "3)multiply\n";
    cout << "4)divide\n";
    cin  >> oper;
    if (oper==1)
    {
                cout << "enter a number to add\n";
                cin  >> first;
                cout << "enter another number to add\n";
                cin  >> second;
                cout << "you get\n";
                output = first + second;
                cout << output <<endl;
    }
    if (oper==2)
    {
                cout << "enter a number to subtract\n";
                cin  >> first;
                cout << "enter another number to subtract\n";
                cin  >> second;
                cout << "you get\n";
                output = first - second;
                cout << output <<endl;
    }
    if (oper==3)
    {
                cout << "enter a number to multiply\n";
                cin  >> first;
                cout << "enter another number to multiply\n";
                cin  >> second;
                cout << "you get\n";
                output = first * second;
                cout << output <<endl;
    }        
    if (oper==4)
    {
                cout << "enter a number to divide\n";
                cin  >> first;
                cout << "enter another number to divide\n";
                cin  >> second;
                cout << "you get\n";
                output = first / second;
                if (first > second)
                cout << output <<endl;
    }
 
 
    system("pause");
    return 0;
}

Using system ("pause") calls is a bad programming practice since it invokes the OS and thus is an expensive as well as non portable function.

If using C just use getchar() and when using C++ use cin.get() to stop the screen from disappearing.

Also one more thing, why dont you use the switch construct instead of repeated use of "if" constructs.

Dont use #include <iostream.h> that is old style headers. Instead use #include <iostream> and then using namespace std; . IT would perform the same functionality.

Hope it helped, bye.

You can include a wrap your coding inside a while loop with a sentinel value to loop to keep requesting for input until you give the option to quit. For example, you can add in another option 5 - quit where if you enter 5 then the program will close or else it will keep on running.

It is a good practice also to provide control such as error message if user enter any option other than the input allowed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.