0

ユーザーの入力に応じて、単語数、文字数、行数を出力するプログラムに取り組んでいました。しかし、私にはまったく知られていないこれらのエラーが発生し続けます。誰か助けてくれないかと思っていました。**以前のミスから変更しましたが、まだエラーが発生しています。申し訳ありませんが、C++は初めてです。

私が得たエラーは

 filestat.cpp:47: error: ‘line’ was not declared in this scope
 filestat.cpp: In function ‘int wc(std::string)’:
 filestat.cpp:55: error: ‘line’ was not declared in this scope
 filestat.cpp: In function ‘int cc(std::string)’:
 filestat.cpp:67: error: ‘line’ was not declared in this scope


#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int lc(string fname);
int wc(string fname);
int cc(string fname);

int main(){
string fname,line,command;
ifstream ifs;
int i;
while(true){
    cout<<"---- Enter a file name : ";

    if(getline(cin,line)){
        if(line.length()== 4 && line.compare("exit")== 0){
            cout<<"Exiting";
            exit(0);
        }else{
            string command = line.substr(0,2);
            fname= line.substr(4, line.length() -5);
                if( ifs.fail()){
                    ifs.open(fname.c_str());
                    cerr<< "File not found" <<fname <<endl;
                    ifs.clear();
                }else{
                    if(command.compare("lc")){
                        lc(fname);
                    }else if (command.compare("wc")){
                        wc(fname);
                    }else  if(command.compare("cc")){           
                                      cc(fname);                    
                    }else
                        cout<<"Command unknown. ";


                }
        }
    }
}
return 0;
}

 int lc(string fname){
 int count;
 while(getline(fname, line)){
    count++;
 }
  cout<<"Number of lines: "<<count ; 
   }

  int wc(string fname){
int count;
while(getline(fname, line)){
    int pos=line.find_first_of("\n\t ",0);
    while(pos =! string::npos){
        int length=line.length();
        line = line.substr(pos+1, length - pos);
        count++;
    }
  }
cout<< "Number of words: " <<count; 
  }
 int cc(string fname){
int count;
while(getline(fname, line)){
    count = count + line.length();
}

cout<< "Number of words: " <<count;

    }

行をグローバル変数として設定すると、次のエラーが発生します。

filestat.cpp:48:エラー:引数「1」の「std ::string」を「char**」に変換できません。「__ssize_tgetline(char **、size_t *、FILE *)」</ p>

4

4 に答える 4

1

あなたが宣言したようlineに、それはmain関数のローカル変数です。cc他の機能(など)では使用できませんwc

それをグローバル変数として宣言するか、引数としてccwcおよび他の関数に渡します。

于 2012-09-13T03:31:16.673 に答える
0

他にもエラーがあります。まず、、、、および関数lineのそれぞれにwcローカル変数が必要です。lccc

getline次に、を使用して呼び出すことはできませんfname。を期待しistreamます。では、関数を渡してみませんifsか?

int wc( ifstream &ifs )
{
    string line;
    int count = 0;
    while(getline(fname, line)){
        int pos=line.find_first_of("\n\t ",0);
        while(pos =! string::npos){
            int length=line.length();
            line = line.substr(pos+1, length - pos);
            count++;
        }
    }
    cout<< "Number of words: " <<count;
    return count;
}

上記では、初期化countして返しました(int returnタイプがあり、何も返さなかったため)。

他の関数についても同様の変更があります。

ちなみに、関数を検索して、本当に毎回部分文字列string::find_first_ofに置き換える必要があるかどうかを判断することをお勧めします。line2番目のパラメーターを見てください。

于 2012-09-13T04:02:38.293 に答える
0

エラーの状態として、lineリストされている「スコープ」(つまり関数)で宣言されていません。lineこれらの関数にアクセスできるようにする場合は、グローバル変数(の外部で宣言されることを意味します)を作成する必要がありmainます。

于 2012-09-13T03:33:24.493 に答える
0

グローバルスコープで行を宣言する以外に、そうです。問題のある関数でfilenameからifstreamオブジェクトを作成する必要があります。例えば

int cc(string fname){
ifstream f(fname);
int count;
while(getline(f, line)){
    count = count + line.length();
}
f.close();
}

これで十分ですが、関数定義を次のように変更することをお勧めします

 int cc(ifstream& f); 
于 2012-09-13T04:08:30.247 に答える