C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
The programme to read-in frm FILE and prints out Posted by tokoG on 20 Apr 2006 at 8:31 PM
This message was edited by tokoG at 2006-4-20 22:13:4

Hello again

I need to complete a programme by using the provided templates (So I can't modify this drastically) to do the followings;

1) Write a C Programme that reads the data from a file
2) The programme should find the number of vowels and full-stop ('.') included in the file
3) Then prints the following two sentences;


Number of vowels = %d (<- number of vowels to be printed)
Number of full-stop = %d (<- number of full-stop to be printed)



I made the folloing code but debugger found error.


} /*end main*/ //Errr: Compound statement missing



if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u')
I purported to select the vowels. Is this OK..?

} /*end main*/
When the error points here out and say, Compound statement is missing, isn't it the either } is missing somewhere? But I can't find(!)


Here is the code;
(**Here again, fflush(stdin) is used because it's in the template and I am not allo to change, thanks!)
#include <stdio.h>
#include <ctype.h>

void main() {

  int data;           //Holds the value of the content in the file
  FILE* fp;           //FILE pointer
  int countvowel = 0; //Count the number of vowels
  int countfs = 0;    //Counte the number of full-stop
  fp = fopen("properties.txt", "r"); //Opens the file for reading, save the containt in the pointer fp
  if (fp == NULL) {                  //If the file opening failed, program exit
    printf("Failed to open file\n");
    return;                          //Leave the programme
  }/*end if*/


  //Otherwise, if the file opened,
  //it loop to search the vowels and full-stop
  while ((data = getc (fp)) != EOF) {//Loops until the EOF
    if(isalnum(data)) {              //If data is a char or a digit
      if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u'){   //If it finds the vowel,
         countvowel++;               //increase the vowel count
      }  else if (data == '.'){      //If not vowel, but full-stop,
           countfs++;                 //increase the full-stop count
         }/*end else-if*/
  }/*end while*/

  printf("%s\n%s\n", "Number of vowels = %d", countvowel,
                     "Number of full-stop = %d", countfs);


  fclose(fp);    //Close the file

  fflush(stdin); //Hold the screen
  getchar();

} /*end main*/

Report
Re: The programme to read-in frm FILE and prints out Posted by tsagld on 21 Apr 2006 at 12:27 AM
: This message was edited by tokoG at 2006-4-20 22:13:4

: Hello again
:
: I need to complete a programme by using the provided templates (So I can't modify this drastically) to do the followings;
:
: 1) Write a C Programme that reads the data from a file
: 2) The programme should find the number of vowels and full-stop ('.') included in the file
: 3) Then prints the following two sentences;
:
:
: Number of vowels = %d (<- number of vowels to be printed)
: Number of full-stop = %d (<- number of full-stop to be printed)
:

:
:
: I made the folloing code but debugger found error.
:
:
: } /*end main*/ //Errr: Compound statement missing
:

:
:
: if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u')
: I purported to select the vowels. Is this OK..?
:
: } /*end main*/
: When the error points here out and say, Compound statement is missing, isn't it the either } is missing somewhere? But I can't find(!)
:
:
: Here is the code;
: (**Here again, fflush(stdin) is used because it's in the template and I am not allo to change, thanks!)
:
: #include <stdio.h>
: #include <ctype.h>
: 
: void main() {
: 
:   int data;           //Holds the value of the content in the file
:   FILE* fp;           //FILE pointer
:   int countvowel = 0; //Count the number of vowels
:   int countfs = 0;    //Counte the number of full-stop
:   fp = fopen("properties.txt", "r"); //Opens the file for reading, save the containt in the pointer fp
:   if (fp == NULL) {                  //If the file opening failed, program exit
:     printf("Failed to open file\n");
:     return;                          //Leave the programme
:   }/*end if*/
: 
: 
:   //Otherwise, if the file opened,
:   //it loop to search the vowels and full-stop
:   while ((data = getc (fp)) != EOF) {//Loops until the EOF
:     if(isalnum(data)) {              //If data is a char or a digit
:       if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u'){   //If it finds the vowel,
:          countvowel++;               //increase the vowel count
:       }  else if (data == '.'){      //If not vowel, but full-stop,
:            countfs++;                 //increase the full-stop count
:          }/*end else-if*/
:      } /* end if (isalnum */ 
:   }/*end while*/
: 
:   printf("%s\n%s\n", "Number of vowels = %d", countvowel,
:                      "Number of full-stop = %d", countfs);
: 
: 
:   fclose(fp);    //Close the file
: 
:   fflush(stdin); //Hold the screen
:   getchar();
: 
: } /*end main*/
: 

