0

when I run the program it says the first print statement but then I have to enter 2 numbers for it to ask the next printf statement then it throws off all the code. then I want it to make a table with the average and lowest in each week but it obviously cant do that when all the inputs are in the wrong places.


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

void main()
{
    //declare variables
    int thisWeek[7];
    int lastWeek[7];
    int i;
    float avgt;
    float avgl;
    int totalt = 0;
    int totall = 0;
    int low = 1000;
    int lowl = 1000;




    //for loop for putting the sales into the arrays... I   don't understand why it asks the first question twice 
    for (i = 0; i < 7; i++)
    {
        printf("Enter the Sales for day %d this week\n", i+1);
        scanf("%d ", &thisWeek[i]);

        printf("Enter the Sales for day %d last week\n", i+1);
        scanf("%d ", &lastWeek[i]);
    }

    //for loop to get the lows of each week and the totals to work out the averages
    for (i = 0; i < 7; i++)
    {
        totalt = totalt + thisWeek[i];
        totall = totall + lastWeek[i];
        if (thisWeek[i] < low)
        {
            low = thisWeek[i];
        }


        if (lastWeek[i] < lowl)
        {
            lowl = lastWeek[i];
        }

    }
    // working out the averages
    avgt = totalt / 7;
    avgl = totall / 7;

    //for loop for displaying the sales in the arrays and the average for each week
    for (i = 0; i < 7; i++)
    {
        printf("The Sales for day %d        ", i+1);
        printf("%d      ", thisWeek[i]);
        printf("%d\n", lastWeek[i]);
    }
    printf("The average sale this week is %f \n", avgt);
    printf("The average sale last week is %f \n", avgl);


    //lowest value in each week
    printf("The lowest sale value this week is %d \n", low);
    printf("The lowest sale value last week is %d \n", lowl);

    //printing to file
    FILE* output;
    output = fopen("Sales.txt", "w");

    if (output == NULL)
    {
        printf("File could not be found\n");
    }
    else
    {
        for (i = 0; i < 7; i++)
        {
            fprintf(output, "The Sales for day %d       ", i+1);
            fprintf(output, "%d     ", thisWeek[i]);
            fprintf(output, "%d\n", lastWeek[i]);
        }
        fprintf(output, "The average sale this week is %f \n", avgt);
        fprintf(output, "The average sale last week is %f \n", avgl);



        fprintf(output, "The lowest sale value this week is %d \n", low);
        fprintf(output, "The lowest sale value last week is %d \n", lowl);
        fclose(output);
    }
}


I thought it would ask one then get the input for the first one then ask and get input for the second one

0 Answers0