# Thread: Newbie needs a little help!

1. ## Newbie needs a little help!

Hey guys, so I started to learn a little C from numerous different sources and Iv'e hit a little "bump". When following a simple tutorial of how to make a average calculator with 5 inputs I decided I wanted to make it a little more advanced, and have a unlimited amount of inputs. I know what I have done wrong(As pointed out in "comments" in the code), but not sure how to correct it. Could you guys have a little look and in very basic terms tell me how to fix it, thanks.

Code:
``` #include <stdio.h>
#include <conio.h>

int main(void)
{
int loopcount;
int numbers;
int total;
int average;
int numtoav;
/* ^^ Number's to average */
int mainvar;
total=0;
loopcount=0;

{
printf("How many numbers do you want to average?");
scanf ("%d, &numtoav");

}

while(loopcount< &numtoav){
/* Problem's persisst here ^^ */
printf ("Enter the numbers you want to average");
scanf("%d", &numbers);
total = total + numbers;
loopcount=loopcount+1;

}
average = total/&numtoav;
/*Aswell as here ^^ */
printf("The average of your numbers is: %d", average);

getch ();
}```
Also any tips on bad coding etiquette would be also appreciated.

2. scanf takes a reference to the integer numtoav. But otherwise you should use it as an ordinary integer.
Code:
```while(loopcount < numtoav) {
...and...
average = total / numtoav;```

3. ## Thanks

Thank you, it runs now, but it still doesn't work fully, CMD looks something like this (Area between *'s is user input)
How many numbers do you want to average?*5*
Enter the numbers you want to average*2 6 7 9*
Enter the numbers you want to averageEnter the numbers you want to averageEnter
the numbers you want to averageEnter the numbers you want to averageEnter the
numbers you want to average

4. I don't understand this:
Enter the numbers you want to average*2 6 7 9*
Are you attempting to enter 2 6 7 9 (four numbers with spaces) on one line of user input before a return?

The program wants each number entered on a separate line with a return.

Anyway, the code works for me after I get rid of all the compile warnings/errors. How about posting the current, compilable (with no warnings) code?

5. Also any tips on bad coding etiquette would be also appreciated.
Get your indentation and whitespace under control. Consistency makes it SO much easier to read and spot errors.

6. Originally Posted by mike65535
I don't understand this:

Are you attempting to enter 2 6 7 9 (four numbers with spaces) on one line of user input before a return?

The program wants each number entered on a separate line with a return.

Anyway, the code works for me after I get rid of all the compile warnings/errors. How about posting the current, compilable (with no warnings) code?
Well I'm glad it works for you, its pretty odd I must admit. But like I said, I'm a newbie and to be honest I'm mostly figuring this out as I go, I use DEV C++ to write and compile and don't know what you mean about posting the compilable. From what I see there are no compiling errors or anything like that. And yes, I was entering the inputs on one line, if I try enter each input on different lines the program just keeps going back to "Enter the numbers you want to average"

7. jubjubj... are you working with any kind of tutorials or textbooks?

Also DEV C++ has been abandoned for several years now... You may want to update to something newer...
something like Pelles C for plain C... Code::Blocks with MinGW for C/C++...

8. Originally Posted by jubjubj
yes, I was entering the inputs on one line, if I try enter each input on different lines the program just keeps going back to "Enter the numbers you want to average"
Of course:

Code:
```          while(loopcount< &numtoav){
printf ("Enter the numbers you want to average");
scanf("%d", &numbers);
total = total + numbers;
loopcount=loopcount+1;
}```
When you press enter, everything you typed gets added to the stdin buffer. That scanf() only reads one number at a time, from that buffer. So if you enter all the numbers on one line, the while() loop reads one from the stdin buffer, then it prints "Enter..." again, then it reads another number (from the numbers still in the stdin buffer), until the loop has completed, or the buffer is empty, in which case it has to wait for you to enter more.

You might get a better idea of this if you say you want 10 numbers, then enter them 2 at a time. "Enter..." will print out twice each time. If you feed it three at a time, "Enter..." will print out three times.

Code:
```Enter: 1 2
Enter: Enter: 2 3 4
Enter: Enter: Enter: 5 6 7 8
Enter: Enter: Enter: Enter: 9```
Probably you should print "Enter..." before the loop, not in it.

