Debugging errors in c++
Posted
by
user1513323
on Stack Overflow
See other posts from Stack Overflow
or by user1513323
Published on 2012-09-13T03:28:44Z
Indexed on
2012/09/13
3:38 UTC
Read the original article
Hit count: 132
I was working on a program that printed out the word count, character count and line count depending on the user's input. But I keep getting these error that are completely unknown to me. I was wondering if anyone could help. ** I've changed it from previous mistakes and am still receiving errors. Sorry I'm new to C++.
The errors I got were
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;
}
© Stack Overflow or respective owner