C++ 调试问题:(
我正在编写一个程序,根据用户的输入打印出字数、字符数和行数。但我一直收到这些我完全不知道的错误。我想知道是否有人可以帮忙。 **我已经改变了它,并且仍然在接收错误。对不起,我是C++新手。 我犯的错误是C++ 调试问题:(,c++,debugging,unix,C++,Debugging,Unix,我正在编写一个程序,根据用户的输入打印出字数、字符数和行数。但我一直收到这些我完全不知道的错误。我想知道是否有人可以帮忙。 **我已经改变了它,并且仍然在接收错误。对不起,我是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
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:47:错误:未在此作用域中声明“line”
filestat.cpp:在函数“int wc(std::string)”中:
filestat.cpp:55:错误:未在此作用域中声明“line”
filestat.cpp:在函数“int cc(std::string)”中:
filestat.cpp:67:错误:未在此作用域中声明“line”
#包括
#包括
#包括
使用名称空间std;
int-lc(字符串fname);
int wc(字符串fname);
int cc(字符串fname);
int main(){
字符串fname、line、command;
国际单项体育联合会;
int i;
while(true){
coutlength()是std::string的成员函数。您缺少()
另外,std::string::length()返回一个整数。4
不应该用括起来。
fname,c_str()
不太正确。另外,您的第一个else
可能缺少一个大括号。您的大括号不匹配
if(line.length== 4 && line.compare("exit")== 0) // line.length()