Thread: hekp with programming

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    hekp with programming

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    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();
    }
    Do I understand correctly your question?
    Sorry my english is very bad!.
    Last edited by cauberong09; 01-04-2012 at 08:59 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Quote Originally Posted by iMalc View Post
    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.
    I'm sorry,but I want to study here!.Of course,I has learning in the forum of my native language.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by cauberong09 View Post
    I'm sorry,but I want to study here!.Of course,I has learning in the forum of my native language.
    Btw.. in your post... you've used fflush(stdin) ...It is undefined behavior.
    (Explained in an analogy (by Adak ?) that it would be foolish to try flushing a faucet )

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Quote Originally Posted by manasij7479 View Post
    Btw.. in your post... you've used fflush(stdin) ...It is undefined behavior.
    (Explained in an analogy (by Adak ?) that it would be foolish to try flushing a faucet )
    Explain to me why it is undefined behavior?.
    fflush - C++ Reference

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by cauberong09 View Post
    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.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cauberong09 View Post
    I'm sorry,but I want to study here!.Of course,I has learning in the forum of my native language.
    There's a difference between studying things here and providing others with complete homework answers.
    If you want to learn things here then start by reading the FAQs, especially the ones about void main and fflush(stdin)!
    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"

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    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

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    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.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    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();
    }
    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 ..

  13. #13
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    you should add && t!= 'a' && t!= 'u', etc to line 13, otherwise it will count all of the lowercase vowels as consonant.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    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 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();
    }
    how about if a change it like this

  15. #15
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    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 :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  2. Simple Median hekp
    By nick753 in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2010, 02:06 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM