r/cprogramming • • Mar 20 '22

HELPP PLSS

I am trying to write this C program that adds numbers until the user enters zero. I think my program is correct but I'm getting ridiculous outputs. I added 4+4 and the answer was 1074790400. I am lost and would appreciate some help. Thank you😭

0 Upvotes

9 comments sorted by

12

u/KleberPF Mar 20 '22

We can't say anything without seeing code.

-4

u/berriesandcigarettes Mar 20 '22

The code is:

include<stdio.h>

Int main() { Int num, sum=0; Do { Printf("enter a number: "); Scanf("%lf", &Num); Sum += num; } While(num != 0); Printf("total=%d", sum); Return 0; }

9

u/KleberPF Mar 20 '22

Try to format the code next time. The problem is that you are using "%lf", which is used for doubles, when num is an int. Use "%d" instead.

4

u/berriesandcigarettes Mar 20 '22

Omggg thankyou so muchhh. It worked. I can't thank you enough. I was losing my head over it. Have the greatest day!!

4

u/nerd4code Mar 20 '22

The compiler should have warned you about that. Either you ignored the warning, or you need to enable it.

2

u/berriesandcigarettes Mar 20 '22

How do I enable it?

2

u/[deleted] Mar 20 '22

-Wall -Wextra

on clang: -Weverything

4

u/[deleted] Mar 20 '22

Do NOT use -Weverything. It's experimental, and sometimes will give you warnings that contradict. Use -Wall -Wextra.