hy guys could you help me with an assignement in c++:
Write a program that receives the input of letters (uppercase and lowercase) until it enters the small letter 'a'. Count how many consonants are in the input.
hy guys could you help me with an assignement in c++:
Write a program that receives the input of letters (uppercase and lowercase) until it enters the small letter 'a'. Count how many consonants are in the input.
Announcements - General Programming Boards
Make an effort, then we help.
Don't just dump your assignment, this isn't a code on demand service.
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.
Do I understand correctly your question?Code:#include <stdio.h> #include <conio.h> #include <ctype.h> void main() { int count=0; char c; printf("Enter a charracter:"); do { c=getchar(); fflush(stdin);//If you type two characters at a time only recieve the first charracter. char t=toupper(c); if (t!='A'&&t!='U'&&t!='I'&&t!='O'&&t!='E') count++; }while (c!='a'); printf("\nNumber of consonants:%d",count); getch(); }
Sorry my english is very bad!.
Last edited by cauberong09; 01-04-2012 at 08:59 PM.
You are not supposed to be posting full solutions for people!
If you do not understand the homework policy that you are supposed to have read when you signed up here, then you should not be using this forum.
I recommend you find a forum in your native language for now, and keep to that.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Explain to me why it is undefined behavior?.
fflush - C++ Reference
Did you bother to read that page at all?
It explains that fflush on input streams are not well defined... in the 2nd sentence.
Also..read this : http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Last edited by manasij7479; 01-05-2012 at 03:39 AM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
okay here i am sory for not answering so long.. first i want to say that when i came here i didnt really expect whole source code written like i m to lazy to do anything by myself.. i idint even have an idea how to start so i was hoping of something to help me begin.. but thanks to cauberong09 for posting code.. it does not do quite what i need. i need it to read characters from single line without hitting "enter", like tyiping text and to stop a loop when "a" is pressed.. after that i need it to count consonants.. could somone explain me please what this line does exactly: "while (c!='a');".. im little confused with condition in while loop
In normal english, its:
While the variable c is not equal to small a
c is the variable, defined on line 7 and initialized on line 11
!= is the not equal to operator
and putting ' around the 'a' makes the compiler treat it literally, as opposed to a variable.
ok so i corrected code a little so it does read all the characters in one line and i used "char=ce" instead of "char=c" because i need "c" to count as consonant ..Code:#include <stdio.h> #include <conio.h> #include <ctype.h> int main() { int count=0; char ce; printf("Enter a charracter:"); do { ce=getchar(); char t=toupper(ce); if (t!='A'&&t!='U'&&t!='I'&&t!='O'&&t!='E') count++; }while (ce!='a'); printf("\nNumber of consonants:%d",count); getch(); }
you should add && t!= 'a' && t!= 'u', etc to line 13, otherwise it will count all of the lowercase vowels as consonant.
how about if a change it like thisCode:#include <stdio.h> #include <conio.h> #include <ctype.h> int main() { int count=0; char ce; printf("Enter a charracter:"); do { ce=getchar(); char ce=tolower(ce); if (ce!='a'&&ce!='u'&&tce='i'&&ce!='o'&&ce!='e') count++; }while (ce!='a'); printf("\nNumber of consonants:%d",count); getch(); }
Well, this is embarrassing.
I'm assuming tolower and toupper change the case of a letter?
In that case, the first version (or the new version) would work just fine. Oops :/