#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
usingnamespace std;
int lowercase(char );
string filename;
ifstream input(filename.c_str());
int main()
{
int y;
char letter,x;
cout<<"Please, enter the name of the file: "<<endl;
cin>>filename;
filename=filename+".txt";
input.open(filename.c_str());
if(!input)
{
cout<<" Error: can not open the file!! "<<endl;
return 1;
}
cout<<"Please Type :"<<endl;
cout<<" L to count the number of lower case characters ('a' to 'z') in the text "<<endl;
cout<<" U to count the number of upper case characters ('A' to 'Z') in the text "<<endl;
cout<<" D to count the number of digit characters ('0' to '9') in the text "<<endl;
cout<<" O to count the number of other characters (non-alphanumeric) in the text"<<endl;
cout<<" Q to stop the program "<<endl;
cin>>letter;
switch (letter)
{
case'L':
y=lowercase('a');
cout<<"a is "<<y<<endl;
y=lowercase('b');
cout<<"b is "<<y<<endl;
break;
}
return 0;
}
int lowercase(char let)
{
int count=0;
char x;
input.get(x);
while (input)
{
if(islower(x) && x==let)
{
count++;
}
input.get(x);
}
return count;
}
the file contain : aabbb
but when the program run it shows a=2 , b=0 !!
there is 3 b in the file!!
Is the problem with 'let'?? i think it stick with 'a' and doesn't change to 'b'!!
The problem is that, by the time you've finished the first call to lowercase, your input stream is positioned at the end of the file. You need to reset it back to the start of the file before you start looking again for the next letter.#
i think the problem is in "let " it stick with 'a' and doesn't change to 'b'
What evidence do you have for that? What behaviour are you seeing that indicates that it's somehow mysteriously not executing the second call to lowercase()?
Have you tried stepping through it with a debugger, to test your theory?