-3

I'm trying to get this c program to work and it keeps printing "Insert value" twice after every output also my average doesn't work.

Here's the code:

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

void main()
{
    int d = 0;

    printf("Command list:\t \n\nCommand: \t Output: ");
    printf("\n \"A\"  \t Declare values of a list.\n \"O\"  \t Obtain the average value of the values in the list.\n");
    printf(" \"P\"  \t Print the values of the list.\n \"S\"  \t End program. \n");

    while (d !=1)
    {
        char value;

        printf("\nInsert value: ");
        scanf("%c", &value);


        if (value == 'S' || value == 's')
        {
            d++;
        }



        float list[1000], average, sum = 0;
        int number_of_values;

    //in order to insert values to array:
        if (value == 'a' || value == 'A')
        {
            printf("Insert number of values in the list: ");
            scanf("%d", &number_of_values);
            for (int i = 1; i<=number_of_values; i++)
            {
                printf("Insert Value of element %d on the list: ", i);
                scanf("%f", &list[i]);
                sum += list[i];

            }

        }

        if ((value == 'P' || value == 'p') && (number_of_values >= 1))
        {
            for (int i =1; i<= number_of_values; i++)
            {
                printf("%.2f\n", list[i]);
            }
        }

        if ((value == 'o' || value == 'O') && (number_of_values >= 1))
        {
            average = sum / number_of_values;
            printf("Average = %.2f", average);
        }


    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
YuKan
  • 1
  • 2

2 Answers2

0

Inside while loop , leave a space before %c in this-

scanf(" %c", &value);

Your scanf returns because of '\n' present in stdin after when you press enter to give input . scanf reads until '\n' is encountered.

When you take input in array using scanf and press enter '\n' is left in buffer , and as loop iterate and it again take input in value it encounters '\n' and scanf return without taking input .That is why you need to add space before %c .

And move these declarations out of your while loop -

float list[1000], average, sum = 0;
int number_of_values;
while(b!=1){
//your code
}

If you declare variables inside loop, they get reset after every iteration due to again declaration .

Note- void main() -> int main(void) or int main(int argc,char **argv)

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • main in C is implementation defined. void main() is ok in C. Can you also explain why should there be a space before %c, since OP probably does not know why he would have to do this. Thank you. – this Nov 27 '15 at 13:17
  • @this I have added the explanation . I have suggested changing , it is allowed but not specified . So suggestion might not harm in any case . – ameyCU Nov 27 '15 at 13:26
  • Thanks so much, what's the difference in leaving a space before the %c inside the scanf? – YuKan Nov 27 '15 at 13:26
  • @LuisGeraldoRivera I have added explanation. Take a look :-) – ameyCU Nov 27 '15 at 13:30
  • If OP's professor specified void main then it is correct to do so. Actually using int main() might not even compile for him. – this Nov 27 '15 at 13:32
  • @this i just suggested it ,and if compiler doesnt support then it should be updated IMHO .i dont think it wrong to just suggest :-) – ameyCU Nov 27 '15 at 13:40
  • It is a minor annoyance, since OP might lose some time while incorrectly changing to int main(). Why would compiler have to be updated? That makes no sense. In C main is implementation defined. That architecture and compiler defined it to be void main(). Full stop. – this Nov 27 '15 at 13:45
0

Your code reads characters, so the first "Insert Value" message is caused by pressing "enter" after any other scanf(). You can get the commands as Integer numbers 1,2,3 etc.

The other possible solution is that you can insert "getch()"; code at the end of the while loop in order to get the "enter" character stored in buffer.

Marduc
  • 37
  • 9