:
See code in red above.

Exactly because of these problems I always preferred to following indention-method:
#include <stdio.h>
#include <ctype.h>

void main()
{
  int data;           //Holds the value of the content in the file
  FILE* fp;           //FILE pointer
  int countvowel = 0; //Count the number of vowels
  int countfs = 0;    //Counte the number of full-stop
  fp = fopen("properties.txt", "r"); //Opens the file for reading, save the containt in the pointer fp
  if (fp == NULL)
  {
    printf("Failed to open file\n");
    return;                          //Leave the programme
  }

  //Otherwise, if the file opened,
  //it loop to search the vowels and full-stop
  while ((data = getc (fp)) != EOF)
  {
    if(isalnum(data))
    {
      if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u')
      {
         countvowel++;               //increase the vowel count
      }
      else if (data == '.')
      {
        countfs++;                 //increase the full-stop count
      }
    }
  }
  printf("%s\n%s\n", "Number of vowels = %d", countvowel,
                     "Number of full-stop = %d", countfs);


  fclose(fp);    //Close the file

  fflush(stdin); //Hold the screen
  getchar();

}


But it is just how you like it.

By the way, your vowel-test is ok ofcourse, but you may consider using a switch-case construction:
switch (data)
{
  case 'a': case 'e' : case 'i': case 'o': case 'u':
     countvowel++; break;
  case '.':
     countfs++; break;
}

It is somewhat easier to read and if you want to expand the tests with other comparisons, you don't end up with hard-to-read-nested=if-else-if's.
But it is a matter of opinion.

Another thing: it is not the debugger that reports the error, but the compiler. Two different things...

Greets,
Eric Goldstein
www.gvh-maatwerk.nl


Report
Re: The programme to read-in frm FILE and prints out Posted by tokoG on 22 Apr 2006 at 7:53 PM
Hi Eric

Thanks for the advise, now my code is much better but doesn't perform perfect.

I got the Number of vowels alright.
(Noticed that I was missing all the UPPERCASES so I added the following code);

      if (isupper(data)) {           //If uppercase,
        data = tolower(data);        //change it to the lowercase


PROBLEM
But, I can't get the Number of vowels alright.
There should be 3 of them but it outputs 0.
I wonder what is wrong..? ('.' is OK, right..?)


Want to Improve
I wondered if I could do like this;

printf("%s\n%s\n", "Number of vowels = %d", countvowels,
                   "Number of full-stops = %d", countfs);


But this didn't work. So I used the convensional way that is to use two printf and print one sentence each, then it works.
But, I want to know what went wrong ith the above code..?



This is the entre code after the modification
It still doesn't output the number of full-stop correctly.

#include <stdio.h>
#include <ctype.h>

void main() {

  int data;           //Holds the value of the content in the file
  FILE* fp;           //FILE pointer
  int countvowel = 0; //Count the number of vowels
  int countfs = 0;    //Counte the number of full-stop
  fp = fopen("properties.txt", "r"); //Opens the file for reading, save the containt in the pointer fp
  if (fp == NULL) {                  //If the file opening failed, program exit
    printf("Failed to open file\n");
    return;                          //Leave the programme
  }/*end if*/


  //Otherwise, if the file opened,
  //it loop to search the vowels and full-stop
  while ((data = getc (fp)) != EOF) {//Loops until the EOF
    if(isalnum(data)) {              //If data is a char or a digit
      if (isupper(data)) {           //If uppercase,
        data = tolower(data);        //change it to the lowercase
       }/*end if*/
       if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u'){//If it finds the vowel,
          countvowel++;               //increase the vowel count
       }

    else if (data == '.'){      //If not vowel, but full-stop,
      countfs++;                 //increase the full-stop count
    }/*end else-if*/

    }/*end if(isalnum)*/
  }/*end while*/

  printf("Number of vowels = %d\n", countvowel);
  printf("Number of full-stops = %d\n", countfs);


  fclose(fp);    //Close the file

  fflush(stdin); //Hold the screen
  getchar();

} /*end main*/






