-1

Assigned task is to ask for # of values, and then at the end output the minimum, maximum, and average values and at this point I've run out of bug fixes

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

int main()
{
    int ErrorDetection = 1;
    char valCounter;
    int valnumber;
    int Incrementer;
    int StoredValue;
    int MinimumValue = 100;
    int MaximumValue = 0;
    float Average;
    int AddToStored;
    int Sum = 0;

    printf("MIN, MAX, and MEAN CALCULATOR\n\n");

    while (ErrorDetection != 0)
    {
        printf("How many values are to be entered?\n");
        scanf("%s", &valCounter);

        if (valCounter > '0' && valCounter < '9') {
            ErrorDetection = 0;
        }
        else {
            ErrorDetection = 1;
            printf("INPUT ERROR!\n");
        }
        valCounter = valCounter - 47;
    }

    for (Incrementer = 1; Incrementer < valCounter; Incrementer++)
    {
        ErrorDetection = 1;

        while (ErrorDetection != 0) {
            printf("Value %d: ", Incrementer);
            scanf(" %d", &StoredValue);

            if (StoredValue > 0 && StoredValue < 9) {
                ErrorDetection = 0;
            }
            else {
                ErrorDetection = 1;
                printf("INPUT ERROR!\n");
                continue;
            }
        }

        if (StoredValue > MaximumValue) {
            MaximumValue = StoredValue;
        }
        if (StoredValue <= MinimumValue) {
            MinimumValue = StoredValue;
        }
        Sum = Sum + StoredValue;
    }

    valCounter = valCounter - 1;
    Average = (float)Sum / (float)valCounter;

    printf(
        "Minimum value is %d, maximum value is %d, and average value is %g.\n",
        MinimumValue, MaximumValue, Average
    );
}

If you input a 2 digit number things begin to breakdown, but at the same time I don't know how to go through with errorchecking if I allow multiple digit answers, as I make use of ASCII conversions to check if an input is a number or not.

kube
  • 13,176
  • 9
  • 34
  • 38
  • Note that you only allow a count between 1 and 8 inclusive. I can see eliminating 0; why not allow 9 too? Using `valCounter = valCounter - 47;` would be better written using `'0'` (48) — `valCounter -= '0' - 1;` to subtract 47. I'm not sure why you have the 47; it means that `'1'` maps to `2`, and so on. Be cautious with magic numbers! – Jonathan Leffler Oct 04 '18 at 16:33
  • @JonathanLeffler That is why OP is starting loop from `1` instead of `0`. A work around to solve bug you pointed out. – kiran Biradar Oct 04 '18 at 16:42

2 Answers2

3

You have undefined behavior here.

     char valCounter;

    scanf("%s", &valCounter);

You have declared valCounter as char type but trying to read string type.

Hence change the scanf to.

scanf("%c", &valCounter);

I would suggest you declare valCounter as int

int valCounter;
scanf("%d", &valCounter);

in that case your if will become.

if ((valCounter > 0) && (valCounter < 9)) 

and you don't need

valCounter = valCounter - 47; //remove

Also your for loop should start from 0 instead of 1

for(Incrementer = 1 ; Incrementer < valCounter; Incrementer++)

should be

for(Incrementer = 0 ; Incrementer < valCounter; Incrementer++)
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • If I change incrementer to 0 then asking for 1 value gives me "Value 1: Value 2:" – Dylan Wolfer Oct 04 '18 at 16:47
  • @DylanWolfer You should subtract `valCounter = valCounter - '0';` instead of `valCounter = valCounter - 47;`. Did you see jonathan lefler comment regarding the same? – kiran Biradar Oct 04 '18 at 16:52
0

Your problem is here.

char valCounter;
scanf("%s", &valCounter);

You're telling scanf to read a string, but you're passing it the address of a character. You should be asking for an integer, and giving it the address of an integer.

int valCounter;
scanf("%d", &valCounter)

There's more information here, including reasons why scanf might not be the best idea: How to scanf only integer?

Tim Randall
  • 4,040
  • 1
  • 17
  • 39
  • Done the above and changed the error detection from character values to integer values, but now if there is a 2 digit input for the number of values, say for example 14, the 1 is used as number of values and the 4 is used for value 1, rather than 14 being used for the initial value – Dylan Wolfer Oct 04 '18 at 16:36
  • And the program also loops if a non integer number is entered still – Dylan Wolfer Oct 04 '18 at 16:38
  • If you read the link, I think you'll find a lot of relevant information. I think some of it was definitely relevant to how to avoid reading too much or too little data from the input stream. – Tim Randall Oct 04 '18 at 16:38
  • So how would I use fgetc to store a variable in the same way scanf does? – Dylan Wolfer Oct 04 '18 at 16:46