0

Please bare with me. I am still a newb at programming and this may seem a bit confusing, but I have many comments and debug lines. This is an extremely long code, but could the advanced programmers out there please help me out? The goal of my program is to simply read an expected number of values, read the values (on one line) themselves, and print their averages. The expected number of values is used as a counter in a while loop. I am trying to make my program robust so that it will print an error when something other than an integer is entered. Therefore, I declare a char string, determine whether each value of string is a digit or letter, and convert the character of that string into an int. My problem with this program is my first while loop. The first loop seems to be fine, but second time around, it is unable to read user input. It instead uses the last output printed to the screen as the input. Any suggestions/help will be greatly appreciated. Here's my program:

* Read in a number of values to read and then print the average
 * of those values.
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tfdef.h"
#define DEBUG

#define FLUSH while(getchar() != '\n');

int IS_DIGIT(char c);
/* Given  : character
 * Returns: TRUE if digit, FALSE otherwise
 */

int main(void)
{
 /* Declare variables */
 int expected;         /* Expected number of inputs for desired avg */
 int count;            /* Count of expected number of inputs for calculating avg */
 float sum;            /* Sum of all inputs for desired avg */
 int value;            /* Value of input for desired avg */
 char input[10] = " "; /* Input string of expected number of inputs for avg */
 int i = 0;            /* Counter for string */
 int digit;            /* Variable to call function for determining digit */

       /* Prompt user for expected number of inputs for desired avg */
       #ifdef DEBUG
       printf("debug: Enter expected number value of inputs: ");
       #endif

       #ifdef FLUSH
       while (scanf("%s", &input) != EOF)
       {
       #endif
               #ifdef DEBUG
               printf("debug: The expected number value is %s\n", input);
               #endif

               /* For i is initially 0, increase i by one for every
               character in input string */
               for(i; input[i]; i++)
               {
                       /* Call function to determine if character is a digit */
                       digit = IS_DIGIT(input[i]);

                       /* Convert string input to integer (binary) */
                       expected = atoi (input);

                       #ifdef DEBUG
                       printf("debug: The integer form of the input: %d\n", expected);
                       #endif

                       /* If the input is not a digit, it is a letter */
                       if (digit == FALSE)
                       {
                               #ifdef DEBUG
                               printf("debug: %d is not a digit\n", expected);
                               #endif
                       }

                       /* If the input is a digit */
                       else
                       {
                               #ifdef DEBUG
                               printf("debug: %d is a digit\n", expected);
                               #endif
                       }
               }

               /* Check for last value of string */

               /* If the input is not a digit, it is a letter */
               if (digit == FALSE)
               {
                       #ifdef DEBUG
                       printf("debug: Final value on line: not a digit\n");
                       #endif
               }

               /* If the input is a digit */
               else
               {
                       #ifdef DEBUG
                       printf("debug: Final value on line: is a digit\n");
                       #endif
               }

               /* Initializee sum at 0 */
               sum = 0;

               #ifdef DEBUG
               printf("debug: Enter values for average: ");
               #endif

               /* Read input of values for the average */
               scanf("%d", &value);


               /* Initialize count at 0
                  While the count is less than the expected count (first input),
                  increase count by one */
               for (count = 0; count < expected; count++)
              {

                       /* Sum is equal to previous sum increased by value */
                       sum += value;

                       #ifdef DEBUG
                       printf("debug: Sum = %f\n", sum);
                       #endif

               }

               /* Print average result of all values */
               printf("Average of %d values is %.2f\n", count, count != 0 ? sum / count : 0.0);


       /* If the last character is not a digit, print an error */
       if (digit == FALSE)
       {
        printf("Error!  Can't read number of expected values.\n");
       }

       /* Update loop */
       #ifdef DEBUG
       printf("debug: Enter expected number value of inputs: ");
       #endif
       }
}


/* Functions */

int IS_DIGIT(char c)
/* Given  : character
 * Returns: TRUE if digit, FALSE otherwise
 */`
{
 if ((c) >= '0' && (c) <= '9')
 {
  return TRUE;
 }
 else
 {
  return FALSE;
 }
}

With the following input, I hope to get the following output:
5
10 20 30 40 50
Average of 5 values is 30.00
6
50 70 whatever 40 60
Error! Can't read expected value #2.
Average of 2 values is 60.00

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by WaltP
0

Please bare with me. I am still a newb at programming and this may seem a bit confusing, but I have many comments and debug lines. This is an extremely long code, but could the advanced programmers out there please help me out?

You really need to get rid of all the #ifdef's. They make the code very difficult to follow. Remember, you are asking others to read your code, and you don't want it to be difficult to read. Just put the printf()'s in there and comment them out later.

Is this a different program from your other thread. If not, please mark this thread solved and repost where you started. Are you expecting different help in a new thread?


The goal of my program is to simply read an expected number of values, read the values (on one line) themselves, and print their averages. The expected number of values is used as a counter in a while loop. I am trying to make my program robust so that it will print an error when something other than an integer is entered. Therefore, I declare a char string, determine whether each value of string is a digit or letter, and convert the character of that string into an int. My problem with this program is my first while loop. The first loop seems to be fine, but second time around, it is unable to read user input. It instead uses the last output printed to the screen as the input. Any suggestions/help will be greatly appreciated. Here's my program:

Unfortunately, I can't follow what you're doing with all those #ifdef's. For some reason you don't seem to want us to know what's really happening because all you post is what you expect to happen. If it's going wrong, ya think we might need to know details about said wrongness? Like what the code is actually showing you? You've got the debug data in front of you....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.