9. Okay, so I have fixed that, and guess what? Another issue! Here is the updated code:
Code:
```#include <stdio.h>
#include <conio.h>

int main(void)
{
int loopcount;
int numbers;
int total;
int average;
int numtoav;
/* ^^ Number's to average */
int mainvar;
total=0;
loopcount=0;
{
printf("How many numbers do you want to average?");
scanf ("%d, numtoav");
}
printf ("Enter the numbers you want to average");
while(loopcount< numtoav){
scanf("%d", &numbers);
total = total + numbers;
loopcount=loopcount+1;
}
average = total/numtoav;
printf("The average of your numbers is: %d", average);
getch ();
}```
Basically now it doesn't work, I can enter the numbers but get no output. It just goes down a line each time I hit enter. I'm probably doing/not doing something very obvious and basic, any idea's?

You may want to update to something newer...
something like Pelles C for plain C... Code::Blocks with MinGW for C/C++...
Thank you I will upgrade once I finish this! And I am using a combination of a book called "Practical C Programming, 3rd Edition (1997)" (IKR I must love old things) and A tutorial series by a youtube user called the new Boston
My program started life when I followed this tutorial. > YouTube - C Programming Tutorial - 6 - While Loops

10. Originally Posted by jubjubj
Basically now it doesn't work,
You need to enable warnings for your compiler.

There's a couple of things wrong here:

Code:
`scanf ("%d, numtoav");`
Namely, the way the whole thing is quoted, and that you didn't use &, the "address of" operator. If you had your warnings enabled, the compiler would have clued you in.

Also: you should return 0 at the end of main.

11. Originally Posted by jubjubj
Thank you I will upgrade once I finish this! And I am using a combination of a book called "Practical C Programming, 3rd Edition (1997)" (IKR I must love old things) and A tutorial series by a youtube user called the new Boston
My program started life when I followed this tutorial. > YouTube - C Programming Tutorial - 6 - While Loops
The others are giving you good help with your code so I'll just add one more comment about tutorials and such...

1) DO NOT mix C and C++, these are separate languages and the result can be very confusing. What you know from C does carry forward to C++ but the reverse is not true... a LOT of C++ code will return a chain of syntax errors in a C compiler.

2) I'm not sure that watching videos is a good way to learn anything. What you need to do is get an *up to date* C tutorial or manual --certainly something newer than 14 years ago-- and go through it page by page, exercise by exercise. Most of these documents teach in a progressive manner making sure each step prepares you for the next. Videos may get the information in front of your eyes (for a fleeting few seconds) but actually doing the work will get it between your ears where it will live, quite comfortably, for the rest of your life. The problem being that moving pictures do not trigger long term retention of information (which is probably why some people needed to see Star Wars 2,927 times )

12. Originally Posted by MK27
You need to enable warnings for your compiler.
I hinted at this but the OP didn't take the bait.

13. Originally Posted by CommonTater
The others are giving you good help with your code so I'll just add one more comment about tutorials and such...

1) DO NOT mix C and C++, these are separate languages and the result can be very confusing. What you know from C does carry forward to C++ but the reverse is not true... a LOT of C++ code will return a chain of syntax errors in a C compiler.

2) I'm not sure that watching videos is a good way to learn anything. What you need to do is get an *up to date* C tutorial or manual --certainly something newer than 14 years ago-- and go through it page by page, exercise by exercise. Most of these documents teach in a progressive manner making sure each step prepares you for the next. Videos may get the information in front of your eyes (for a fleeting few seconds) but actually doing the work will get it between your ears where it will live, quite comfortably, for the rest of your life. The problem being that moving pictures do not trigger long term retention of information (which is probably why some people needed to see Star Wars 2,927 times )
I think your right, I rushed into this, I told myself I would stick to books, but that changed quickly. I will go back to reading an up-todate book. And maybe re-visit this later on. I'm still proud of what I achieved in 24 hours, but there is deferentially holes in the way I work that need to be fixed.
Thanks guys!

14. Originally Posted by jubjubj
I think your right, I rushed into this, I told myself I would stick to books, but that changed quickly. I will go back to reading an up-todate book. And maybe re-visit this later on. I'm still proud of what I achieved in 24 hours, but there is deferentially holes in the way I work that need to be fixed.
Thanks guys!
Patience is it's own reward.
Impatience is it's own punishment.

Figure a month to learn the basics, 3 months before you can write anything useful, 6 months to a year to get good at it.

15. On this note, are there any book you would recommend of that you used? I was thinking "C for Dummies 2nd edition" ?