•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,593 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,705 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1772 | Replies: 15
![]() |
Okay Ive made a bunch of single use calculator programs (one for addition, subtraction, mutiplication, and division).
Addition Code
Subtraction
Multiplication
Division
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
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";
} Member of: Beautiful Code Club.
#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;#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:
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:
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();
}#include <iostream>
int main()
{
std::cout<< 12345 <<'\n';
} Member of: Beautiful Code Club.
>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.
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.
Member of: Beautiful Code Club.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Other Threads in the C++ Forum
- Previous Thread: Help!!!!!! General Protection Exception
- Next Thread: Scope Problem



Linear Mode