Localization Legend "Supper the Subtitler" has "joined the club" in being targeted for CD-pressings by bootleg master Tobias/PCEWorks! His projects like Private Eyedol, Galaxy Fräulein Yuna 1 & 2, etc. are now being sold on Chinese factory-pressed CDROMs...
IMG
Main Menu

The programming help thread!

Started by Dark Fact, 06/01/2007, 01:46 AM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Dark Fact

I'm not using whole numbers in my program though.  I'm using real numbers.

I also initialized my totals for each category to 0.00 so that it has a value to start off with before accumulations.  However, this doesn't solve the problem as my program is still giving me crap values.

Here's the code to make it more easier:

/* Prob 10-3
*  Created by Yuriy Mokriy
*/

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

double Get_Opening_Balance();                                 /* Gets the user's opening account balance */
void Choice_Menu();                                           /* Displays a menu for choices */
void Validate_Choice(double);                                 /* Validates the user's choice from the menu */
double Deposit(double, double);                               /* Gets the user's deposit amount */
double Withdrawal(double, double);                            /* Gets the user's withdrawal amount */
double Check(double, double);                                 /* Gets the user's check amount */
void Display_Results(double, double, double, double, double); /* Displays all results and totals */

main()
{   
      double open_balance_amount;            /* The user's opening account balance */
     
      /* Call functions */
     
      open_balance_amount = Get_Opening_Balance();
      Choice_Menu();     
      Validate_Choice(open_balance_amount);
}

double Get_Opening_Balance()
{
      double open_acct_balance;              /* The user's opening account balance */
     
      /* Prompt the user */
     
      printf("\nEnter the opening account balance: $");
      scanf("%lf", &open_acct_balance);
     
      if (open_acct_balance < 0.00)
         exit(0);
      else
         return open_acct_balance;     
}

void Choice_Menu()
{
      /* Display menu */
     
      printf("\nSelect your choice: ");
      printf("\n(D)Deposit");
      printf("\n(C)Check");
      printf("\n(W)Withdrawal");
      printf("\n(Q)Quit"); 
      printf("\n"); 
}

void Validate_Choice(double opening_balance_amt)
{
      char choice;                           /* The choice entered by the user */
      double total_deposit_amt,              /* The total amount deposited */
             total_withdrawal_amt,           /* The total amount withdrawn */
             deposited_amt,                  /* The amount deposited */
             withdrawn_amt,                  /* The amount withdrawn */
             total_check_amt,                /* The total check amount */
             check_amt,                      /* The check amount */
             account_balance,                /* The current account balance */
             closing_balance_amt;            /* The closing balance amount */
             
      /* Initialization */ 
       
      total_deposit_amt = 0.00;
      total_withdrawal_amt = 0.00;
      total_check_amt = 0.00;
      account_balance = 0.00;

      account_balance += opening_balance_amt;
         
      /* Get choice */
     
      getchar();
      choice = getchar();
     
      /* Validate choice */
     
      while (choice != 'q' || choice != 'Q')
      {
       if (choice == 'd' || choice == 'D')
       {
          account_balance = Deposit(deposited_amt, account_balance);
          total_deposit_amt = account_balance - opening_balance_amt - total_withdrawal_amt - total_check_amt;
       }
       else if (choice == 'w' || choice == 'W')
       {
          account_balance = Withdrawal(withdrawn_amt, account_balance);
          total_withdrawal_amt = -(account_balance - opening_balance_amt - total_deposit_amt - total_check_amt);
       }
       else if (choice == 'c' || choice == 'C')
       {
          account_balance = Check(check_amt, account_balance);
          total_check_amt = -(account_balance - opening_balance_amt - total_deposit_amt - total_withdrawal_amt);
       }
       else if (choice == 'q' || choice == 'Q')
       {
         closing_balance_amt = account_balance;
         Display_Results(opening_balance_amt, total_deposit_amt, total_withdrawal_amt, total_check_amt, closing_balance_amt);
         exit(0);
       } 
       else
         printf("\nError! You must enter the following choices listed on the menu.");
         
         Choice_Menu();
         getchar();
         choice = getchar();
      }       
}

double Deposit(double deposit_amount, double pres_account)
{         
      /* Prompt the user */
     
      printf("\nEnter the amount to be deposited: $");
      scanf("%lf", &deposit_amount);
     
      pres_account += deposit_amount; 
     
      return pres_account;
}

