tags:

views:

87

answers:

3

This is a basic program to get two 5-digit numbers as string and use addition on the 2 numbers utilising operator overloading on '+' .

#include <iostream>
#include <limits>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;


class IntStr
{
   int InputNum;
   public:
     //IntStr();
     IntStr::IntStr(int num);
     IntStr operator+ (const IntStr &);
     //~IntStr();
     void Display();
};

IntStr::IntStr(int num)
{
  InputNum = num;
}

void IntStr::Display()
{
   cout << "Number is (via Display) : " << InputNum <<endl;
}


IntStr IntStr::operator+ (const IntStr & second) {
        int add_result = InputNum + second.InputNum;
        return IntStr(add_result);
        }



int main()
{
    string str;
    bool option = true;
    bool option2 = true;
    while (option)
    {
    cout << "Enter the number : " ;
    if (!getline(cin, str)) 
    {
       cerr << "Something went seriously wrong...\n";
    }

    istringstream iss(str);
    int i;
    iss >> i;    // Extract an integer value from the stream that wraps str

    if (!iss) 
    {
       // Extraction failed (or a more serious problem like EOF reached)
       cerr << "Enter a number dammit!\n";
    } 
    else if (i < 10000 || i > 99999) 
    {
    cerr << "Out of range!\n";
    } 
    else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposesc only
      cout << "Number is : " << i << endl;
      option = false;
      IntStr obj1 = IntStr(i);
      obj1.Display();
    }
    }//while


    while (option2)
    {
    cout << "Enter the second number : " ;
    if (!getline(cin, str)) 
    {
       cerr << "Something went seriously wrong...\n";
    }

    istringstream iss(str);
    int i;
    iss >> i;    // Extract an integer value from the stream that wraps str

    if (!iss)  //------------------------------------------> (i)
    {
       // Extraction failed (or a more serious problem like EOF reached)
       cerr << "Enter a number dammit!\n";
    } 
    else if (i < 10000 || i > 99999) 
    {
    cerr << "Out of range!\n";
    } 
    else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposes only
      cout << "Number is : " << i << endl;
      option2 = false;
      IntStr obj2 = IntStr(i);
      obj2.Display();
      //obj1->Display();
    }
    }//while

    //IntStr Result = obj1 + obj2; // --------------------> (ii)
    //Result.Display();

    cin.get();
}

Need clarification on the points (i) & (ii) in the above code ...

(1) What does (i) actually do ?

(2) (ii) -> Does not compile.. as the error "obj1 not declared (first use this function)" comes up. Is this because obj1 & obj2 are declared only inside the while loops? How do I access them globally?

A: 
if (!iss)

tests if the stream is in a bad state, which will be the case if a conversion failed or if you are at the end of the stream

obj1 is defined here:

else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposesc only
      cout << "Number is : " << i << endl;
      option = false;
      IntStr obj1 = IntStr(i);
      obj1.Display();
    }

it is therefore local to the else-block & can't be accessed outside it. If you want to increase its scope, modve its definition outside of the block. It is not a good idea to move it outside of all blocks (i.e. make it global), however.

anon
A: 
  1. calls the overloaded operator which evaluates the stream in boolean context. This checks the state of the stream to see if the previous operation had failed - if so, you cannot rely on the value in the integer variable i being valid because the input on the stream was not an integer.

  2. the variables obj1 and obj2 are defined in the scope of the while loop - they are not available outside the scope. You can declare them outside the scope of the while in which case the variable will hold the last value it held in the while loop.

1800 INFORMATION
+1  A: 

1) From http://www.cplusplus.com/reference/iostream/ios/operatornot/ :

bool operator ! ( ) const; Evaluate stream object

Returns true if either one of the error flags (failbit or badbit) is set on the stream. Otherwise it returns false.

From http://www.cplusplus.com/reference/iostream/ios/fail/ :

failbit is generally set by an input operation when the error was related with the internal logic of the operation itself, while badbit is generally set when the error involves the loss of integrity of the stream, which is likely to persist even if a different operation is performed on the stream.

2) The two objects are not in scope, they exists only in the previous brackets.

Klaim
Wow didn't know about the operator!() overload, thanks!
Robert Gould
Well I didn't either, but in this kind of case it's easy to check the documentation...
Klaim
Understood the 'fail' part. But shouldn't we be checking something on these lines:Let 'abc' denote the operation "iss >> i;"Now shouldn't we check if (!abc), then say 'cerr << "Enter a number dammit!\n";' ???How do we justify checking if (!iss), because either ways, iss does exist. It's just that extraction of the stringstream to integer that we need to check..Am I being clear?
halluc1nati0n