Hey, everyone! I've got small problem in this code! I cannot find a mistake in this code. I was playing many times by changing functions and values, but something wrong. I'm beginer in C++, but I would like to be professional. I never ask for codes, only help for explaining. Thank you!!!

Compiler shows error:(59)error C2181: illegal else without matching if //-???
(65) error C1075: end of file found before than left brace... //-???

//Specification: Count the letter is a Text file 
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char FileName[] = "c:\\TestCount.txt";

int main()
{
     string lineBuffer;
     ifstream inMyStream (FileName); //open my file stream

     if (inMyStream.is_open()){
           //create an array to hold the letter counts
           int upperCaseCount[26] = {0};
           int lowerCaseCount[26] = {0};
 
           //read the text file
           while (!inMyStream.eof()){

                 //get a line of text
                 getline (inMyStream, lineBuffer);

                //read through each letter in the lineBuffer
                char oneLetter;
                for( int n=0; n < lineBuffer.length(); ++n ){
                    oneLetter = char( lineBuffer[n] );         //get a letter

                    if (oneLetter >= 'A' && oneLetter <='Z'){ //decide if it is a capital letter
                          upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                    }
                    
                    if (oneLetter >= 'a' && oneLetter <='z'){ //decide if it is a lower case letter
                          upperCaseCount[int(oneLetter)- 97]++; //make the index match the count array
                    }
                
            }//end while
             
            inMyStream.close(); //close the file stream
            //display the counts
            for (int i = 0; i < 26; i++)
                 cout << char(i + 65) << "\t\t" << upperCaseCount[i] << endl;
             
            for (int i = 0; i < 26; i++)
                 cout << char(i + 97) << "\t\t" << lowerCaseCount[i] << endl;
             
       }
       else 
            cout << "File Error: Open Failed";
        
  return 0;

}

Recommended Answers

All 9 Replies

Watch your indentation.

//Specification: Count the letter is a Text file 
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char FileName[] = "c:\\TestCount.txt";

int main()
{
   string lineBuffer;
   ifstream inMyStream (FileName); //open my file stream

   if ( inMyStream.is_open() )
   {
      //create an array to hold the letter counts
      int upperCaseCount[26] = {0};
      int lowerCaseCount[26] = {0};

      //read the text file
      while ( !inMyStream.eof() )
      {

         //get a line of text
         getline (inMyStream, lineBuffer);

         //read through each letter in the lineBuffer
         char oneLetter;
         for ( int n=0; n < lineBuffer.length(); ++n )
         {
            oneLetter = char( lineBuffer[n] );         //get a letter

            if ( oneLetter >= 'A' && oneLetter <='Z' )
            { //decide if it is a capital letter
               upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
            }

            if ( oneLetter >= 'a' && oneLetter <='z' )
            { //decide if it is a lower case letter
               upperCaseCount[int(oneLetter)- 97]++; //make the index match the count array
            }

         }//end while

         inMyStream.close(); //close the file stream
         //display the counts
         for ( int i = 0; i < 26; i++ )
            cout << char(i + 65) << "\t\t" << upperCaseCount[i] << endl;

         for ( int i = 0; i < 26; i++ )
            cout << char(i + 97) << "\t\t" << lowerCaseCount[i] << endl;

      }
      else
         cout << "File Error: Open Failed";
        
  return 0;
}

Note how that if statement is missing a closing brace.

Yes, I've found mistake myself. I missed the braket in for (for ( int n=0; n < lineBuffer.length(); ++n )). But here is another error, code shows me only cout << "File Error: Open Failed". Why it doesn't read upper and lower case letters? Where can be my mistake? Thanks

Did you mean this?

else
   {
     cout << "File Error: Open Failed";
   }

It shows me the same error: "File Error: Open Failed"

First make sure you can open the file.

Use this:

while ( getline (inMyStream, lineBuffer) )

instead of

while ( !inMyStream.eof() )
      {
         getline (inMyStream, lineBuffer);

Be consistent with the 'A' and 'Z' stuff instead of going to 65 and 97.

Oh, and post updated code. I don't read minds.

Did you mean this?
else
{
cout << "File Error: Open Failed";
}
It shows me the same error: "File Error: Open Failed"

The else statement is one line, so it's irrelevant whether you put brackets around that one line or not.

You were missing bracket at the end of your "for" statement, which you say you've found and corrected. If you are getting the "File Error: Open Failed" display, then this returned false:

if ( inMyStream.is_open() )

so you weren't able to open this file:

const char FileName[] = "c:\\TestCount.txt";

Check to make sure it exists and that you have permission to open it.

Sorry, but could you make clear what you mean: "Check to make sure it exists and that you have permission to open it."
1. Make true, false statement? or,
2. File should be present in the code?
3. Take this off: if ( inMyStream.is_open() )? or...?
Im sorry, bud Im little bit confusing about storing and reading files.
Thanks for your help!!!

Ok, if this: if ( inMyStream.is_open() ) returned me false, I change it for : if ( !inMyStream.is_open() ). I think, that was your comment about, but Im not sure.
Code is working, but shows me the blank page or nothing. Why?

I meant you need to have permission to open the file from an Operating System point of view, not a C++ programmer point of view. If you have a limited account and someone with an admin account created the file, you may not be able to open it. Try opening it manually (i.e. double click it) and see if it opens. if it does, you have permission to open it.

Thank you very much, VernonDozier !!! That's what I missed learning my course. I didn't know that I have to save these files to C drive. You know, seems like when I visit forums I get more help and knowledge than from university! That's my first time getting help from forum, and I've got more for 3 hours than from 3 days. Thanks guys, and have a nice day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.