Matthew Stone
Matthew Stone

Reputation: 17

User enter 5 characters and averge sums out the 5 numbers in C

I been having issues having my C code work. I have 1 warning, which states Warning: too many arguments for format. I am a beginner in C so I haven't encountered this issue yet. Any ideas on how to fix it and I cannot use conditions as I am in the beginning segment of my course learning from the start. I just need to know what I did wrong so I can fix the issue. Here's the code below:

#include <stdio.h>

int main() {
    float firstNumber, secondNumber, thirdNumber;
    float fourthNumber, fifthNumber;

    float sumAverage1 = (firstNumber+secondNumber+thirdNumber);
    float sumAverage2 = (fourthNumber+fifthNumber);


    long a = 1000000000;
    long b = 1250000000;
    long c = 1500000000;
    long d = 1750000000;
    long e = 2000000000;

        printf("A is %li\n", a);
        printf("B is %li\n", b);
        printf("C is %li\n", c);
        printf("D is %li\n", d);
        printf("E is %li\n", e);

        printf("Enter 5 Random numbers and guess what the total will be summed up when program runs.\n");
        printf("You cannot enter a decimal integer and enter numbers below 100.\n");
            scanf("%f", &firstNumber);
            scanf("%f",&secondNumber);
            scanf("%f",&thirdNumber);
            scanf("%f",&fourthNumber);
            scanf("%f",&fifthNumber);

            printf("Your numbers average out to:\n", sumAverage1+sumAverage2/5);

    system("pause");
return 0;
}

Upvotes: 0

Views: 138

Answers (3)

Vishwajeet Vishu
Vishwajeet Vishu

Reputation: 492

Here this will solve all your problem.

#include <stdio.h>
#include <stdlib.h>
int main() {
float firstNumber, secondNumber, thirdNumber,sumAverage1;
float fourthNumber, fifthNumber,sumAverage2;
long a = 1000000000;
long b = 1250000000;
long c = 1500000000;
long d = 1750000000;
long e = 2000000000;
printf("Enter 5 Random numbers and guess what the total will be summed up when program runs.\n");
printf("You cannot enter a decimal integer and enter numbers below 100.\n");
scanf("%f",&firstNumber);
scanf("%f",&secondNumber);
scanf("%f",&thirdNumber);
scanf("%f",&fourthNumber);
scanf("%f",&fifthNumber);

sumAverage1 = (firstNumber+secondNumber+thirdNumber);
sumAverage2 = (fourthNumber+fifthNumber);

    printf("A is %li\n", a);
    printf("B is %li\n", b);
    printf("C is %li\n", c);
    printf("D is %li\n", d);
    printf("E is %li\n", e);
        printf("Your numbers average out to:%f\n", (sumAverage1+sumAverage2)/5);

system("pause");
return 0;
}

I ran this on my Visual Studio and it works just fine. Hope it Solves your Problem.

Upvotes: 0

Felix Guo
Felix Guo

Reputation: 2708

You need to change the printf format specifier, but your scanf does not capture the extra dangling newline. You need to either clear the buffer, fflush(stdin) after each scanf() or you need an extra scanf("%c") to get rid of the newline character.

See scanf() leaves the new line char in buffer?

Upvotes: 0

Govind Parmar
Govind Parmar

Reputation: 21542

The line:

 printf("Your numbers average out to:\n", sumAverage1+sumAverage2/5);

Has an argument but no format specifier. Also, that expression is unparenthesized; the division has higher precedence than the addition, so what you're calculating is sumAverage1+(sumAverage2/5), which is integer division, which is probably not what you want.

What you probably want is:

printf("Your numbers average out to: %f\n", (double)(sumAverage1+sumAverage2)/5.0);

Upvotes: 1

Related Questions