hey guys i got a problem with my program i cant get it to work properly.

The question is:
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%

and this is my program:

#include<stdio.h>
#include<iostream.h> 
#include<stdlib.h> 
#include<string.h> 

void main() 
{ 
	int j,k; 
	float a,e,i,o,u;
	int A,E,I,O,U; 
	char word[50]; 


	printf("Enter First string: ");
	scanf("%49[^\n]%c", word);

    word = strlen(word); 


for(j=0;j<=k;j++)
{ 
    if ('a'==word[j])     
            a++; 

    if ('e'==word[j])     
            e++; 

    if ('i'==word[j])     
            i++; 

    if ('o'==word[j])     
            o++; 

    if ('u'==word[j])     
            u++; 
} 

    printf("You Typed: "); 
    printf("a:%d\t e:%d\t i:%d\t o:%d\t u:%d\t ", a, e, i, o, u); 
   

    printf("Percantage Of Letter:"); 
	A=(a/k)*100;
	E=(e/k)*100;
	I=(i/k)*100;
	O=(o/k)*100;
	U=(u/k)*100;
    printf("a: %f\t e:%f\t i:%f\t o:%f\t u:%f\t", A, E, I, O, U); 

}

Thanxs

<< moderator edit: added [code][/code] tags >>

Dani AI

Generated

— glad you got it working and thanks to for catching the initialization/type problems. Below is a cleaner, safer C++ approach that keeps the 50-character input limit, treats vowels case-insensitively, counts only alphabetic characters for the total (so spaces/punctuation are excluded), formats percentages to two decimals, and avoids division-by-zero and signed-char issues.

#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <array>

int main() {
    std::string s;
    std::cout << "Enter a line (max 50 chars): ";
    if (!std::getline(std::cin, s)) return 0;
    if (s.size() > 50) s.resize(50);

    std::array<int,5> v{0,0,0,0,0}; // a,e,i,o,u
    int letters = 0;
    for (char c : s) {
        unsigned char uc = static_cast<unsigned char>(c); // safe for ctype
        if (std::isalpha(uc)) {
            ++letters;
            char lc = static_cast<char>(std::tolower(uc));
            if (lc == 'a') ++v[0];
            else if (lc == 'e') ++v[1];
            else if (lc == 'i') ++v[2];
            else if (lc == 'o') ++v[3];
            else if (lc == 'u') ++v[4];
        }
    }

    int sum_v = v[0]+v[1]+v[2]+v[3]+v[4];
    int rest = letters - sum_v;

    std::cout << "You entered: " << s << '\n';
    std::cout << "Numbers of characters:\n";
    std::cout << "a:" << v[0] << " e:" << v[1] << " i:" << v[2]
              << " o:" << v[3] << " u:" << v[4] << " rest:" << rest << '\n';

    std::cout << "Percentages of total:\n" << std::fixed << std::setprecision(2);
    if (letters == 0) std::cout << "No letters to calculate percentages\n";
    else {
        auto pct = [&](int x){ return (x * 100.0) / letters; };
        std::cout << "a:" << pct(v[0]) << "% e:" << pct(v[1]) << "% i:" << pct(v[2])
                  << "% o:" << pct(v[3]) << "% u:" << pct(v[4]) << "% rest:" << pct(rest) << "%\n";
    }
    return 0;
}

Notes and troubleshooting:

  • Cast chars to unsigned char before std::isalpha / std::tolower to avoid undefined behavior (see isalpha docs).
  • Use std::getline for lines with spaces (see std::getline).
  • Always initialize counters, use int main() with return 0, and check letters == 0 before dividing.
  • For Unicode (multi-byte) input a byte-based loop will not count graphemes correctly; use a Unicode-aware library if needed.

Recommended Answers

All 2 Replies

Okay here goes. You had got your types mixed up and the casting was not done properly. You had assigned floats to integers there by losing information. Also initialize every variable you define. Otherwise you will have unexpected results. I corrected them and basically got it to work. Also I changed the scanf line too. Yours maybe correct, but I prefer this one.

Here is my code. Compare with your program to see the changes I have made,

#include<stdio.h>
#include<iostream> 
#include<stdlib.h> 
#include<string.h> 
using namespace std;

void main() 
{ 
	int j,k; 
	int a = 0,e= 0,i= 0,o= 0,u= 0;
	float A,E,I,O,U; 
	char word[50]; 


	printf("Enter First string: ");
	cin.getline( word, 50, '\n');

	k = strlen(word); 


	for(j=0;j<=k;j++)
	{ 
		if ('a'==word[j]) 
		a++; 

		if ('e'==word[j]) 
		e++; 

		if ('i'==word[j]) 
		i++; 

		if ('o'==word[j]) 
		o++; 

		if ('u'==word[j]) 
		u++; 
	} 

printf("You Typed: "); 
printf("a:%d\t e:%d\t i:%d\t o:%d\t u:%d\t ", a, e, i, o, u); 


printf("Percantage Of Letter:"); 
A=( ( float )a/k)*100;
E=(( float )e/k)*100;
I=(( float )i/k)*100;
O=(( float )o/k)*100;
U=(( float )u/k)*100;
printf("a: %f\t e:%f\t i:%f\t o:%f\t u:%f\t", A, E, I, O, U); 

}

yeah thanxs heaps i got it to work

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.