HAVING TROUBLE OVERLOADING THE EXTRACTION OPERATOR
Mar 22, 2012 at 3:49pm UTC
Hi there, i want to overload the extraction operator >> to read a pair of numbers in the following form (4,6). i have struggled for 10hours. I COULD REALLY USE SOME HELP. below is my attempt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
istream& operator >>(istream& ins, const Pairs& _member)
{
string pair;
int next = 0, i = 0, num2;
getline(ins, pair, '\n' );
for (int i = 1; i < pair.size(); i++)
{
if ((isdigit(pair[i]))&&(pair[i-1] == '-' )){
ins >> pair[i];
num1 = -atoi(pair[i]);
}
else if (isdigit(pair[i]))
ins >> pair[i];
}
/*char myPair[12];
char myDigits[12];
while(myPair[next] != '\0')&&()
{
if(myPair[next] == '-')
{
while(isdigit(myPair[next]))
{myDigits[i] = myPair[next];i++;}
f = -atoi(myDigits[]);
ins >> _member.f;
}
else
{
while(isdigit(myPair[next]))
{myDigits[i] = myPair[next];i++;}
f = atoi(myDigits[]);
ins >> _member.f;
}
if(myPair[next] == ',')
{
if(myPair[next] == '-')
{
while(isdigit(myPair[next]))
{myDigits[i] = myPair[next]; i++;}
s = -atoi(myDigits[]);
ins >> _member.s;
}
else
{
while(isdigit(myPair[next]))
{myDigits[i] = myPair[next]; i++;}
s = atoi(myDigits[]);
ins >> _member.s;
}
}next++;
} */
return ins;
}
Last edited on Mar 22, 2012 at 7:35pm UTC
Mar 22, 2012 at 4:39pm UTC
Why not just create a function instead? There are compatibility issues with some operator ">>" and getline(), hate to see them mixed into one.
Mar 22, 2012 at 5:44pm UTC
create a function? i lost u there.
Mar 22, 2012 at 5:59pm UTC
Hi
Couldyou post the structure of your Pair class ? what you are attending to do, may have another easy solution, algorithm.
Last edited on Mar 22, 2012 at 5:59pm UTC
Topic archived. No new replies allowed.