🔒 Closed Basic Turbo C++ programming : This code will enable the user to identify wether the letter is Vowel or Consonants ( Sana makatulong :) )

Status
Not open for further replies.

MrLoki28

Fanatic
#include <stdio.h>
#include <conio.h>
#define s scanf
#define p printf

int main()

{

char n;
clrscr();


p("Enter Letter:");
s("%s",&n);


if (n=='A'||n=='a'||n=='E'||n=='e'||n=='I'||n=='i'||n=='O'||n=='o'||n=='u'||n=='U')

{

p("The Letter you entered is Vowel");

}

else


{



p("The Letter you entered is Consonant");



}

getch();

return 0;


}
 
Improve your if statement to something like:

C++:
if(toupper(n) == 'A' || toupper(n) == 'E'...N)
{
    /Statement here...
}
 
Subukan mo ilagay between code tags yung program mo.

Eto clean version at may input validation pa for char input such that it only allows lower/uppercase alphabet.

But it won't validate the length of input though. To do that, you would need additional logic. For this simple exercise, this code will suffice.

Code:
#include <stdio.h>

int main(void) {
    char c;
    printf("Enter input: ");
    scanf("%s", &c);
    // ASCII code points for alphabetic characters
    while ((c < 65 || c > 122) || (c > 90 && c <= 96)) {
        printf("Invalid input. Re-enter: ");
        scanf("%s", &c);
       
    }
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
        printf("You entered: '%c' which is a VOWEL\n", c);
    }
    else {
        printf("You entered: '%c' which is a CONSONANT\n", c);
    }

    return 0;
}
 
Not the best algorithm but it does the job in a simplest form:

Simulate/Run this code using this link : You do not have permission to view the full content of this post. Log in or register now.
C++:
#include <iostream>
#include <string>
#include <locale>

int main ()
{
  std::locale loc;
  std::string str="";
  std::string st = "";

  std::cout << "Input a Character : ";
  std::cin >> str;

  /*This is actually to convert a string to it's lowercase form. You can use the tolower function without the foreach loop though*/
  for(char ch : str)
     st = std::tolower(ch,loc);

  if(st == "a" ||
      st == "e" ||
      st == "i" ||
      st == "o" ||
      st == "u")
  {
    std::cout << "The Letter you entered is a Vowel";
    return 0;
  }

std::cout << "The Letter you entered is not a Vowel";

}

What is happening here though?

I've declared a Locale header file so I can use the class locale to look for the culture info of the letter/string that will be inputted. Then use the overload method tolower() to convert the inputted character/string to lowercase so you dont need to write a lengthy if statement for the lower and the uppercase form of the character inputted.

Finally, the if statement just evaluate for a single case form of the inputted character if its within the "vowel" lists. Else, it displays the message "...not a Vowel".

Improve the code by incorporating input evaluation algorithm to avoid error while inputting undesired inputs.

Hope this helps!
 
please use tool chain like msvc, clang or gcc that support modern c++ unlike this 20-year-old borland pre-isocpp shit

We are familiar with that but this is for demonstration purposes which I think the same version as the students still use in their school.
 
Status
Not open for further replies.

About this Thread

  • 6
    Replies
  • 687
    Views
  • 4
    Participants
Last reply from:
aa1012

Trending Topics

Online now

Members online
1,032
Guests online
884
Total visitors
1,916

Forum statistics

Threads
2,274,901
Posts
28,959,245
Members
1,233,482
Latest member
MyNameIsElga
Back
Top