while & if help

Hello
i tried to solve this but i cant , the program will read the content of text file named (letter.txt )and determine number of
capitals and number of smalls., the problem is that i cant make a loop to read all the characters of the string ! it just read the first letter

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
 #include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
	
	

	cout<<"please enter a choice :\n"
		<<"1:Capital & smalls\n"
		<<"2:Vowels and consonants\n"
		<<"3:exit "<<endl;
     int a ;
	 cin>>a;
	 switch(a)
	 {
	 case 1:
		 {
			int small=0,capital=0;
			char a;
			int b,c=1;
			 ifstream infile;
			 infile.open("letters.txt");
			
			while(c){
			 infile>>a;
			  b=static_cast<int>(a);
			
			if((b>=65)&&(b<=90)){
				 capital++;}
			else if((b>=97)&&(b<=122)){
				small++;}}
			
			
			
			
			  cout<<small<<" "<<capital<<endl;
	
		 }

	 }
	system("pause");

	return 0;
}
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
#include <iostream>
#include <fstream>
#include <cctype>

int main()
{
    std::ifstream file( __FILE__ ) ; // this source file

    int upper = 0 ;
    int lower = 0 ;
    int digit = 0 ;
    int whitespace = 0 ;
    int punct = 0 ;
    int other = 0 ;

    char c ;
    while( file.get(c) ) // reads every byte including white spaces
    {
        if( std::isupper(c) ) ++upper ; // upper case in the C locale
        else if( std::islower(c) ) ++lower ;
        else if( std::isdigit(c) ) ++digit ;
        else if( std::isspace(c) ) ++whitespace ;
        else if( std::ispunct(c) ) ++punct ;
        else ++other ;
    }

    std::cout << "upper: " << upper << '\n' // 'A' 'B' ...
              << "lower: " << lower << '\n' // 'a' 'b' ...
              << "digit: " << digit << '\n' // '0' '1' ...
              << "space: " << whitespace << '\n' // ' ' '\n' ...
              << "punct: " << punct << '\n' // '?' '.' ...
              << "other: " << other << '\n' ; 
}

http://coliru.stacked-crooked.com/a/a6ccb000175ed594
closed account (1wvX92yv)
try using the following code snippet ......

1
2
3
4
5
6
7
8
9
10
11
ifstream infile;
infile.open("letters.txt");

while(!infile.eof())              //The important part
{
infile>>ch;
if((b>=65)&&(b<=90)){
				 capital++;}
			else if((b>=97)&&(b<=122)){
				small++;}}
}



The .eof() functions checks if the pointer in the file has reached the end of file or not. Once the pointer reaches the end of file, it returns true(1) which breaks the loop.

And infile>>ch; reads every character(even white spaces).
Last edited on
it dose not work !!
In "C" you can do a whileloop with this command:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int zahl=0;

while(zahl<5)
{
zahl++;
printf("Es wurde das %d.Mal wiederholt.\n",zahl);
system("PAUSE");
return 0;
}


I hobe I can help you
thank you very much
Topic archived. No new replies allowed.