Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将函数更改为根据y变量从输入文件中获取的数据通过引用传递?_C++_File Io - Fatal编程技术网

C++ 如何将函数更改为根据y变量从输入文件中获取的数据通过引用传递?

C++ 如何将函数更改为根据y变量从输入文件中获取的数据通过引用传递?,c++,file-io,C++,File Io,我们的项目是从输入文件中获取二进制数,然后将它们转换为十进制等效数。我在jGrasp中创建了输入文件,因为这是我正在使用的编译器。如果数字前面有空格,则忽略它并继续,直到有数字为止。如果数字之间有空格,则会打印错误消息。除了在我的函数中我使用了cin.ignore,我让它在某种程度上工作,现在当我将它更改为fcin.ignore时,它会给我一条错误消息。我的老师说把我的函数改为通过引用传递,这样可以解决这个问题,但我不知道我会怎么做。有什么建议吗 //input file 111101\n

我们的项目是从输入文件中获取二进制数,然后将它们转换为十进制等效数。我在jGrasp中创建了输入文件,因为这是我正在使用的编译器。如果数字前面有空格,则忽略它并继续,直到有数字为止。如果数字之间有空格,则会打印错误消息。除了在我的函数中我使用了cin.ignore,我让它在某种程度上工作,现在当我将它更改为fcin.ignore时,它会给我一条错误消息。我的老师说把我的函数改为通过引用传递,这样可以解决这个问题,但我不知道我会怎么做。有什么建议吗

//input file
   111101\n
1101\n
11  001\n
111000000000000011101010101010101010100\n


//Program
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main( ){
   // vairables
   char ch;
   int decimal = 0;

   //function prototypes
   void decimalValue (char , int&);



   //declaring filestream
   string fileName;
   ifstream fcin;

   //asking user for file name
   cout << "What is the file called: "<< endl;
   cin >> fileName;

   fcin.open(fileName.c_str());
   fcin.get(ch);
   while (!fcin.eof()){ 

      //fcin.get(ch);
      decimalValue(ch,decimal);
      fcin.get(ch);
    }  // end while
    cout << decimal << endl; 
   fcin.close();

return 0;
}

//functions
void decimalValue( char x, int& y ){
   void improperInput(char , int&);
   if (x == '1'){
      y = ((y *2)+1);
      }
   else if (x == '0'){
      y = (y *2);
      }
   else if (x == '\n'){
      cout << y << endl;
      y = 0;
   }
   else if (x == '2')
      improperInput(x,y);
   else if (x == '3')
      improperInput(x,y);
   else if (x == '4')
      improperInput(x,y);
   else if (x == '5')
      improperInput(x,y);
   else if (x == '6')
      improperInput(x,y);      
   else if (x == '7')
      improperInput(x,y);   
   else if (x == '8')
      improperInput(x,y);
   else if (x == '9')
      improperInput(x,y);
   else if ((y == 0) && (x == ' '))
      fcin.ignore();
   else if ((y != 0) && (x == ' '))
      improperInput(x,y);
}

void improperInput(char x, int& y ){
      fcin.ignore(50,'\n');
      cout << "*Improper Input*" << endl;
      y = 0;

}
//输入文件
111101\n
1101\n
11 001\n
1110000000000000111010101010101010100\n
//节目
#包括
#包括
#包括
使用名称空间std;
int main(){
//虚荣
char ch;
整数小数=0;
//功能原型
无效小数值(字符、整数和);
//声明文件流
字符串文件名;
iffcin;
//询问用户文件名
cout文件名;
打开(fileName.c_str());
fcin.get(ch);
而(!fcin.eof()){
//fcin.get(ch);
小数值(ch,十进制);
fcin.get(ch);
}//结束时

这看起来很像家庭作业,所以我不会直接回答,而是给学生留一些练习

通过引用传递是您已经在使用
int&y
进行的操作。您还需要了解通过引用传递的内容

“它打印出一条错误信息”是什么意思?错误信息是什么?它是什么?这些都是请求帮助时能够告诉别人的重要事情

如果是编译器错误消息,您应该尝试阅读并理解它的含义

error: 'fcin' was not declared in this scope.
请对C++中的变量的范围和用法进行适当的研究。