2012-09-13 2 views
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; 

    } 
+2

'fname, c_str()'이 적절하지 않습니다. 또한 첫 번째 'else'에는 아마도 여는 중괄호가 빠져 있습니다. – chris

+0

그리고 중괄호가 맞지 않습니다. – aland

답변

1

길이() 표준의 멤버 함수가 있었다 : : 문자열입니다. 누락되었습니다. ()

if(line.length== 4 && line.compare("exit")== 0) // line.length() 

또한 std :: string :: length()는 정수를 반환합니다. 4""으로 묶어서는 안됩니다.