what is the problem here

hi , this is the program


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace 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'!!


Last edited on
Basically, you finished up reading up to the end of the file.
You must go back to the beginning of the file ( input.seekg (0, input.beg); )

For your example, you must write it into the "lowercase" function, before the "return" statement.
Last edited on
If you mean like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int lowercase(char let)
{

	
	int count=0;
	char x;
	input.get(x);
	while (input)
	{
		
		
		
		if(islower(x) && x==let) 
		{
		count++;
		}
		input.get(x);
		
	}
	input.seekg (0, input.beg);\\here
	return count;
}


it didn't work , i think the problem is in "let " it stick with 'a' and doesn't change to 'b'
Last edited on
please I want the solution now :(
try clearing the failed bit flags also.
input.clear();
abhishekm71 thanx but it doesn't work either the problem not from reading the file , the problem with 'let'!!
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?
thanx all of you , finally it worked the solution is what abhishekm71 suggested

I put input.clear(); before return statement in the lowercase function .
Have you made sure that you have cleared the flags before the seekg function?
1
2
input.clear();
input.seekg (0, input.beg);


The documentation for istream::seekg function states:
If the eofbit flag is set before the call, the function fails (sets failbit and returns).
Last edited on
Topic archived. No new replies allowed.