Thanks for the idea for using switch.
I agree, it looks much better.
(But this time, I just consentraited modifying the above code. ;) )

Cheers!!
Report
Re: The programme to read-in frm FILE and prints out Posted by tsagld on 23 Apr 2006 at 2:07 PM
: Hi Eric
:
: Thanks for the advise, now my code is much better but doesn't perform perfect.
:
: I got the Number of vowels alright.
: (Noticed that I was missing all the UPPERCASES so I added the following code);
:
:
:       if (isupper(data)) {           //If uppercase,
:         data = tolower(data);        //change it to the lowercase
: 

:

Why the extra test? Just do
data = tolower(data);// (without the if)

If data is already lowercase, the function doesn't do anything...


: PROBLEM
: But, I can't get the Number of vowels alright.
: There should be 3 of them but it outputs 0.
: I wonder what is wrong..? ('.' is OK, right..?)

No idea, your code seems fine.

:
: Want to Improve
: I wondered if I could do like this;
:
:
: printf("%s\n%s\n", "Number of vowels = %d", countvowels,
:                    "Number of full-stops = %d", countfs);
: 

:
: But this didn't work. So I used the convensional way that is to use two printf and print one sentence each, then it works.
: But, I want to know what went wrong ith the above code..?

You can't nest modifiers this way.
However, you can do this:
printf("Number of vowels = %d\r\nNumber of full-stops = %d\r\n", countvowels, countfs);


:
:
: This is the entre code after the modification
: It still doesn't output the number of full-stop correctly.

Again, no idea. Your code is fine as it is printed here.

:
: #include <stdio.h>
: #include <ctype.h>
: 
: void main() {
: 
:   int data;           //Holds the value of the content in the file
:   FILE* fp;           //FILE pointer
:   int countvowel = 0; //Count the number of vowels
:   int countfs = 0;    //Counte the number of full-stop
:   fp = fopen("properties.txt", "r"); //Opens the file for reading, save the containt in the pointer fp
:   if (fp == NULL) {                  //If the file opening failed, program exit
:     printf("Failed to open file\n");
:     return;                          //Leave the programme
:   }/*end if*/
: 
: 
:   //Otherwise, if the file opened,
:   //it loop to search the vowels and full-stop
:   while ((data = getc (fp)) != EOF) {//Loops until the EOF
:     if(isalnum(data)) {              //If data is a char or a digit
:       if (isupper(data)) {           //If uppercase,
:         data = tolower(data);        //change it to the lowercase
:        }/*end if*/
:        if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u'){//If it finds the vowel,
:           countvowel++;               //increase the vowel count
:        }
: 
:     else if (data == '.'){      //If not vowel, but full-stop,
:       countfs++;                 //increase the full-stop count
:     }/*end else-if*/
: 
:     }/*end if(isalnum)*/
:   }/*end while*/
: 
:   printf("Number of vowels = %d\n", countvowel);
:   printf("Number of full-stops = %d\n", countfs);
: 
: 
:   fclose(fp);    //Close the file
: 
:   fflush(stdin); //Hold the screen
:   getchar();
: 
: } /*end main*/
: 
: 
: 
: 
: 


: Thanks for the idea for using switch.
: I agree, it looks much better.
: (But this time, I just consentraited modifying the above code. ;) )
:
: Cheers!!
:

One more tip:
Instead of isalnum, you may use isalpha. That only tests for alphabetic characters and is probably somewhat faster than isalnum.


Greets,
Eric Goldstein
www.gvh-maatwerk.nl


