0

私たちのプロジェクトは、入力ファイルから 2 進数を取得し、それらを 10 進数に変換することです。jGrasp は私が使用しているコンパイラであるため、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;

}
4

1 に答える 1

0

これは宿題のように見えるので、直接答えるだけではなく、学生の練習問題としていくつか残しておきます。

参照渡しは、すでに で行っていることですint& yまた、何を参照渡しするかを理解する必要があります。

「エラーメッセージを出力する」とはどういう意味ですか? エラーメッセージは何ですか? それは何ですか'?これらは、助けを求めるときに誰かに伝えることができる重要なことです。

コンパイラ エラー メッセージの場合は、その意味を読んで理解するようにしてください。たとえば、次のようなエラーが表示されると予想されます

error: 'fcin' was not declared in this scope.

「スコープ」の意味と、それが c++ プログラムの変数にどのように適用されるかについて、適切な調査を行ってください。

于 2013-10-10T16:01:42.433 に答える