double Withdrawal(double withdrawal_amount, double present_account_balance)
{   
      /* Prompt the user */
     
      printf("\nEnter the amount to be withdrawn: $");
      scanf("%lf", &withdrawal_amount);
     
      if (withdrawal_amount > present_account_balance)
      {
        printf("\nWarning! The amount you have withdrawn exceeds your current account balance.\n");
        exit(1);
      }
     
      else
      {
        present_account_balance -= withdrawal_amount;
        return present_account_balance;
      }
}

double Check(double check_amount, double curr_account)
{       
      /* Prompt the user */
     
      printf("\nEnter the check amount $");
      scanf("%lf", &check_amount);
     
      if (check_amount > curr_account)
      {
        printf("\nWarning! The check amount exceeds your current account balance.\n");
        exit(1);
      }
     
      else
      {
        curr_account -= check_amount;
        return curr_account; 
      }
}

void Display_Results(double opening_balance, double total_deposit, double total_withdrawal, double total_check, double closing_balance)
{
      /* Display the results */
     
      printf("\nOPENING BALANCE:        $%.2lf", opening_balance);
      printf("\nTOTAL AMOUNT DEPOSITED: $%.2lf", total_deposit);   
      printf("\nTOTAL AMOUNT WITHDRAWN: $%.2lf", total_withdrawal);
      printf("\nTOTAL CHECK AMOUNT:     $%.2lf", total_check);
      printf("\n----------------------------------");
      printf("\nCLOSING BALANCE:        $%.2lf", closing_balance);
      printf("\n");
}
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Sorry, been busy with work so I haven't glanced at your code yet. I DO have a real job M-F. ;)

Today is Saturday so I'll take a look a little bit later and get back to you.

Dark Fact

homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

Hey nat, I've already found someone else to look at the problem for me.  The program is working fine now so you don't have to worry about it. :)
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

#55
sorry dude, I know I'm lame. I still haven't even looked at it yet. :) I'll get to it next time, I promise. But it always helps to post the code right away with your problem, cuz usually without code I can only speculate at what's going on.

What was the problem, BTW?

TurboXray

Quote from: nat on 07/04/2007, 10:54 PMFirst, if you're only dealing with whole dollars in this program change your data type for all your variables to long int, although it's probably not a big deal.

Next, without seeing your code, I'm going to guess you aren't initializing your variables. Depending how your program is set up this could make a big difference. I always believe it's good practice to initialize variables as you declare them regardless:

long int         nat_rules = 0,
                 dark_fact_rules = 0,
                 we_all_rule = 0;


Without seeing your code it's going to be tough debugging the program further.... Try the initialization thing and if that doesn't work, post up your code.
Funny, I've heard the argument for not initializing your variables on declaration - for debugging reasons.

nat

Quote from: TurboXray on 07/11/2007, 09:53 AMFunny, I've heard the argument for not initializing your variables on declaration - for debugging reasons.
Well, you're probably a much more serious/experienced programmer than I am.

I'm sure there are merits to both "philosophies" but in my experience I've found it beneficial to initialize variables on declaration more often than not. As with everything, there are exceptions to the rule....

TurboXray

QuoteI'm sure there are merits to both "philosophies" but in my experience I've found it beneficial to initialize variables on declaration more often than not. As with everything, there are exceptions to the rule....
I totally agree. I had an associate once tell me there only one way to do things - insinuating that there is only one right way. I nodded my head, but laughed to myself. I lost *all* respect for him after that  :mrgreen:

 I always initialized my variables in declaration in the past. I just came across that article recently.

 I'm still "amateur hour" in C - ASM's my strong point.

guyjin

Quote from: TurboXray on 07/12/2007, 12:52 AMI totally agree. I had an associate once tell me there only one way to do things - insinuating that there is only one right way.
Yeah, pretty much anyone who says this, or anything like it, is a tool and can safely be ignored.*

*(unless you're dealing with potentially fatal stuff, then it's okay sometimes)

Dark Fact

Quote from: natsorry dude, I know I'm lame. I still haven't even looked at it yet. Smile I'll get to it next time, I promise. But it always helps to post the code right away with your problem, cuz usually without code I can only speculate at what's going on.

