Some questions on type.

This is a discussion on Some questions on type. within the C++ Programming forums, part of the General Programming Boards category; Was reading the other thread on a basic calculator and started making one myself just for some practice. The problem ...

  1. #1
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60

    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 addIt(int addOne, int addTwo) { return (addOne + addTwo); };
        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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Alternatively, just
    if (bob)
    will do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60

    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.
    Last edited by xentaka; 05-22-2011 at 02:40 PM.

  5. #5
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  2. Replies: 5
    Last Post: 01-24-2011, 04:37 AM
  3. Replies: 10
    Last Post: 03-08-2010, 10:30 AM
  4. Replies: 6
    Last Post: 04-10-2008, 11:49 PM
  5. Replies: 17
    Last Post: 03-06-2008, 01:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21