Report
Re: The programme to read-in frm FILE and prints out Posted by Ed Hall on 24 Apr 2006 at 7:58 AM
: PROBLEM
: But, I can't get the Number of vowels alright.
: There should be 3 of them but it outputs 0.
: I wonder what is wrong..? ('.' is OK, right..?)

I think you meant full stops above instead of vowels. The reason is that the isalnum doesn't allow you to test for the full stop. You should be able to remove the isalnum test and just run the vowel and full stop checks on the entire file.

Another note: Perhaps your template limitation is the reason, but main should return an int instead of being void.

Take Care,
Ed


Report
Re: The programme to read-in frm FILE and prints out Posted by tsagld on 24 Apr 2006 at 8:44 AM
: : PROBLEM
: : But, I can't get the Number of vowels alright.
: : There should be 3 of them but it outputs 0.
: : I wonder what is wrong..? ('.' is OK, right..?)
:
: I think you meant full stops above instead of vowels. The reason is that the isalnum doesn't allow you to test for the full stop. You should be able to remove the isalnum test and just run the vowel and full stop checks on the entire file.


Ah, I see it too now. The test for '.' occurs within the if (isalnum) compound. That's the reason.


: Another note: Perhaps your template limitation is the reason, but main should return an int instead of being void.
:
: Take Care,
: Ed

:
:


Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl/english/E_index.htm


Report
Re: The programme to read-in frm FILE and prints out Posted by tokoG on 22 Apr 2006 at 8:27 PM

: Another thing: it is not the debugger that reports the error, but the compiler. Two different things...
:
: Greets,
: Eric Goldstein
: www.gvh-maatwerk.nl
:
:

Hi Eric

One more thing. About the debugger & compiler.
I was reporting the problem I am having with my C++Builder to the Borland and I also noticed that I am misunderstanding what debugger and complier is.

Noted it's the complier that reports the error, but what is debugger? Isn't it the one comes out after the compling with those green allows to tell us where to modify??



Report
Re: The programme to read-in frm FILE and prints out Posted by tsagld on 23 Apr 2006 at 2:13 PM
:
: : Another thing: it is not the debugger that reports the error, but the compiler. Two different things...
: :
: : Greets,
: : Eric Goldstein
: : www.gvh-maatwerk.nl
: :
: :
:
: Hi Eric
:
: One more thing. About the debugger & compiler.
: I was reporting the problem I am having with my C++Builder to the Borland and I also noticed that I am misunderstanding what debugger and complier is.
:
: Noted it's the complier that reports the error, but what is debugger? Isn't it the one comes out after the compling with those green allows to tell us where to modify??
:
:
The debugger is the tool you can use to run your code in 'debug-mode'.
Usually, an IDE comes with a debugger. It allows you to watch your program flow, variable-changes, and, most important, to set breakpoints at source-code lines. Your code will be suspended when it comes to a breakpoint, allowing you to inspect your program's state.
If you've never used a debugger, please start to do so. You don't know what you are missing.


Greets,
Eric Goldstein
www.gvh-maatwerk.nl


Report
Re: The programme to read-in frm FILE and prints out Posted by tokoG on 24 Apr 2006 at 8:06 PM
: :
: The debugger is the tool you can use to run your code in 'debug-mode'.
: Usually, an IDE comes with a debugger. It allows you to watch your program flow, variable-changes, and, most important, to set breakpoints at source-code lines. Your code will be suspended when it comes to a breakpoint, allowing you to inspect your program's state.
: If you've never used a debugger, please start to do so. You don't know what you are missing.
:
:
: Greets,
: Eric Goldstein
: www.gvh-maatwerk.nl
:

Hi Eric

I use C++Builder 6.0 and I think it comes with the debugger and I think I've been using it.. Never set it but when I for example tried to use struct and hen I tried to type variables[ the pop up comes out and tells me the type (int or float) of the variable I am typing in (or should type in). Is that what you call debugger?

Also, if I compile and it executes the programme but it stopps hen it finds the error for example that I used scanf and wrote as follows;

scanf("%d", anInt); <---- forgetting the ampersand & for &anInt

Then, it stops right there and points the incorrect sentence with the green arrow.

Is that debugger...?




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.