What was the problem, BTW?
Sorry for the late reply.  The problem I had involved finding a way to add up total deposits, withdrawals, and checks within my functions and then returning those totaled values to the Validate_Choice function.  However, the problem always put out incorrect values.  After re-reading the question, I found out that I wasn't supposed to total up my deposits, withdrawals, and checks within their own functions, but within Validate_Choice. 

Anyways, a friend of mine over MSN messenger was able to point out that problem to me and lo and behold, my problem is fixed.

Nat, chances are I'll be asking for help from my other friend over MSN when it comes to programming in the future.  No offense, but I can't really wait a week or two for you to look at my program and tell me what's wrong.  I need solutions, stat.

If I run into additional problems where I need to ask for further help, I'll let you know. :wink:
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Quote from: Dark Fact on 07/23/2007, 02:46 PM
Quote from: natsorry dude, I know I'm lame. I still haven't even looked at it yet. Smile I'll get to it next time, I promise. But it always helps to post the code right away with your problem, cuz usually without code I can only speculate at what's going on.

What was the problem, BTW?
Nat, chances are I'll be asking for help from my other friend over MSN when it comes to programming in the future.  No offense, but I can't really wait a week or two for you to look at my program and tell me what's wrong.  I need solutions, stat.

If I run into additional problems where I need to ask for further help, I'll let you know. :wink:
:oops: Sorry... Again.

Is this for some kind of class, or something?

Anyway, yeah, I'm always here to help if you need/want it. I don't usually take that long to respond. Really.

Dark Fact

No, it wasn't in a class.  It's just your typical C function. :wink:

I'll let you know in the future again if I have trouble. :wink:
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

Okay, I've got another thing here with my program that I need a third opinion on.  It's a program that requires me to read in values using an array and then after all the values have been entered, display the minimum value, maximum value, median, and average.

Although the program works, I'm not too sure about the minimum value procedure.  I had a bug with my program that involved displaying "0.00" for minimum value every time the program finishes.  For instance, I would type in values from 1.00 to 20.00 and then the program would display the values in descending order.  The maximum value is 20.00 while the minimum value is 1.00.  The program always displays 0.00 as minimum value.

I corrected the problem by initializing minimum_income to 1.00.  While it corrected the problem, I'm wondering if this kind of procedure is acceptable? Take a look at the code and tell me what you think:

/* Prob 11-5
*  Created by Yuriy Mokriy
*/

#include <stdio.h>

#define HOMES 20                        /* The maximum number of homes surveyed */

main()
{
      float household_incomes[HOMES],   /* The array to store the incomes */   
            maximum_income,             /* The maximum income amount in the survey */
            minimum_income,             /* The minimum income amount in the survey */
            average_income,             /* The average income amount in the survey */
            total_income,               /* The total income amount from the survey */
            median_income;              /* The median income amount in the survey */
      int income_count,                 /* Counts the incomes in the survey */
          limit,                        /* Keeps track of how far to go on a pass */
          pass,                         /* The number of the pass */
          temp;                         /* Temporarily stores income amounts for sorting */
     
      /* Initialization */
     
      total_income = 0.00;
      average_income = 0.00;
      median_income = 0.00;
      maximum_income = 0.00;
      minimum_income = 1.00;
     
      /* Prompt for incomes */
     
      for (income_count = 0; income_count < HOMES; income_count++)
      {
       printf("\nEnter the income amount for home #%d: $", income_count + 1);
       scanf("%f", &household_incomes[income_count]);
       total_income += income_count + 1;
       
       /* Determine maximum income */
       
       if (household_incomes[income_count] > maximum_income)
        maximum_income = household_incomes[income_count];
       
       /* Determine minimum income */
       
       if (household_incomes[income_count] < minimum_income)
        minimum_income = household_incomes[income_count];
      }
       
      /* Calculate median and average income */
     
      average_income = total_income / HOMES;
      median_income = ((household_incomes[10] - 1) + (household_incomes[11] - 1)) / 2;

      /* Sort the array in descending order */
     
      limit = HOMES - 2;
     
      for (pass = 1; pass <= HOMES - 1; pass++)
      {
       for (income_count = 0; income_count <= limit; income_count++)
        if (household_incomes[income_count] < household_incomes[income_count + 1])
        {
         temp = household_incomes[income_count];
         household_incomes[income_count] = household_incomes[income_count + 1];
         household_incomes[income_count + 1] = temp;
        }
        --limit;
      }
     
      /* Display the sorted incomes */
     
      printf("\nThe incomes in decreasing order are as follows:\n");
     
      for (income_count = 0; income_count < HOMES; income_count++)
       printf("$%.2f ", household_incomes[income_count]);
      printf("\n");
       
      /* Display the results */
     
      printf("\nMAXIMUM INCOME: $%.2f", maximum_income);
      printf("\nMINIMUM INCOME: $%.2f", minimum_income);
      printf("\nAVERAGE INCOME: $%.2f", average_income);
      printf("\nMEDIAN INCOME:  $%.2f", median_income);   
      printf("\n");
}

