1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ exercise

Discussion in 'C++' started by imported_PKing, Apr 9, 2007.

  1. imported_PKing

    imported_PKing New Member

    Joined:
    Apr 9, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Create a simple function...Please make it simple to understand!!! its for a beggining course in C++...create a function that allows the user to input text and that output the number of vowels that were entered.
     
  2. psppb

    psppb New Member

    Joined:
    Dec 26, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    create an array of characters and go down the list using an 'or' statement


    int numberofvouls = 0
    int x
    for(x = 0; x < number of characters; x++)
    if(char = a || e || i || o || u || y)
    {
    cout << "voul baby!" << endl;
    numberofvouls++
    }
    ..... something on that matter

    Bill
     
  3. crow

    crow New Member

    Joined:
    Nov 12, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Macedonia
  4. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    We won't do your homework for you. Post what you've got so far and show where you're stuck and what you don't understand.

    If you're really stuck why not ask the teacher, that's what he's paid for.
     
  5. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    Here is simple function:----------

    Code:
    void total_vowels()
    {
    char string[80];
    cout<<"\n enter your text\n";
    cin>>string;
    int novowels = 0
    int x
    for(x = 1; x <=80; x++)
    {
    if(string[x]== a || e || i || o || u )
    {
    novowels++
    }
    }
    cout<<"total no of vowels is :"<<novowels;
    }
     
  6. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    hkp819, Please learn to use code blocks in the posts where you have code in the posts
     
  7. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Simple yes, but did you actually try compiling it?
    I'd be interested to know what language accepts if(string[x]== a || e || i || o || u || y) as valid, and whether or not you got any kind of error on "novowels++".

    OP, if you want to learn, where are you stuck? Post what you've got so far, let us know what you're struggling with and we'll explain it to you.
     
  8. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    Yes, the above program posted by me is wrong. My mistake is in if statement. I have rectify it in this program....
    Code:
    void total_vowels()
    {
    char string[80];
    cout<<"\n enter your text\n";
    cin>>string;
    int novowels = 0
    int x
    for(x = 1; x <=79; x++)
    {
    if(string[x]== 'a' || string[x]== 'e' || string[x]== 'i' || string[x]== 'o' || string[x]== 'u')
    {
    novowels++
    }
    }
    cout<<"total no of vowels is :"<<novowels;
    }
    
     
  9. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    You still haven't compiled and run the program; I can still see at least one very obvious syntax error. Try building and running it, and make sure it produces the required output, THEN AND ONLY THEN is it a good idea to consider posting the result.

    Correction: three syntax errors, each of which will throw a compile time error, unless your compiler is VERY slack.
     
  10. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    I have compile it and it is working .
     
  11. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Your compiler is very slack then, because the following three statements should be terminated with a semicolon:
    Code:
    int novowels = 0
    int x
    novowels++
    
    What if you call the function twice, the first time entering the string "sausage" then on the second call "apple"? Do you get the correct scores 4 and 2?

    Edit: whoops, on second thoughts, no that won't show the bug I'm thinking of. Try aaaaaaaaaa and aaaaa (10 and 5 a's respectively). This should give you the results 10 and 5, but on inspection of the code I think you won't get these results.
     
  12. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    A non-debug build in Visual Studio 2005, fixing the above three syntax errors, gives the expected results 9 and 8. Can you see why?
     
  13. AidanPaine

    AidanPaine New Member

    Joined:
    Feb 26, 2009
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Security Guard
    Location:
    NY
    Let me give the above code a shot and let me see what I come up with.
    Will keep you up dated.
     

Share This Page