[b][red]This message was edited by tokoG at 2006-4-20 22:13:4[/red][/b][hr]
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;
[blue]
Number of vowels = %d (<- number of vowels to be printed)
Number of full-stop = %d (<- number of full-stop to be printed)
[/blue]
I made the folloing code but debugger found error.
[blue]
} /*end main*/ [b]//Errr: Compound statement missing[/b]
[/blue]
[b][blue]if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u')[/blue][/b]
I purported to select the vowels. Is this OK..?
[b][blue] } /*end main*/[/blue][/b]
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!)
[code]
#include <stdio.h>
#include 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
");
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
%s
", "Number of vowels = %d", countvowel,
"Number of full-stop = %d", countfs);
fclose(fp); //Close the file
fflush(stdin); //Hold the screen
getchar();
} /*end main*/
[/code]
Comments
: 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;
:
: [blue]
: Number of vowels = %d (<- number of vowels to be printed)
: Number of full-stop = %d (<- number of full-stop to be printed)
: [/blue]
:
:
: I made the folloing code but debugger found error.
:
: [blue]
: } /*end main*/ [b]//Errr: Compound statement missing[/b]
: [/blue]
:
:
: [b][blue]if(data == 'a'||data == 'e'||data == 'i'|| data== 'o'||data == 'u')[/blue][/b]
: I purported to select the vowels. Is this OK..?
:
: [b][blue] } /*end main*/[/blue][/b]
: 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!)
: [code]
: #include <stdio.h>
: #include
:
: 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
");
: 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*/
: [red]} /* end if (isalnum */ [/red]
: }/*end while*/
:
: printf("%s
%s
", "Number of vowels = %d", countvowel,
: "Number of full-stop = %d", countfs);
:
:
: fclose(fp); //Close the file
:
: fflush(stdin); //Hold the screen
: getchar();
:
: } /*end main*/
: [/code]
:
See code in red above.
Exactly because of these problems I always preferred to following indention-method:
[code]
#include
#include
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
");
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
%s
", "Number of vowels = %d", countvowel,
"Number of full-stop = %d", countfs);
fclose(fp); //Close the file
fflush(stdin); //Hold the screen
getchar();
}
[/code]
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:
[code]
switch (data)
{
case 'a': case 'e' : case 'i': case 'o': case 'u':
countvowel++; break;
case '.':
countfs++; break;
}
[/code]
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
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 [b]UPPERCASES[/b] so I added the following code);
[code]
if (isupper(data)) { //If uppercase,
data = tolower(data); //change it to the lowercase
[/code]
[b][red]PROBLEM[/red][/b]
But, I can't get the [b]Number of vowels[/b] alright.
There should be [b]3[/b] of them but it outputs [b]0[/b].
I wonder what is wrong..? ('.' is OK, right..?)
[b][blue]Want to Improve[/blue][/b]
I wondered if I could do like this;
[code]
printf("%s
%s
", "Number of vowels = %d", countvowels,
"Number of full-stops = %d", countfs);
[/code]
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..?
[b]This is the entre code after the modification[b]
It still doesn't output the [b]number of full-stop[/b] correctly.
[code]
#include
#include
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
");
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
", countvowel);
printf("Number of full-stops = %d
", countfs);
fclose(fp); //Close the file
fflush(stdin); //Hold the screen
getchar();
} /*end main*/
[/code]
Thanks for the idea for using switch.
I agree, it looks much better.
(But this time, I just consentraited modifying the above code.
Cheers!!
: 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 [b]debugger[/b] and [b]complier[/b] is.
Noted it's the [b]complier[/b] 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??
:
: 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 [b]UPPERCASES[/b] so I added the following code);
:
: [code]
: if (isupper(data)) { //If uppercase,
: data = tolower(data); //change it to the lowercase
: [/code]
:
[green]
Why the extra test? Just do
[code]
data = tolower(data);// (without the if)
[/code]
If data is already lowercase, the function doesn't do anything...
[/green]
: [b][red]PROBLEM[/red][/b]
: But, I can't get the [b]Number of vowels[/b] alright.
: There should be [b]3[/b] of them but it outputs [b]0[/b].
: I wonder what is wrong..? ('.' is OK, right..?)
[green]
No idea, your code seems fine.
[/green]
:
: [b][blue]Want to Improve[/blue][/b]
: I wondered if I could do like this;
:
: [code]
: printf("%s
%s
", "Number of vowels = %d", countvowels,
: "Number of full-stops = %d", countfs);
: [/code]
:
: 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..?
[green]
You can't nest modifiers this way.
However, you can do this:
[code]
printf("Number of vowels = %d
Number of full-stops = %d
", countvowels, countfs);
[/code]
[/green]
:
:
: [b]This is the entre code after the modification[b]
: It still doesn't output the [b]number of full-stop[/b] correctly.
[green]
Again, no idea. Your code is fine as it is printed here.
[/green]
: [code]
: #include
: #include
:
: 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
");
: 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
", countvowel);
: printf("Number of full-stops = %d
", countfs);
:
:
: fclose(fp); //Close the file
:
: fflush(stdin); //Hold the screen
: getchar();
:
: } /*end main*/
:
:
:
:
: [/code]
: Thanks for the idea for using switch.
: I agree, it looks much better.
: (But this time, I just consentraited modifying the above code.
:
: Cheers!!
:
[green]
One more tip:
Instead of isalnum, you may use isalpha. That only tests for alphabetic characters and is probably somewhat faster than isalnum.
[/green]
Greets,
Eric Goldstein
www.gvh-maatwerk.nl
: : 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 [b]debugger[/b] and [b]complier[/b] is.
:
: Noted it's the [b]complier[/b] 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
: But, I can't get the [b]Number of vowels[/b] alright.
: There should be [b]3[/b] of them but it outputs [b]0[/b].
: I wonder what is wrong..? ('.' is OK, right..?)
[blue]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[/blue]
: : But, I can't get the [b]Number of vowels[/b] alright.
: : There should be [b]3[/b] of them but it outputs [b]0[/b].
: : I wonder what is wrong..? ('.' is OK, right..?)
:
: [blue]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.
[green]
Ah, I see it too now. The test for '.' occurs within the if (isalnum) compound. That's the reason.
[/green]
: Another note: Perhaps your template limitation is the reason, but main should return an int instead of being void.
:
: Take Care,
: Ed[/blue]
:
:
Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl/english/E_index.htm
: 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 [b]think[/b] it comes with the debugger and I [b]think[/b] I've been using it.. Never set it but when I for example tried to use [b]struct[/b] and hen I tried to type [b]variables[[/b] 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 [b]scanf[/b] and wrote as follows;
[b]scanf("%d", anInt);[/b] <---- forgetting the ampersand & for &anInt
Then, it stops right there and points the incorrect sentence with the [b]green arrow[/b].
Is that debugger...?