1

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;
}
4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by rinoa04
Featured 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. Read More

  • 1

    [quote=Maux;251750]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... [code]// simple calc // Chris Wilson #include <iostream.h> int … Read More

1

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.

1

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.

1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.