Need help, would be very appreciated.

This is a discussion on Need help, would be very appreciated. within the C Programming forums, part of the General Programming Boards category; I need help, I am in the middle of writing a program of minigames for children. I have so far ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Need help, would be very appreciated.

    I need help, I am in the middle of writing a program of minigames for children. I have so far completed a crossword, draughts and tic tac toe. I am currently in the middle of making a hang man program I have the basics worte but I can't think of a way to do this - Have the user enter a sentance and the program will output the number of vowels in the sentance. For example if someone wrote "My name is John." It will output "There are 4 vowels." Could someone please help its the last game for my program.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Iterate through the string, letter by letter. If that letter is equal to a, e, i, o, or u, then increment your vowel counter.

    Hopefully you don't have to handle the letter 'y'.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by bithub View Post
    Iterate through the string, letter by letter. If that letter is equal to a, e, i, o, or u, then increment your vowel counter.

    Hopefully you don't have to handle the letter 'y'.
    I really don't know how to do that though, I'm new to C programming and I havn't come across this yet.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes you could do something like this

    1. After the user enters the sentence make a character pointer point to the first character of the string.
    2. Then iterate through while you reach the end but with each iteration you check maybe using switch block
    2.1 if the alphabet is upper or lower case a,e,i,o,u
    2.2 If it is a vowel then keep incrementing the count
    2.3 if at the end of string('\0') terminate out this loop
    3. Print the number of times you figured count was incremented in 2.2

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    >> I really don't know how to do that though, I'm new to C programming and I havn't come across this yet.

    So you've completed a game of crossword, draughts, and tic tac toe, but you can't manage to count the vowels in a string? Sorry, but your "game for children" is starting to sound like a homework question to me. Not that it matters, but it usually helps to be up-front about those sorts of things (and forgive me if I am wrong).



    ITSA
    Socket Library!

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by roaan View Post
    Yes you could do something like this

    1. After the user enters the sentence make a character pointer point to the first character of the string.
    2. Then iterate through while you reach the end but with each iteration you check maybe using switch block
    2.1 if the alphabet is upper or lower case a,e,i,o,u
    2.2 If it is a vowel then keep incrementing the count
    2.3 if at the end of string('\0') terminate out this loop
    3. Print the number of times you figured count was incremented in 2.2
    #include <stdio.h>

    void Main (void) {
    char c;

    printf("Enter a sentence:\n");

    scanf("%c", &c);
    while (c != ".") {
    printf("%c\n", c);
    scanf("%c", &c);
    }

    printf(".\n");
    }
    So far I have this but I don't know where to put the integer or counter or what to put, I really only started this this week as a hobby and this one is really frustrating me.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I really don't know how to do that though, I'm new to C programming and I havn't come across this yet.
    A string is just an array of chars. Iterate through it with a for loop:
    Code:
    int i;
    for(i = 0; i < the_string_length; i++)
        // The letter is string[i]

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by Sebastiani View Post
    >> I really don't know how to do that though, I'm new to C programming and I havn't come across this yet.

    So you've completed a game of crossword, draughts, and tic tac toe, but you can't manage to count the vowels in a string? Sorry, but your "game for children" is starting to sound like a homework question to me. Not that it matters, but it usually helps to be up-front about those sorts of things (and forgive me if I am wrong).
    Im 28 and what homework would I be doing in July if I was in school? I appreciate you would hate kids doing this but I really only started this as a hobby.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    So far I have this but I don't know where to put the integer or counter or what to put, I really only started this this week as a hobby and this one is really frustrating me.
    You are obviously the same person who started this thread, and you obviously didn't read any of the replies to it.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Are you sure you wrote this code

    [insert]
    Code:
    #include <stdio.h>
    
    void Main (void) {
    char c;
    
    printf("Enter a sentence:\n");
    
    scanf("%c", &c);
    while (c != ".") {
    printf("%c\n", c);
    scanf("%c", &c);
    }
    
    printf(".\n");
    }
    characters are compared with a ' ' (single quote) and not double quotes. Comeon i just saw this code in one of the other posts as well. Be honest with what you want. No one is going to write the code for you dear.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by roaan View Post
    Are you sure you wrote this code

    [insert]
    Code:
    #include <stdio.h>
    
    void Main (void) {
    char c;
    
    printf("Enter a sentence:\n");
    
    scanf("%c", &c);
    while (c != ".") {
    printf("%c\n", c);
    scanf("%c", &c);
    }
    
    printf(".\n");
    }
    characters are compared with a ' ' (single quote) and not double quotes. Comeon i just saw this code in one of the other posts as well. Be honest with what you want. No one is going to write the code for you dear.
    I say that and used it but I started from scratch on the advice I recieved, here it is and if anyone could tell me if I'm doing anything wrong please point it out.

    #include <iostream>
    #include <string>

    int main()
    {
    int a = 0, e = 0, i = 0, o = 0, u = 0;

    std::cout << "Enter a line of text: ";
    std::string lineOfText;
    getline( std::cin, lineOfText );

    for ( std::string::size_type letters = 0;
    letters != lineOfText.size(); letters++ )
    {
    switch( lineOfText[letters] )
    {
    case 'a' : ++a; break;
    case 'e' : ++e; break;
    case 'i' : ++i; break;
    case 'o' : ++o; break;
    case 'u' : ++u; break;
    }
    }

    std::cout << a << " a ";
    std::cout << e << " e ";
    std::cout << i << " i ";
    std::cout << o << " o ";
    std::cout << u << " u " << std::endl;

    return 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by DK12 View Post
    Im 28 and what homework would I be doing in July if I was in school? I appreciate you would hate kids doing this but I really only started this as a hobby.
    This sounds crazy to me :-).
    I m also in college and doing homework right now (even in JULY summer session goes on)

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Talking

    Quote Originally Posted by roaan View Post
    This sounds crazy to me :-).
    I m also in college and doing homework right now (even in JULY summer session goes on)
    Then I would just say I needed help in my work? This isn't a witch hunt

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    >> Im 28 and what homework would I be doing in July if I was in school?

    College, perhaps? The thing is, we handle a lot of homework questions and after a while you get pretty good at spotting questionable posts. Yours just happened to set off that alarm. Anyway, your claim to have already completed a handful of games still doesn't wash, considering that the code you posted doesn't even compile.

    At any rate, my recommendation to you would be to read through your entire book (or several of them) before venturing off to make a game.



    ITSA
    Socket Library!

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    As I stated in your other thread, please use [code] tags.

    Also, your new code works (and it seems to have changed from C to C++), so what's the problem?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Any help much appreciated
    By euclid in forum C Programming
    Replies: 13
    Last Post: 11-15-2007, 04:53 AM
  3. Program Control, HELP appreciated.
    By Astra in forum Windows Programming
    Replies: 7
    Last Post: 01-01-2007, 05:59 AM
  4. Help Appreciated
    By cboard_member in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2005, 06:31 AM
  5. I need help..anything will be appreciated
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2001, 09:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21