Is it okay to use 1.00 as minimum income or do I need to have it set to 0.00 and find another way to fix the problem?
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Hmm..

If I understand the spirit of your problem correctly, I don't think your solution is correct.

The reason the program always displayed "0" for minimum income before you initialized it to "1" is because using your code:

   if (household_incomes[income_count] < minimum_income)
        minimum_income = household_incomes[income_count];


...that condition was never true unless you entered a negative income. So minimum_income was never set and retained a value of "0". Follow me?

I think a proper solution would be to initialize "minimum_income" on the first loop through with whatever you happen to enter as an income rather than initializing it before the loop. For example inserting the code:

   if (income_count == 0)
        minimum_income = household_incomes[income_count];


...right before the if statement that checks to see if the current income is lower than the current minimum should initialize the minimum_income variable on the first loop through.

Make sense?

Dark Fact

I just gave that a shot.  It works great now. :)

My friend over MSN suggested that I use the initialization method for maximum_income and minimum_income but I felt that this just didn't sit right with me as the program required me to determine maximum and minimum income based on inputted values, not on comparisons with pre-initialized values.

Anyways, thanks for the help.  Arrays have been kicking my ass lately.
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

Got another problem.

I have a program that requires me enter text at the terminal.  After I'm finished entering text, I'm then required to provide totals of each letter instance in the text.  For example, if I have a piece of text saying hahayoulose, the program would have to count up these total letters:

total 'a' = 2
total 'e' = 1
total 'h' = 2
total 'l' = 1
total 'o' = 2
total 's' = 1
total 'u' = 1
total 'y' = 1

All other letters would be read in as 0.  I require an array of counters for each letter in order to add up the values.  The program should not distinguish between uppercase and lowercase.  So, if I type in 'a' or 'A', it would still be added to my variable called 'a_count'.  The program also requires an EOF (end-of-file) character to terminate input.

Here's what I have for my code so far:

/* Prob 11-9
*  Created by Yuriy Mokriy
*/

#include <stdio.h>

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

