Almost got it

Ok I almost got it....I think and this may be a simple fix but what is wrong with this, when I run it I get in the window screen File Error: Open Failed Press Any Key To Continue...

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
#include "stdafx.h"
#include <iostream>  
#include <fstream>  
#include <string>  
   

 using namespace std;

 const char FileName[] = "c:\\TestCount.txt";  
 
int main () {  
 string lineBuffer;  
 ifstream inMyStream (FileName);  
if (inMyStream.is_open()) {  
          
        int upperCaseCount[26] = {0};  
        int lowerCaseCount[97] = {0};  
       while (!inMyStream.eof() ){  
               getline (inMyStream, lineBuffer);  
              char oneLetter;  
              for( int n=0; n < lineBuffer.length(); ++n ){  
                     oneLetter = char( lineBuffer[n] );  
                     if (oneLetter >= 'A' && oneLetter <='Z') {  
                           upperCaseCount[int(oneLetter)- 65]++;   
                     
                       }  
                     if (oneLetter >= 'a' && oneLetter <='z') {   
                           lowerCaseCount[int(oneLetter)- 97]++;   
                    }  
             }  
        }  
         inMyStream.close();  
         for (int i= 0; i < 26; i++)  
                 cout << char(i + 65) << "\t\t" << upperCaseCount[i] << endl << char(i + 97) << "\t\t" << lowerCaseCount[i] << endl;  
        }  
         else cout << "File Error: Open Failed";  
        return 0;  
} 
well you never actually open the file...you say

1
2
3
4
5
6
if (inMyStream.is_open()) 
{
     blah blah
}
else
    cout << "File Error: Open Failed";


even though you never tried to open it. The open didn't fail you just never tried to do it.
I find no error in your code.. Just make sure "TestCount.txt" exist in the C drive.
Topic archived. No new replies allowed.