istream operator overload>>不管用

时间:2013-11-06 02:32:06

标签: c++

我必须创建一个istream运算符重载函数来读取用户输入的数据。该函数假设读取单个字符和单个数字而忽略字符,因为只有数字被输入并打印输出。它读取进入的数据但是当我打印出来时,它会打印出奇怪的符号。我正在提供我的istream>>函数和我的输出<<功能,虽然我很确定问题必须在istream内的某个地方。如果有人能看到一个很棒的错误,因为我整天都在处理这个问题。

    friend ostream& operator<< (ostream& os, const MyInt& x);
friend istream& operator>> (istream& is, MyInt& x);






   ostream& operator<< (ostream& oS, const MyInt& x)    
   {
for (int i = 0; i < x.currentSize; i++)
{
    oS << x.digitList[i];
}
return oS;
   }

    istream& operator>> (istream& is, MyInt& x)
    {
char c;

x.currentSize = 0;
x.maxSize = 5;

c = is.peek();
while(!isdigit(c))
{
    is.ignore();
    c = is.peek();

}

while(isdigit(c))
{
    c =is.get();     

    x.digitList[x.currentSize] = C2I(c);

    x.currentSize++;
    c = is.peek();

    if(x.currentSize >= x.maxSize)
         x.Grow();        
}
return is;

}

1 个答案:

答案 0 :(得分:0)

在这里,您要将ASCII数字转换为某种内部表示形式:

x.digitList[x.currentSize] = C2I(c);

但是当你执行输出时,在另一个方向上没有翻译,回到文本。

相关问题