# Thread: Some questions on type.

1. ## Some questions on type.

Was reading the other thread on a basic calculator and started making one myself just for some practice. The problem I had was checking for valid types.

Code:
```#include <iostream>
#include <string>
#include <math.h>
#include <sstream>

using namespace std;

class calcIt {

int varA, varB;

public:

calcIt() { varA=0; varB=0; };
int subIt(int subOne, int subTwo) { return (subOne - subTwo); };
int mulIt(int mulOne, int mulTwo) { return (mulOne * mulTwo); };
int divIt(int divOne, int divTwo) { return (divOne / divTwo); };
int checkNum(int checkN);

};

int calcIt::checkNum(int checkN) {
if (fmod(checkN, 1) == 0) { return checkN; }
return -1;
}

int main() {
int varA, varB, firstW, secondW;
string varC;
calcIt c;

cout << "Enter the calculation to be made ( n +/-* n ) : ";
getline(cin, varC);
firstW = varC.find(" ",0);
secondW = varC.find(" ", (firstW + 1));
stringstream(varC.substr(0, firstW)) >> varA;
stringstream(varC.substr(secondW)) >> varB;
varC = varC.substr((firstW + 1), 1);
if (( c.checkNum(varA) == -1 ) || ( c.checkNum(varB) == -1)) {
cout << endl << "Invalid operators!" << endl;
return 1;
}
if (varC == "*") { cout << endl << c.mulIt(varA,varB) << endl; }
else if(varC == "/") { cout << endl << c.divIt(varA,varB) << endl; }
else if(varC == "+") { cout << endl << c.addIt(varA,varB) << endl; }
else if(varC == "-") { cout << endl << c.subIt(varA,varB) << endl; }
else { cout << endl << "Invalid operator!" << endl; }

return 0;
}```
It works perfectly, but if you pass alpha strings into varA and varB it will still calculate them since the stringstream turns them into int form. This isn't really a problem, but I would rather have it reject alpha chars for these vars.

2. If a read fails, it will set the failbit. That does mean you can't immediately destroy the stringstream like you do.
Code:
```stringstream bob(varC.substr(0, firstW));
bob >> varA;
if (bob.fail()) { //no input }```

3. Alternatively, just
if (bob)
will do.

4. ## Hmmm

Would this be better, or should I still error check each line?

Code:
```int main() {
int varA, varB, firstW, secondW;
string varC;
calcIt c;

try {
cout << "Enter the calculation to be made ( n +/-* n ) : ";
getline(cin, varC);
firstW = varC.find(" ",0);
secondW = varC.find(" ", (firstW + 1));
stringstream (varC.substr(0, firstW)) >> varA;
stringstream (varC.substr(secondW)) >> varB;

varC = varC.substr((firstW + 1), 1);
if (( c.checkNum(varA) == -1 ) || ( c.checkNum(varB) == -1)) {
cout << endl << "Invalid operators!" << endl;
return 1;
}
if (varC == "*") { cout << endl << c.mulIt(varA,varB) << endl; }
else if(varC == "/") { cout << endl << c.divIt(varA,varB) << endl; }
else if(varC == "+") { cout << endl << c.addIt(varA,varB) << endl; }
else if(varC == "-") { cout << endl << c.subIt(varA,varB) << endl; }
else { cout << endl << "Invalid operator!" << endl; }
}
catch (exception& e) {
cout << "Exception: " << e.what() << endl;
return 1;
}

return 0;
}```
If I enter a blank string it returns an exception bad_string::substr and exits the programs.

5. Thought you meant if nothing was entered, but added following based on your suggestion.

Code:
```        stringstream in1(varC.substr(0, firstW));
stringstream in2(varC.substr(secondW));
in1 >> varA;
in2 >> varB;
if ((!in1) || (!in2)) {
cout << "Error!" << endl;
return 1;
}```
Works perfectly now. Thank you Tab and El.