hello everyone I really need help so I have to make functions that find vowels, digits and uppercase and lowercase letters from a sentence inputted. So i was able to do it all in main but i cant seem to make the new program work with the functions properly, it says on the compiler that it has no errors but ones i debug it, it shoes the wrong answers. would really appreciate the help.

this is what i have so far with the functions

#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;

int findUpper(char ch); //function to find the uppercase letters in the sentence.
int findLower(char ch); //function to find the lowercase letters in the sentence.
int findDigits(char ch); //function to find the digits in the sentence.
int findVowels(char ch); //function to find the vowels in the sentence.
void display (char ch, int upcount, int lowcount, int digitcount, int vowelcount ); // function that outputs the results.

int main()
{
int upcount; //variable for number of uppercase letters.
int lowcount; //variable for number of lowercase letters
int digitcount; //variable for the number of digits.
int vowelcount; //variable for the number of vowels.
char ch;

//ask user to enter sentence and finds the uppercase letters, lowercase letters, digits and vowels in the sentence.
cout<<"Please enter a sentence. :"<<endl;
cin>>ch;

upcount=findUpper(ch);
lowcount=findLower(ch);
digitcount=findDigits(ch);
vowelcount=findVowels(ch);
display (ch, upcount, lowcount, digitcount, vowelcount );

return 0;
}

int findUpper(char ch)
{
int upcount=0;

while (ch != '\n')
{
if (isupper(ch)) upcount++;
cin.get(ch);
}
return upcount;

}

int findLower(char ch)
{
int lowcount=0;

while (ch != '\n')
{
if (islower(ch)) lowcount++;
cin.get(ch);
}

return lowcount;
}

int findDigits(char ch)
{
int digitcount=0;

while (ch != '\n')

{
if (isdigit(ch)) digitcount++;
cin.get(ch);
}

return digitcount;
}

int findVowels(char ch)
{
int vowelcount=0;

while (ch != '\n')
{
ch=tolower(ch);
if ((ch == 'a')||(ch == 'e')||(ch == 'i')||(ch == 'o')||(ch == 'u')) ++vowelcount;
cin.get(ch);
}

return vowelcount;
}

void display(char ch, int upcount, int lowcount, int digitcount, int vowelcount )
{
cout<<setfill('.');
cout<<left<<setw(24)<<"No. of uppercase letters"
<<right<<setw(10)<<upcount<<endl;
cout<<left<<setw(24)<<"No. of lowercase letters"
<<right<<setw(10)<<lowcount<<endl;
cout<<left<<setw(24)<<"No. of digits letters"
<<right<<setw(10)<<digitcount<<endl;
cout<<left<<setw(24)<<"No. of vowels letters"
<<right<<setw(10)<<vowelcount<<endl;

}

this is without functions which i was able to do but i need to make it into functions

#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;

int main()
{
int upcount=0, lowcount=0, digitcount=0, vowelcount=0;
char ch;

cout<<"Please enter a sentence. :"<<endl;

cin.get(ch);

while (ch != '\n')
{
if (isupper(ch)) upcount++;
if (islower(ch)) lowcount++;
if (isdigit(ch)) digitcount++;
ch=tolower(ch);
if ((ch == 'a')||(ch == 'e')||(ch == 'i')||(ch == 'o')||(ch == 'u')) ++vowelcount;
cin.get(ch);
}

Thank you to whoever can help me. :D

Dani AI

Generated

Good news — the problem wasn’t a mysterious compiler bug but how the input was being read and who was reading it. Using cin >> ch reads a single non‑whitespace character, and having each helper function call cin.get(...) means the first helper you call will consume the rest of the line so later helpers see nothing. was right to suggest using a string; , your final change to make the helpers pure predicates (returning 0/1) and drive them from a single loop in main is exactly the safer pattern.

Recommended approach:

  • Read the whole line into a std::string with std::getline(std::cin, line). That preserves spaces and gives you exactly one place to iterate.
  • Do a single pass over the string and update all counters in one loop for efficiency.
  • Make helper functions pure (take a char or unsigned char and return bool/int) so they don’t touch std::cin. That removes surprising side effects and makes them easy to test.

A minimal example pattern (not a copy of earlier code):

#include <string>
#include <cctype>

std::size_t countUpper(const std::string& s) {
    std::size_t n = 0;
    for (char ch : s)
        if (std::isupper(static_cast<unsigned char>(ch))) ++n;
    return n;
}

int main() {
    std::string line;
    std::getline(std::cin, line);
    // call countUpper/countLower/countDigits/countVowels or do one-pass counting here
}

Extra notes: always cast to unsigned char when calling <cctype> functions to avoid undefined behavior on negative char values; use std::tolower(static_cast<unsigned char>(ch)) before vowel checks; prefer std::size_t for counts. If you need Unicode-aware classification (accented letters, non‑Latin scripts) use a proper Unicode library — the C ctype functions handle only the current C locale / single‑byte chars.

Recommended Answers

All 2 Replies

First things first.

You must use array of storing sentences or string datatype.

Line 24 : Its simply takes the first character of you input.

ok thanks for the help but i actually got it now

#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;

int findUpper(char ch); //function to find the uppercase letters in the sentence.
int findLower(char ch); //function to find the lowercase letters in the sentence.
int findDigits(char ch); //function to find the digits in the sentence.
int findVowels(char ch); //function to find the vowels in the sentence.
void display (char ch, int upcount, int lowcount, int digitcount, int vowelcount ); // function that outputs the results.

int main()
{
int upcount=0; //variable for number of uppercase letters.
int lowcount=0; //variable for number of lowercase letters
int digitcount=0; //variable for the number of digits.
int vowelcount=0; //variable for the number of vowels.
char ch;

//ask user to enter sentence and finds the uppercase letters, lowercase letters, digits and vowels in the sentence. 
    cout<<"Please enter a sentence. :"<<endl; 
    cin.get(ch); 
    while (ch != '\n') 
    {
        upcount += findUpper(ch);
        lowcount += findLower(ch);
        digitcount += findDigits(ch);
        vowelcount += findVowels(ch);
        cin.get(ch);
    }  
    display (ch, upcount, lowcount, digitcount, vowelcount );

return 0;
}

int findUpper(char ch)
{
// return 1 if the charater is upper case. Else return 0. 
    if (isupper(ch)) return 1; 
    return 0;
} //End findupper() 



int findLower(char ch)
{ 
    if(islower(ch)) return 1; 
    return 0; 
}

int findDigits(char ch)
{
//int digitcount=0;

if (isdigit(ch)) return 1;
return 0;
}

int findVowels(char ch)
{
//int vowelcount=0;

ch=tolower(ch);
if ((ch == 'a')||(ch == 'e')||(ch == 'i')||(ch == 'o')||(ch == 'u')) return 1;
return 0; 
}

void display(char ch, int upcount, int lowcount, int digitcount, int vowelcount )
{
cout<<setfill('.');
cout<<left<<setw(24)<<"No. of uppercase letters"
<<right<<setw(10)<<upcount<<endl;
cout<<left<<setw(24)<<"No. of lowercase letters"
<<right<<setw(10)<<lowcount<<endl;
cout<<left<<setw(24)<<"No. of digits letters"
<<right<<setw(10)<<digitcount<<endl;
cout<<left<<setw(24)<<"No. of vowels letters"
<<right<<setw(10)<<vowelcount<<endl;
} 

now it works perfectly

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.