hey guys i did this program and its not working it worked for a while and then stopped so would someone please tell me whats wrong with it cause i have to do it soon.

#include <conio.h>   
#include <stdio.h>   
#include <stdlib.h>  


void main()
{
    char sentence[50];  
    int vowel_a; 
    int vowel_e; 
    int vowel_i;    
    int vowel_o; 
    int vowel_u; 
    int non_vowels; 

    float total;
    float perc_a;
    float perc_e;
    float perc_i;   
    float perc_o;
    float perc_u;
    float perc_rest;

    vowel_a = vowel_e = vowel_i = vowel_o = vowel_u= non_vowels = 0;    
    printf("Enter your desired sentence: ");    
    scanf("%49[^\n]%c", &sentence);     

    for(int x = 0; sentence[x]!='\0';x++)
    {
        if(sentence[x]=='a')    
            vowel_a++;              
        if(sentence[x]=='e')
            vowel_e++;
        if(sentence[x]=='i')
            vowel_i++;
        if(sentence[x]=='o')    
            vowel_o++;                      if(sentence[x]=='u')
            vowel_u++;
        if(sentence[x]!='a'&&sentence[x]!='e'&&sentence[x]!='i'&&sentence[x]!='o'&&sentence[x]!='u')
            non_vowels++;
    }

    system("cls");

    printf("You Entered: %s\n\n", sentence);

    printf("Number of characters:\n\n");
    printf("a = %d\t", vowel_a);
    printf("e = %d\t", vowel_e);
    printf("i = %d\t", vowel_i);    
    printf("o = %d\t", vowel_o);
    printf("u = %d\t", vowel_u);
    printf("rest = %d\n\n", non_vowels);


    total = vowel_a + vowel_e + vowel_i + vowel_o + vowel_u + non_vowels;


    perc_a = (vowel_a / total) * 100;
    perc_e = (vowel_e / total) * 100;
    perc_i = (vowel_i / total) * 100;   
    perc_o = (vowel_o / total) * 100;
    perc_u = (vowel_u / total) * 100;
    perc_rest = (non_vowels / total) * 100;


    printf("Percentages of total:\n");
    printf("a = %.2f%%  ", perc_a);
    printf("e = %.2f%%  ", perc_e);
    printf("i = %.2f%%  ", perc_i);
    printf("o = %.2f%%  ", perc_o);
    printf("u = %.2f%%  ", perc_u);
    printf("rest = %.2f%%\n\n", perc_rest);

    getch();
}

The question that comes with this program is:

  1. Write a C/C++ program to count the vowels and letters from the keyboard (maximum 50 characters). The string may contain spaces.
    Then it prints out the number of occurrences of each of the vowels a, e, i, o and u, the total number of letters, and each of the vowels as an integer percentage of the letter total.

If the input contains
What a beautiful day!

Suggested output format is:
You enterted: What a beautiful day!

Numbers of characters:

a:4      e:1     i:1     o:0     u:2     rest:13

Percentages of total:

a:19.05%    e:4.76%   i:4.76%    o:0.00%    u:9.52%    rest:61.90%

Dani AI

Generated

Quick summary of what I saw and why the program behaved oddly: was right to point out int main() (use a proper return value), and ’s hint about the scanf format was on the right track — the input-reading is the main culprit. A trailing %c on that scanf will try to consume one more character (often the leftover newline) and can leave the input in an unexpected state; also the code treats every non‑vowel character (spaces, punctuation, digits) as “rest.” That matches the sample output in the original post (the sample used total = all characters), but the problem text is ambiguous when it says “total number of letters.” Decide first whether “total” means all characters or only alphabetic letters.

Practical fixes and rules of thumb:

  • Read the whole line with fgets() (safer than scanf) and strip the trailing newline.
  • Decide the counting rule: if you want letters only, use isalpha() and count non‑vowel alphabetic characters as “rest”; if you want all characters, skip isalpha() and treat any non‑nul char as part of the total.
  • Normalize with tolower((unsigned char)ch) before comparing so uppercase vowels are counted.
  • Guard against division by zero before computing percentages.
  • Drop nonstandard headers (conio.h), getch() and system("cls") for portable code.

Example (letters-only approach):

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

int main(void)
{
    char sentence[51];
    int a=0,e=0,i_v=0,o=0,u=0,rest=0;
    if (!fgets(sentence, sizeof sentence, stdin)) return 0;
    size_t len = strlen(sentence);
    if (len && sentence[len-1]=='\n') sentence[len-1]='\0';

    for (size_t k=0; sentence[k]; ++k) {
        unsigned char ch = (unsigned char)sentence[k];
        if (isalpha(ch)) {
            switch (tolower(ch)) {
                case 'a': ++a; break;
                case 'e': ++e; break;
                case 'i': ++i_v; break;
                case 'o': ++o; break;
                case 'u': ++u; break;
                default: ++rest; break;
            }
        }
    }

    int total = a + e + i_v + o + u + rest;
    printf("You entered: %s\n", sentence);
    printf("a=%d e=%d i=%d o=%d u=%d rest=%d\n", a,e,i_v,o,u,rest);
    if (total) printf("a=%.2f%% e=%.2f%% i=%.2f%% o=%.2f%% u=%.2f%% rest=%.2f%%\n",
        a*100.0/total, e*100.0/total, i_v*100.0/total, o*100.0/total, u*100.0/total, rest*100.0/total);
    else printf("No letters to compute percentages.\n");
    return 0;
}

Extra notes: if you need to reproduce the exact sample output that counts spaces/punctuation, remove the isalpha() check and increment rest for any character that isn’t a vowel. Also always cast to unsigned char before passing to tolower/isalpha to avoid undefined behavior on negative char values.

Recommended Answers

All 4 Replies

Code Tags!
oh and void main is wrong it should be int main.

what do u mean by the code tags cause i changed it to int main()
and then it still does

remove %c from scanf...

//scanf("%49[^\n][B]%c[/B]", &sentence);
scanf("%49[^\n]", &sentence);

Thanxs guys that works
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.