main()
{
      char text[ALPHABET_TOTAL],          /* Text inputted by the user */
           letter_count;                  /* array subscript for text */
          /* The following declarations below are the array subscripts for the 'text' variable */
      int a_count = 0,
          b_count = 0,
          c_count = 0,
          d_count = 0,
          e_count = 0,
          f_count = 0,
          g_count = 0,
          h_count = 0,
          i_count = 0,
          j_count = 0,
          k_count = 0,
          l_count = 0,
          m_count = 0,
          n_count = 0,
          o_count = 0,
          p_count = 0,
          q_count = 0,
          r_count = 0,
          s_count = 0,
          t_count = 0,
          u_count = 0,
          v_count = 0,
          w_count = 0,
          x_count = 0,
          y_count = 0,
          z_count = 0;
         
      /* Text input */
     
      printf("\nPlease enter text.  Terminate program by pressing enter: ");
      text[letter_count] = getchar();
     
     while (text[letter_count] = getchar() != EOF)
      {
       for (letter_count = 'a'; letter_count <= 'z'; letter_count++)
       {
        while (text[letter_count] = getchar() != '\n')
        {
          if (text[letter_count] == 'a')
           ++a_count;
          if (text[letter_count] == 'b')
           ++b_count;
          if (text[letter_count] == 'c')
           ++c_count;
          if (text[letter_count] == 'd')
           ++d_count;
          if (text[letter_count] == 'e')
           ++e_count;
          if (text[letter_count] == 'f')
           ++f_count;
          if (text[letter_count] == 'g')
           ++g_count;
          if (text[letter_count] == 'h')
           ++h_count;
          if (text[letter_count] == 'i')
           ++i_count;
          if (text[letter_count] == 'j')
           ++j_count;
          if (text[letter_count] == 'k')
           ++k_count;
          if (text[letter_count] == 'l')
           ++l_count;
          if (text[letter_count] == 'm')
           ++m_count;
          if (text[letter_count] == 'n')
           ++n_count;
          if (text[letter_count] == 'o')
           ++o_count;
          if (text[letter_count] == 'p')
           ++p_count;
          if (text[letter_count] == 'q')
           ++q_count;
          if (text[letter_count] == 'r')
           ++r_count;
          if (text[letter_count] == 's')
           ++s_count;
          if (text[letter_count] == 't')
           ++t_count;
          if (text[letter_count] == 'u')
           ++u_count;
          if (text[letter_count] == 'v')
           ++v_count;
          if (text[letter_count] == 'w')
           ++w_count;
          if (text[letter_count] == 'x')
           ++x_count;
          if (text[letter_count] == 'y')
           ++y_count;
          if (text[letter_count] == 'z')
           ++z_count;
        }
       }
         
       for (letter_count = 'A'; letter_count <= 'Z'; letter_count++)
       {
        while (text[letter_count] = getchar() != '\n')
        {
          if (text[letter_count] == 'A')
           ++a_count;
          if (text[letter_count] == 'B')
           ++b_count;
          if (text[letter_count] == 'C')
           ++c_count;
          if (text[letter_count] == 'D')
           ++d_count;
          if (text[letter_count] == 'E')
           ++e_count;
          if (text[letter_count] == 'F')
           ++f_count;
          if (text[letter_count] == 'G')
           ++g_count;
          if (text[letter_count] == 'H')
           ++h_count;
          if (text[letter_count] == 'I')
           ++i_count;
          if (text[letter_count] == 'J')
           ++j_count;
          if (text[letter_count] == 'K')
           ++k_count;
          if (text[letter_count] == 'L')
           ++l_count;
          if (text[letter_count] == 'M')
           ++m_count;
          if (text[letter_count] == 'N')
           ++n_count;
          if (text[letter_count] == 'O')
           ++o_count;
          if (text[letter_count] == 'P')
           ++p_count;
          if (text[letter_count] == 'Q')
           ++q_count;
          if (text[letter_count] == 'R')
           ++r_count;
          if (text[letter_count] == 'S')
           ++s_count;
          if (text[letter_count] == 'T')
           ++t_count;
          if (text[letter_count] == 'U')
           ++u_count;
          if (text[letter_count] == 'V')
           ++v_count;
          if (text[letter_count] == 'W')
           ++w_count;
          if (text[letter_count] == 'X')
           ++x_count;
          if (text[letter_count] == 'Y')
           ++y_count;
          if (text[letter_count] == 'Z')
           ++z_count;
        }
       }
      }         
      printf("\ntotal a or A: %d", a_count);
      printf("\ntotal b or B: %d", b_count);
      printf("\ntotal c or C: %d", c_count);
      printf("\ntotal d or D: %d", d_count);
      printf("\ntotal e or E: %d", e_count);
      printf("\ntotal f or F: %d", f_count);
      printf("\ntotal g or G: %d", g_count);
      printf("\ntotal h or H: %d", h_count);
      printf("\ntotal i or I: %d", i_count);
      printf("\ntotal j or J: %d", j_count);
      printf("\ntotal k or K: %d", k_count);
      printf("\ntotal l or L: %d", l_count); 
      printf("\ntotal m or M: %d", m_count);
      printf("\ntotal n or N: %d", n_count);
      printf("\ntotal o or O: %d", o_count);
      printf("\ntotal p or P: %d", p_count);
      printf("\ntotal q or Q: %d", q_count);
      printf("\ntotal r or R: %d", r_count);
      printf("\ntotal s or S: %d", s_count);
      printf("\ntotal t or T: %d", t_count);
      printf("\ntotal u or U: %d", u_count);
      printf("\ntotal v or V: %d", v_count);
      printf("\ntotal w or W: %d", w_count);
      printf("\ntotal x or X: %d", x_count);
      printf("\ntotal y or Y: %d", y_count);
      printf("\ntotal z or Z: %d", z_count);
}

