2012-09-13 35 views
0

我正在編寫一個程序,它根據用戶的輸入輸出字數,字符數和行數。但是我不斷收到這些完全不爲我所知的錯誤。我想知道有沒有人可以幫忙。 **我從之前的錯誤中改變了它,並且仍然收到錯誤。對不起,我是C++新手。在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: error: cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’

+0

你可以使代碼更大量使用縮進正確讀取。 (也許你是混合空格字符與製表符。從來不是一個好主意。) – jogojapan

回答

0

您還有其他錯誤。首先,您需要在wclccc函數中分別使用局部變量line

其次,您不能使用fname致電getline。它預計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返回類型,並沒有返回任何東西)。

其他功能的相似變化。

順便說一句,您可能想查找string::find_first_of函數,並決定是否真的需要每次都用子字符串替換line。看看第二個參數。

1

你聲明line的方式,它是在一個局部變量main功能。您不能在其他功能中使用它(ccwc等)。

要麼將​​其聲明爲全局變量,要麼將其作爲參數傳遞給ccwc和其他函數。

+0

當我聲明它作爲一個全局變量我然後得到錯誤 – user1513323

+0

[filestat.cpp:48:錯誤:不能轉換'std :: string'到'char **'作爲參數'1'到'__ssize_t getline(char **,size_t *,FILE *)'] – user1513323

+1

將字符串轉換爲char *你做line.c_str()和額外* char * *你做&line.c_str() – Borgleader

0

由於您的錯誤狀態,line沒有在列出的「範圍」(即函數)中聲明。如果您希望它們可以訪問這些函數,則需要使line爲全局變量(意爲在main之外聲明)。

0

好吧除了在全局範圍內聲明行。你需要在有問題的函數中從文件名中創建一個ifstream對象。例如,

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

本應該做的,但我會建議改變函數定義到

int cc(ifstream& f);