:
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