Author Topic: The programming help thread!  (Read 2173 times)

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: The programming help thread!
« Reply #75 on: August 20, 2007, 01:12:24 PM »
 I'd look at that test statement inside of the while loop. Looks like you already call getchar() once, why call it again?

 Actually I think you want use getche() instead of getchar() for the test loop, though I don't think EOF will work in that case. Getchar() will grab multiple characters from a buffer and release when enter is pressed, getche() will grab a single character at a time. Look up the ascii key code for "enter" and poll that instead, for using getche().
« Last Edit: August 20, 2007, 01:17:46 PM by Bonknuts »

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #76 on: September 02, 2007, 02:33:00 PM »
Okay, this is the most simplified format of the program I've made thus far. 

Code: [Select]
/* Prob 11-9
*  Created by Yuriy Mokriy
*/

#include <stdio.h>
#include <stdlib.h>

#define ALPHABET_TOTAL 25                    /* The total number of letters in the alphabet */

main()
{
      int  text[ALPHABET_TOTAL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
           alpha_count,                      /* Counts the letters in the alphabet */
           char_count = 0,                   /* Counts the instances of each letter */
           letter_count = 0;                 /* Counts the letters in the text input */
      char text_input;                       /* The text inputted by the user */
     
      /* Text input */
     
      printf("\nPlease enter text.  Terminate program by pressing Ctrl+Z: ");
      text_input = getchar();
     
      /* Increment counter if not End Of File */
     
      while (text_input = getchar() != EOF)
       ++letter_count;
   
      /* Display the results */
   
      for (alpha_count = 65; alpha_count <= 90; alpha_count++)
      {
       if (getchar() == text[alpha_count] || getchar() == text[alpha_count + 32])
        char_count++;
       printf("\nTotal %c or %c: %d", alpha_count, alpha_count + 32, char_count);
      }
}
I haven't used getche() because my textbook doesn't cover that and I haven't seen anything good come out of that when I tried it in my program.  I declared a couple of variables called "alpha_count" to count all the letters in the alphabet and "char_count" to increment the letter instances in my block of text.

Now here's the kicker.  Upon reading the question more thoroughly, the author's definition of the "end-of-file" character is the usage of Ctrl+Z to break out of the loop and display the results.  That solves one problem. :)  However, the program is not counting the letter instances.

Here's the original question if you want to know what the author is getting at:

Write a program that prompts the user to enter text at the terminal.  When the user finishes entering text, he or she should enter the appropriate end-of-file character to terminate input.  The program should count the frequency of each letter of the alphabet.  The program should not distinguish an uppercase letter from a lowercase letter.  When the user finishes inputting text, the program should display each letter and its output.

Here's the hint given by the author:

Declare an array of counters, one for each letter.  Use the letter itself as the subscript that determines which array element to increment.  To do this, the program must convert the letter to the corresponding subscript.  Remember that the decimal ASCII values of the lowercase letters are 97 to 122 and the uppercase letters are 65 to 90.

So basically, the only problem left with the program is to count the letter instances of the text.  Everything else works fine.

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #77 on: September 13, 2007, 05:06:06 AM »
Well, after a couple of mind numbing weeks, I believe I finally got this sumbitch figured out:

Code: [Select]
/* Prob 11-9
*  Created by Yuriy Mokriy
*/

#include <stdio.h>

#define ALPHABET_TOTAL 26                    /* The total number of letters in the alphabet */

main()
{
      int text[ALPHABET_TOTAL] = {0},        /* Array sized 26, acting as counters for all characters 'a' .. 'z'  (case insensitive) */     
          text_input,                        /* Reads in the text input as characters */
          ASCII_count,                       /* The ASCII numerical representation of letters in the alphabet */
          alpha;                             /* boolean "flag" which turns to 0 (false) in case of a non-alpha character */
     
      /* Initialization */
     
      text_input = 0;
      ASCII_count = 0;
      alpha = 0;

      /* Get input */
     
      printf("\nPlease enter text.  Terminate program using Ctrl+Z: ");
     
      while( text_input != EOF )
      {
        alpha = 1;
        text_input = getchar();   
       
      /* If input is alphabetical, convert to a number 0-25 so that it can be used as an array index locator */
     
      if(text_input >= 'a' && text_input <= 'z')
       text_input -= 'a';
      else if(text_input >= 'A' && text_input <= 'Z')
       text_input -= 'A';

      /* Non-alphabetical character, set the alpha flag to false */
   
      else
       alpha = 0;

      /* Increment the appropriate counter in the text array as long as the character is alphabetical */
   
      if(alpha)
       text[text_input]++;
     }
     
     /* Display results */
     
    for (ASCII_count; ASCII_count <= 25; ASCII_count++)
     printf("\nTotal %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A', text[ASCII_count]);
}

I chose to put the input inside of the while loop rather than outside of it as I believed it the characters would be read in more easier that way.  I also changed my for loop iteration to read the actual letters by their capacity size instead of by their ASCII representation.  That was a major source of confusion to me.

The results look great.  What do you think? :)

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #78 on: September 14, 2007, 09:04:17 AM »
Great Job!

Although somehow I missed that you were having problems initially...  :?:

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #79 on: September 14, 2007, 09:13:08 AM »
How the hell did you miss it? :lol: Maybe your eye strain is finally getting the better of you. :shock:

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!