Thread: compiler error help

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    4

    compiler error help

    im trying to write a program that counts the total vowels in a string and count the number of each individual vowel(like total A's=4 and so on) Im stuck with my fisrt function to count the total vowels im getting a compiler error that says expected primary expression before '}' token.I cant figure out what that means can someone help


    Code:
    Code:
    #include <stdio.h>
     
         int countvowels(char []);
             //int displayleachvowels(char[]);
            
            
                      int main ()
                {
                   char sentence[150];
                   int vowels=0;
                
               
                      printf("Enter a sentence \n");
    
                       gets(sentence);
                   
                       vowels = countvowels(sentence);
                   
                       printf("Number of vowels: %d\n", sentence);
                   
                      // total=displayleachvowels(sentence);
                   
                        // printf("Total A's:,E's,I'sO's,U's, %d\n%d\n%d\n%d\n%d\n", sentence);
               
                    return 0;
               }
               int countvowels(char sentence[])
        
              {
                int x=0;
                int count=0;
           
                  for(x=0;sentence[x]!='\0';x++)
                 {
                   
                     if(sentence[x]=='a' || sentence[x]=='e' || sentence[x]=='i' ||
                  sentence[x]=='o' || sentence[x]=='u' || sentence[x]=='A' ||
                   sentence[x]=='E' || sentence[x]=='I' || sentence[x]=='O' ||
                    sentence[x]=='U')    
               
                   
        
                   } // error here
           
                  count++;    
               
              
           
                }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Well first of all, your indentation sucks.
    Sorry, but sloppy code leads to all sorts of errors like you describe if you can't see the real program flow at a glance.

    Pick a style, and stick to it rigorously.
    Indentation style - Wikipedia

    Here, like this:
    Code:
    #include <stdio.h>
    
    int countvowels(char[]);
    //int displayleachvowels(char[]);
    
    int main()
    {
      char sentence[150];
      int vowels = 0;
    
      printf("Enter a sentence \n");
      gets(sentence);
      vowels = countvowels(sentence);
      printf("Number of vowels: %d\n", sentence);
    
      // total=displayleachvowels(sentence);
      // printf("Total A's:,E's,I'sO's,U's, %d\n%d\n%d\n%d\n%d\n", sentence);
    
      return 0;
    }
    
    int countvowels(char sentence[])
    {
      int x = 0;
      int count = 0;
    
      for (x = 0; sentence[x] != '\0'; x++) {
        if (sentence[x] == 'a' || sentence[x] == 'e' || sentence[x] == 'i' ||
            sentence[x] == 'o' || sentence[x] == 'u' || sentence[x] == 'A' ||
            sentence[x] == 'E' || sentence[x] == 'I' || sentence[x] == 'O' || sentence[x] == 'U')
      }                         // error here
      count++;
    }
    Perhaps if you moved the count++ up a line, so it was actually INSIDE the for loop, you would have more success.

    Then we can talk about never using gets() in your programs.
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    thanks for the indentation style. i tried to move count up before when i do that i get an id returned 1 exit status error

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Line 14 has also a error
    Code:
    printf("Number of vowels: %d\n", sentence);
    I think it should be
    Code:
    printf("Number of vowels: %d\n", vowels);
    Other have classes, we are class

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I see room for improvement, put your vowels in an array then loop through it checking your sentence against it.

    uses two loops
    practice for nested loops

    Code:
    int vowels [5] = {'a','e','i','o','u'};
    // and an if 
    if( tolower( sentence[x] ) == vowels[b]) 
         count++;
    
    //gets
    
    $ ./checking_4_vowels
    Enter a sentence 
    hello aioeu
    Count : 7
    to get total a e i o u
    you now have a zero based array for your vowels you can use the numbers in your array to get a count on how many times they hit for each vowel then keep score like that before printing that out. maybe work it off (char which is an int) 'a' = "num of ascii code of char' even, OR something completely different, because they are just ideas off the top of my head. Therefore they are still just theories.

    If you have covered switches or know how to use them in accordance with char as int then you can do this with it
    Code:
    userx@slackwhere:~/bin
    $ ./checking_4_vowels
    Enter a sentence 
    hello you aeoiu
    a: 1 e: 2 i: 1 o: 3 u: 2
    Count : 9
    Last edited by userxbw; 11-04-2017 at 11:36 AM.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Prototype for the function that is capable of meeting your requirements.
    practice is passing values back through your prams.
    Code:
    int get_vowels(char s[], int *a, int *e, int *i, int *o, int *u)
    
    // out put
    
    $ ./checking_4_vowels
    Enter a sentence 
    gitty up yo eaiuo
    a: 1 e: 1 i: 2 o: 2 u: 2
    Count : 8
    Last edited by userxbw; 11-04-2017 at 12:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 10:02 PM
  2. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM
  3. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM
  4. Compiler error or human error?
    By skyruler54 in forum C++ Programming
    Replies: 6
    Last Post: 09-06-2002, 02:27 PM
  5. fatal error C1001: INTERNAL COMPILER ERROR
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 12:07 PM

Tags for this Thread