-1

I have an exercise asks me to correct the syntactic error, I tried to correct but every time I want to compile it shows me an error in the for loop.

(the exercise allows for counting and displays the number of vowel letters in a sentence entered by the user, the user indicates the end of the entry by typing '*')

#include <stdio.h>
main(){
char c;
char TV[5];
int k;
for (k = 0; k < 5; k++)
 {
 TV[k]=0;
  }
 printf("Entrer un texte. Tapez le caractére * pour sortire. \n");
 c = getchar();
while(c!='*')
{
  switch (c) {
     case 'A': TV[0]++;
     case 'a': TV[0]++;
     case 'E': TV[1]++;
     case 'e': TV[1]++;
     case 'I': TV[2]++;
     case 'i': TV[2]++;
     case 'O': TV[3]++;
     case 'o': TV[3]++;
     case 'U': TV[4]++;
     case 'u': TV[4]++;
    default: c = getchar();

   }
 }
 printf("a \t e \t i \t o \t u \n");
 for(k=0;k<5;k++)
 {
  printf("%d \t",TV[k]);
 }
 }

error message:

mariem@MIGI:~/Bureau/syt_exp$ gcc Tp6-lesChaines-Exercice1.c
Tp6-Channels-Exercise1.c:2:1: warning: return type defaults to ‘int’ [-Wreturn-type]
 main(){
 ^~~~
mariem@MIGI:~/Bureau/syt_exp$ ./Tp6-lesChaines-Exercice1.c
./Tp6-Channels-Exercise1.c: line 6: syntax error near the unexpected symbol "("
./Tp6-Channels-Exercise1.c: line 6: `for (k = 0; k <5; k ++) '

I think it's better now.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
MIGI
  • 23
  • 1
  • 3
  • 4
    post your code rathar than posting image with code because image will not visible but code is – jasinth premkumar Dec 21 '18 at 11:21
  • 3
    Please include the code/error message in the post – Ed Heal Dec 21 '18 at 11:21
  • Stackoverflow should deactivate image upload for users with lower reputation. – Thomas Sablik Dec 21 '18 at 11:26
  • 1
    @ThomasSablik No, that is a poor expectation. There are cases where the image is coherent part of the post. It's just that, code should not be posted as images - that's it. – Sourav Ghosh Dec 21 '18 at 11:50
  • @SouravGhosh Yes, images can be very important for a question but I rarely see an image as coherent part of a question asked by a user with reputation < 50. Comments are limited to privileged users. Images should be too – Thomas Sablik Dec 21 '18 at 12:04
  • @ThomasSablik You know, comments are always allowed for own posts, irrespective of the rep. How's a question any different? – Sourav Ghosh Dec 21 '18 at 12:05
  • @SouravGhosh Comments on others posts are limited to avoid misuse. Here we have a perfect example for misuse of images by an inexperienced user. Images as code from inexperienced user are common on SO – Thomas Sablik Dec 21 '18 at 12:08
  • @ThomasSablik I thought I was talking about own posts - as a question is a "own post" from OP. – Sourav Ghosh Dec 21 '18 at 12:11
  • @SouravGhosh A new user can't comment another users question to ask for details. He has to gain some reputation. Same thing would be good for usage of images. I've never seen a user with reputation > 100 posting an image of code – Thomas Sablik Dec 21 '18 at 12:12

4 Answers4

2

I don't see a syntax error. And I can't copy the code to check with my compiler. However:

main should be declared as

int main(int argc, char **argv)

Your loop: while (c=='*')?? You mean while (c!='*').

And: a case should be terminated with a break;, otherwise execution just continues.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • I mean while (c!=' *' ) I have already fixed it, and I don't understand why i have to put break after case ??? – MIGI Dec 21 '18 at 12:18
  • Read about `switch`: without a `break` the next statement will be executed, so after `case 'A': TV[0]++;` the `TV[0]++;` will be performed belinging to `case 'a':` – Paul Ogilvie Dec 21 '18 at 12:21
1

Why you don't have declarated type of main()?

int main()
Unknown123
  • 208
  • 2
  • 13
1

You are trying to execute the C source file directly in the shell, as if it was a shell script. C needs to be compiled, and then you execute the file created by the compiler.

The default name for this that GCC produces is a.out, so execute it with ./a.out

Zebra North
  • 11,412
  • 7
  • 37
  • 49
  • OP did compile it but he executed the source instead of the binary file – Thomas Sablik Dec 21 '18 at 11:36
  • i tried with int main() and it compiled but it stay give me the error message : linge2: syntax error near the unexpected symbol "(" – MIGI Dec 21 '18 at 13:25
  • @MIGI as MrZebra said: 1. Compile your code (`gcc Tp6-Channels-Exercise1.c`) and 2. execute the binary file (`./a.out`). Don't execute the source file (don't `./Tp6-Channels-Exercise1.c`). This is the correct answer to your problem. – Thomas Sablik Dec 21 '18 at 14:12
  • oh yes all this moment I executed the source file ,it work now thanks :) – MIGI Dec 21 '18 at 14:41
  • @MIGI Great ^.^ You can click the check mark next to the answer to accept it as being correct – Zebra North Dec 21 '18 at 16:55
0

A couple things...

1- to fix the warning - main(){ should be int main(){ OR even void main(){ works, but return type int is the most standard here. input parameters are optional - i.e. argv/argc.

2- after compilation (which you are doing correctly), without specifying an executable name, the default .exe produced by the compiler is either a.out or a.exe. to run, type ./a.exe on the command line.

Note - by doing those 2 things, I have confirmed that the program runs.

static_cast
  • 1,174
  • 1
  • 15
  • 21