how to use operator overloading and catch invalid input
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I couldn't quite follow the code in the original post so I did a little formatting and used the code block tags to make it a bit easier to read. FWLIW, when I try this out it just goes into an infinite loop on the input. But my C++ skills are nascent at best.
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <complex>
using namespace std;
const char openParen = '(';
const char closeParen = ')';
const char iChar = 'i';
const string ops("+-*/");
const string signs("-+");
const int negPos[] = { -1, 1 };
int main(int argc, char *argv[]) {
complex<float> *c1, *c2, result;
float a, b, c, d;
char opChar, lp, rp, signChar, i;
size_t op, s1, s2;
string in;
stringstream ss;
cout << "Enter complex equation ..." << endl;
cout << " (a +|- bi) op (c +|- di) " << endl;
cout << "where op is one of : " << ops << endl;
while (true) {
cout << endl << "> ";
getline(cin,in);
ss.clear(); ss.str(in);a;
if ( (ss >> lp >> a >> signChar >> b >> i >> rp >> opChar) &&
(lp == openParen) && (i == iChar) && (rp == closeParen) &&
( (s1 = signs.find(signChar) ) != string::npos) &&
( (op = ops.find(opChar) ) != string::npos) &&
(ss >> lp >> c >> signChar >> d >> i >> rp) &&
( (s2 = signs.find(signChar) ) != string::npos) &&
(lp == openParen) && (i == iChar) && (rp == closeParen) ) {
b *= negPos[s1]; d *= negPos[s2];
c1 = new complex<float>(a,b);
c2 = new complex<float>(c,d);
switch (opChar) {
case '+' : result = *c1 + *c2; break;
case '-' : result = *c1 - *c2; break;
case '*' : result = *c1 * *c2; break;
case '/' : result = *c1 / *c2; break;
}
cout << endl << in << " = (" << noshowpos << result.real()
<< showpos << result.imag() << "i)" << endl;
delete c1;
delete c2;
}
}
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.