调试问题:(
Debugging problems :(
我正在编写一个程序,该程序根据用户的输入打印字数、字符数和行数。但我一直在犯这些我完全不知道的错误。我想知道是否有人能帮上忙。**我已经更改了它,但仍然收到错误。对不起,我是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("nt ",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;
}
length()是std::string的一个成员函数。您错过了()
if(line.length== 4 && line.compare("exit")== 0) // line.length()
此外,std::string::length()返回一个整数。4
不应被包围在""
中。
相关文章:
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 需要帮助调试"attribute parser"!C++ 来自黑客排名的问题
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- 调试C++程序时遇到问题
- 在发布模式下崩溃,但如果可调试为 true - 不是..什么是可能的问题
- C++ CDB 调试器问题
- 如何调试错误代码 77:cudaErrorIllegal地址当 cuda-memcheck 没有发现问题时
- 如何调试我的 OpenGl 问题
- 调试基于 Linux 的 C++ 代码 Visual Studio 代码时出现问题
- Linux Opensuse C 编译器问题.(无法初始化部分调试器的解压缩状态)
- 如何调试这个C++类出了什么问题?
- 每当调试C (Xcode 10.1)时,输入问题
- 调试堆栈损坏问题
- 为什么调试器在排序前显示问题
- C Microsoft Visual Studio 2015调试问题(找不到或打开PDB文件.)
- gdb 7.5.1调试通过g 4.7.2构建的可执行文件的问题
- 在非调试模式下运行代码时出现问题
- 调试Xcode 4.4中的libc++问题
- 在OS X上使用MacPorts GCC调试问题
- 调试问题:(