简体   繁体   English

istream运算符重载>>将不起作用

[英]istream operator overload >> will not work

I have to create an istream operator overload function to read the data the user enters. 我必须创建一个istream运算符重载函数来读取用户输入的数据。 The function is suppose to read both single chars and single digits while ignoring the chars because only digits are suppose to be entered and printedout. 该函数假定在忽略字符的同时读取单个字符和单个数字,因为仅假定数字被输入和打印输出。 It reads the data coming in but when my they are printed out, it prints out weird symbols instead. 它读取输入的数据,但是当我打印出数据时,它会打印出奇怪的符号。 i am supplying my istream>> function along with my output<< function although im pretty sure the problem has to be somewhere inside the istream. 我提供我的istream >>函数以及我的output <<函数,尽管我非常确定问题一定存在于istream内部。 if anyone can see an error that would be awesome as i've been working on this single problem all day. 如果有人整天都在研究这个单一问题,那么看到的错误将是非常棒的。

    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;

} }

Here you are translating ASCII digits to some internal representation: 在这里,您正在将ASCII数字转换为一些内部表示形式:

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

But there is no translation in the other direction, back to text, when you perform output. 但是执行输出时,没有反方向的转换,即文本转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM