Thread: can someone double check and maybe improve my code

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >return true;
    Whence comes true? Unless you defined it yourself I can only assume that you are mixing C and C++ (which is frowned upon on the C forum) or using C99 and not telling us (which is also frowned upon before C99 compilers are so rare).

    >static int vowelIndex[5];
    What reason do you have for declaring this static? The only reason I can see for this program is to get a zero fill, but that can be easily done like so without using static:
    Code:
    int vowelIndex[5] = {0};
    >fgets(buffer, 80, stdin);
    fgets is smart enough to read n - 1 characters and put a null character at the nth position. There's no need to subtract 1 from the array size when using it as the second argument. In fact, a better way to call fgets would be:
    Code:
    fgets(buffer, sizeof buffer, stdin);
    >fflush(stdin);
    Um...no. fflush is undefined for input streams.

    >why dont you just use a switch statement?
    It's a matter of style. You'll notice that the switch statement is often overly verbose compared to an equivalent if chain.
    My best code is written with the delete key.

  2. #17
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Quote Originally Posted by enjoy
    salut
    je vous conseille tous d'abord de voir un livre d'algorithmique pour ameliorer vos competences
    2emme pour ce programme je vous conseille d'utuliser (enum) c'est a dir d'enumere les lettre choisies puis de faire une boucle ou une fonction avec (switch)
    si vous ne connaissez pas le syntaxe le voila
    switch(variable)
    {
    case(variable):{anything to do}
    .
    .
    .
    .
    .
    case(n):...
    defaul:{anything to do}
    }
    si vous voulez que je fait cet exercice a votre palce demandez le et je n'hesiterai pas a te donner un coup de main
    mon e-mail est [email protected]
    bonne chance
    HUH?

  3. #18
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    >>>>why dont you just use a switch statement?
    >>It's a matter of style. You'll notice that the switch statement
    >>is often overly verbose compared to an equivalent if chain.

    I suppose.

  4. #19
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    Can someone make on in C++ for me to read, I am new to C++ and never studied C. I understand they are the same but being new to programming I don't want to overload myself.
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can someone make on in C++ for me to read
    Who better to do that than you? By converting a C program to C++ or a C++ program to C, you learn a great deal about both.

    >I understand they are the same
    No, they are completely different.
    My best code is written with the delete key.

  6. #21
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Quote Originally Posted by Salem
    >
    Or the book they're reading was "written" by Herb Schildt
    We are using the book called "A First Book of ANSI C" by Gary J Bronson". I think it's an excellent book.

  7. #22
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Whats real neat is that you can simplify this:
    Code:
    #define NUM_VOWELS 5
    #define VOWELS "AEIOU"
    With this:
    Code:
    #define VOWELS "AEIOU"
    #define NUM_VOWELS (sizeof VOWELS - 1)
    Sizeof of a quote returns the number of bytes needed to hold that string, including the null at the end. However, it would be better to loop until a null instead of count the vowels.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How is that simplifying it? The only thing you gain is if for some reason you change VOWELS, then you don't have to update NUM_VOWELS. But it is hardly simplifying it.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Its not really simplifying anyways, you're right. My bad choice of words. Its useful in some situations and I just wanted to bring it up since we were discussing string immediate values and how you could use indexers to access each character.

Popular pages Recent additions subscribe to a feed