The array itself has a capacity of 25 to fit 25 letters of the alphabet.  The problem with the program that I keep having is that it just keeps hanging on me after I finished my input.  I suspect it to be a problem with my EOF, but I 'm not sure how to fix this.  I have a couple of for loops that count the presence of each letter both in lowercase and uppercase with the appropriate accumulator being incremented for each instance found.  If anyone can figure out what's wrong, it would be greatly appreciated. :)
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

TurboXray

#67
Just wanted to point out something - there's no need to write that many if statements for the whole alphabet, let alone two sets for upper and lowercase.

In ASCII you can avoid case sensitive checking, if case is not important. Bit 5 of the byte is set when the character is upper case and clear when the character is lower case.

unsigned char get_char;
unsigned int count_array[25]; 

   //note: I usually make my arrays a little bigger then the space I need incase I overwrite another area of memory with an out of bounds pointer
.
.
.
  get_char|=0x60;
That will convert all alphabetical characters to lower case. Use get_char&=0x5F to convert it to upper case.

 You'd probably want to check to see of the input char is within the correct range first:



 if( (get_char>0x40 && get_char<0x5b) || (get_char>0x60 && get_char<0x7b) )
  {
      get_char&=0x5F;   //convert to lowercase
      get_char-=0x41;   //subtract offset to beginning char starts at 0
      count_array[get_char]++;   //use the char as an index into your array and increment that specific usage
  } else {
            //error
         }


Dark Fact

The chapter examples in my textbook doesn't use specific hexadecimal notation in retrieving character values.  A previous example made use of a for loop to count each letter specifically and increment the accumulators if an instance is found.

The example that you provided may read in the values but what am I supposed to do for output? My question asks to display the number of each letter instance in the inputted text.
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

TurboXray

#69
hmm.. how about this:

  for(int i=0,i<25,i++)
  {
     printf("\n total %s or %s: %d", (i + 0x41), (i + 0x61), count_array[i]);
  }

nat

Looks like Bonknuts beat me to this one.

Dark Fact

homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Still not working for you?

I'll look at it as soon as I'm off work.

TurboXray

 What compiler are you using? Knowing your way around the debugger is very import (I'd say a must). You can easily(usually) find out at what point the program is crashing when stepping through the debugger.

 Also another way to debug your could is with process of elimination. Like for instance, cut the program down to:
     while (text[letter_count] = getchar() != EOF)
      {
          letter_count++;
      }

 hint: I'd take a look at that test statement.

Dark Fact

I've tried modifying the code like this but the program still leaves me hanging:

/* 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()
{
      unsigned int text[ALPHABET_TOTAL];  /* Array to store all letter instances of the alphabet */
      int letter_count,                   /* Counts the letter instances in the text input */
          char_count = 0;                 /* Counts the letters in the text input */
     
       
      /* Text input */
     
      printf("\nPlease enter text.  Terminate program by pressing enter: ");
      text[char_count] = getchar();

      while (text[char_count] = getchar() != EOF)
      {
       if (text[char_count] = getchar() == EOF)
        break;
       else
       {
        ++char_count;
        while (text[char_count] = getchar() != '\n')
        {
         if (text[char_count] = getchar() == '\n')
          break;
         else
          ++char_count;
        }
       }
      }
     
      for (letter_count = 0; letter_count < ALPHABET_TOTAL; letter_count++)
      {                 
       printf("\n total %c or %c: %d", (char_count + 0x41), (char_count + 0x61), text[char_count]);
     
       if((getchar() > 0x40 && getchar() < 0x5b) || (getchar() > 0x60 && getchar() < 0x7b))
       {
        text[char_count] &= 0x5F;
        text[char_count] -= 0x41;
        text[char_count]++;
       }
       else
       {
        printf("\nError.");
        exit(1);
       }
      }     
       
     
}
The program is really stubborn with that EOF statement.  I've tried inserting a couple of break statements to break the loop but that hasn't worked.  I also tried using a different character count variable.  What am I doing wrong now? :(
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

TurboXray

#75
 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().

Dark Fact

Okay, this is the most simplified format of the program I've made thus far. 

/* 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.
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

Well, after a couple of mind numbing weeks, I believe I finally got this sumbitch figured out:

/* 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? :)
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Great Job!

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

Dark Fact

How the hell did you miss it? :lol: Maybe your eye strain is finally getting the better of you. :shock:
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!