Okay Ive made a bunch of single use calculator programs (one for addition, subtraction, mutiplication, and division).

Addition Code

#include <iostream>
using namespace std;

int main()
{
     cout << "Please enter 2 numbers to be added "; 
     int a;
     int b;
     cin >> a;
     cout << "\n num1: " << a << endl;
     cin >> b;
     cout << " \n num2: " << b << endl;
     cout << " \n a + b = " << a + b << endl;
     
     cout << "Press the enter key to exit";
     cin.ignore(numeric_limits<streamsize>::max(), '\n');
     cin.get();
     return 0;
}

Subtraction

// Division Calculator
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter two numbers to be divided: ";
    int a;
    int b;
    
    cin >> a;
    cout << "\n num1: " << a << endl;
    cin >> b;
    cout << "\n num2: " << b << endl;
    
    cout << "\n a / b = " << a / b << endl;
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    
    return 0;
}

Multiplication

//Multiplication Calculator
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter two numbers to be multiplied: ";
    int a;
    int b;
    
    cin >> a;
    cout << "\n num1: " << a << endl;
    cin >> b;
    cout << "\n num2: " << b << endl;
    
    cout << "\n a x b = " << a * b << endl;
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    
    return 0;
}

Division

// Subtraction Calculator
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter 2 numbers to be subtractred";
    int a;
    int b;
    
    cin >> a;
    cout << "\n num1: " << a << endl;
    
    cin >> b;
    cout << "\n num2: " << b << endl;
    
    cout << "\n a - b = " << a - b << endl;
    cout << "Press the enter key to exit";
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    
    return 0;
}

My question is how do I combine these all into one program. Id ask please enter + - * or /. And then make an if statement ....if "symbol" ==x multiply. Is that the right logic or is there a much easier way to do this. And how would I right that if statement. Thanks youve helped me a lot lately guys. -Nick

The only difference is asking for an operator too:

#include <iostream>

using namespace std;

int main()
{
  int a, b;
  char op;

  cin>> a >> ws >> op >> b;

  if ( op == '+' )
    cout<< a + b <<'\n';
  else if ( op == '-' )
    cout<< a - b <<'\n';
  else if ( op == '*' )
    cout<< a * b <<'\n';
  else if ( op == '/' )
    cout<< a / b <<'\n';
  else
    cerr<<"Invalid operation\n";
}
#include <iostream>
using namespace std;

int main()
{
    int a;
    int b;
    char op;
    
    cout << "Please enter one number thne press enter then enter the second number";
    cin >> a;
    cout << "\n Num1: " << a << endl;
    cin >> b;
    cout << "\n Num2: " << b << endl;
    cout << "\n Now enter + to add....- to subtract....* to multiply....or / to divide";
    cin >> op;
    
    if ( op == "+" )
       cout << a + b << '\n;
    else if  ( op == "-")
       cout << a - b << '\n;
    else if ( op == "*" )
       cout << a * b << '\n;
    else if ( op == "/" )
       cout << a / b << '\n;
       else cout << "Invalid Operation\n";

Well heres my code and Im getting a lot of errors.
1.
IM not sure if theres the quotation marks or the thing on the same key around the operation sign?

if ( op == "+" )
       cout << a + b << '\n;

im really new to C++ and I never used char to declare a variable. I always used int but that wont work now cause int only works for numbers. Now what do you mean by char op; being a single char

BTW does char stand for character

#include <iostream>
using namespace std;

int main()
{
    int a;
    int b;
    char op;
    
    cout << "Please enter one number thne press enter then enter the second number";
    cin >> a;
    cout << "\n Num1: " << a << endl;
    cin >> b;
    cout << "\n Num2: " << b << endl;
    cout << "\n Now enter + to add....- to subtract....* to multiply....or / to divide";
    cin >> op;
    
    if ( op == '+' )
       cout << a + b << endl;
    else if  ( op == '-')
       cout << a - b << endl;
    else if ( op == '*' )
       cout << a * b << endl;
    else if ( op == '/' )
       cout << a / b << endl;
       else cout << "Invalid Operation\n";
  
     cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    

    return 0;
}

K Thans for all the help guys I dont know what I would do without you. One question though Narue in the code snippet u gave me it was '\n; after the if statment. however I got like 4 compiler errors when trying to compile it so I jsut changed it to endl;......What does the '\n; do and why did my compiler say it was an error?

>BTW does char stand for character
Yes.

>What does the '\n; do and why did my compiler say it was an error?
'\n' is an escape character that means newline. It's basically does the same thing as pressing Return on your keyboard. You shouldn't be getting errors with it though, especially since endl uses it for half of the operation:

template <typename T, typename Traits>
basic_ostream<T, Traits>& endl ( basic_ostream<T, Traits>& out )
{
  out.put ( out.widen ( '\n' ) );
  out.flush();
}

Notice that endl also flushes the stream, which can be expensive if done often. That's why I prefer using '\n' explicitly and let the C++ flush the stream for me except when I really need it. I would imagine that something else was causing the problem, but you can use this test program to see:

#include <iostream>

int main()
{
  std::cout<< 12345 <<'\n';
}

basic_ostream<T, Traits>& endl ( basic_ostream<T, Traits>& out )

gives me an invalid syntax error.

>gives me an invalid syntax error.
You're not meant to understand what's going on with that function or try to compile it. That code was strictly explanatory for future reference (and yes, it is correct. You're just trying to compile it incorrectly). The second example was for you to compile so that we could figure out what you're doing wrong so that '\n' fails to compile.

#include <iostream>

int main()
{
  std::cout<< 12345 <<'\n';
}

Oh I get it now. In the example you gave me for the if statement of which operation to use you just had ' \ n and didnt put a ' at the end like you jsut did in the example above.

>In the example you gave me for the if statement of which operation to
>use you just had ' \ n and didnt put a ' at the end like you jsut did in the example above.
I don't mind taking the blame, but pay more attention. My code was (and still is) correct. You butchered it into something broken by using string literals instead of character literals and forgetting to close your character literals.

Sorry about that then. In the example that you gave me you declared a, b, op, and ws

what was the ws mean and what was it for?

And something completely off topic but what do you use a header for and when do you use it? (didnt think I should make a new thread so ill just update this one)

>what was the ws mean and what was it for?
It's a modifier like endl that, put simply, eats whitespace from an istream.

>what do you use a header for and when do you use it?
A header provides declarations for functions and types that you might need. For example, cin and cout are declared in <iostream>, so if you want to use one of those you include <iostream>.

In other programs i see the header as a seperate file ****.h
Is that necesary to do or is it just for ease of use when declaring funtions and types instead of typing them in the main.cpp code.

>Is that necesary to do or is it just for ease of use when declaring
>funtions and types instead of typing them in the main.cpp code.
For library functions (standard or not) you have no choice but to include the proper header. For your own stuff, you don't have to, but it makes life easier if you modularize your code so that each source file is small and uncluttered.

This article has been dead for over six months. Start a new discussion instead.