Click to See Complete Forum and Search --> : Whats wrong with this code????
Ercan
March 30th, 1999, 07:26 AM
This code is supposed to ask the user
How many numbers would you like to enter?
And then give the smallest, largest , average, and the difference
between each number and the average. But it wont work. And i cant find
why. I get no error but it still wont work.
Could you please help me fix this thing as I have no more hair left
thinking about it. I am new to programming so I need the most basic
code to fix it....
I thank you in advance..
Ercan
#include <iostream.h>
#include <conio.h>
main()
{
clrscr();
int maxentries,maxnumber,minnumber,number,totalaverage;
float average,sum,difference;
cout<<"How many Numbers would you like to Enter?\t";
cin>>maxentries;
for (int count = 0; count < maxentries; count++)
{
cout<<"\n\nEnter a number Please:\t";
cin>>"number";
sum += number;
average = ( sum / count );
cout<<"\nAverage of these numbers is\t"<<average;
difference = ( number - average );
cout<<"\nDifference between this number and the average is"<<difference;
if ( number < minnumber )
minnumber = number;
if ( number > maxnumber )
maxnumber = number;
}
cout<<"\nSmallest number is\t"<<minnumber;
cout<<"\nLargest number is\t"<<maxnumber;
totalaverage = sum / maxentries;
cout<<"\nTotal average is\t"<<totalaverage;
getch();
return 0;
}
LiuDengKe
March 30th, 1999, 07:37 AM
Hello,Ercan.I have read your source code . You should initialize these variable such as sum ,maxentries,maxnumber,minnumber,number,totalaverage.Good Luck!
LiuDengKe 99.3.30
Ercan
March 30th, 1999, 07:40 AM
But they are initialised.
????????????????????????????????????????
Michael Decker
March 30th, 1999, 07:43 AM
Hey! Weren't you the guy who asked something similar yesterday?
1) change cin>>"number"; to cin>>number;
2) You need to initialize the minimum and maximum before testing.
Check for the first time throught the loop (ie. count == 0) and initialize them then.
if (count == 0)
{
minnumber = maxnumber = number;
}
else
{
if ( number < minnumber )
minnumber = number;
if ( number > maxnumber )
maxnumber = number;
}
There may be more, but try these suggestions first
Michael Decker
March 30th, 1999, 07:45 AM
He's right, you do need to initialize all of your variables. In C and C++, automatic variables are not initialized, so they contain garbage.
Franky Braem
March 30th, 1999, 07:46 AM
They are not initialised.
The int and float variables you use must be initialised to zero.
If you don't believe that are not initialised then try to debug your code
and watch the variables.
Martin Speiser
March 30th, 1999, 07:46 AM
Hi Ercan,
I assume you made your first programming experiences in a language like BASIC, right? So do I, and I stepped in the
same problem ;-) In a language like C or C++ a variable isn't initialized during declaration. So you need to set "sum"
to zero by yourself. That's the first point.
Second, during calculation of "average" you will get a runtime error, because during the first loop "count" is 0, and
you're trying to divide by zero. The line must be "average = ( sum / count + 1 );"
The calculation of "difference" will be okay if you change this.
"minnumber" and "maxnumber" is the same problem like the one with "sum", they aren't initialized, so they can have
every value. Change it to
if ( 0 == count )
{
minnumber = maxnumber = number;
}
else
{
minnumber = min( minnumber, number );
maxnumber = max( maxnumber, number );
}
min and max are macros defined in one of the standard headers, don't know which one. Maybe stdlib.h.
If you're wondering why I wrote "if ( 0 == count )" instead of "if ( count == 0 )", that's my prefered coding style. If
I compare a variable with a constant, I (and many others) are writing the constant first. So you get a compiler error
if you mix up "==" and "=".
HTH
Martin
Ercan
March 30th, 1999, 07:48 AM
Yes it probably was me who asked similar question yesterday but this
code is giving me the s__ts.. heheheh but i am enjoying the challenge.
I am really new at this just 5 weeks into programming so please
accept my appologies if i am annoying....
I will get better as I learn more..
Thank You for responding
Ercan
Bore
March 30th, 1999, 07:49 AM
The biggest problem I see is
cin >> "number";
At best, this is going to read user input into a six-byte text string, and not into the int called 'number.' At worst
it would cause your app to crash. You should get rid of the quotes:
cin >> number;
Next, you need to initialize variables before you use them. For example, in the for() loop:
sum += number;
'sum' didn't have a legitimate value to start with, so adding 'number' to it is not going to be meaningful for you.
Before the for() loop, add a line:
sum = 0;
And check your other variables to see whether you're initializing them before you use them.
Next, when computing your average inside the for() loop, you use:
average = (sum / count);
This will get you in trouble, because your for() loop starts with count = 0. And even the new Pentiums can't divide by
zero. I recommend that you change your for() loop:
for(count = 1; count <= maxentries; count++)
In general, start a for() with zero when you're accessing an array or something that's really based on zero.
Last, take a look at your minnumber processing. If you init minnumber to zero, then your app will claim that the
smallest number entered is zero, even if the user enters all positive numbers. So you'll have to create a mechanism
that tells you whether you've stored a user-entered value into minnumber before you check the value of number against
minnumber. Same thing for maxnumber. Good luck.
Ercan
March 30th, 1999, 07:57 AM
I had heaps of responses and i thank everyone of you. The more responses i
get the more silly i feel. heheheheh the answers are quite obvious when you
are told where the problems are hehehe. Thank you again to everyone who
helped me.
You guys are great for helping us poor newbies.
But oneday even us newbies will become pros. hehehehe
Thank You again
Ercan
Dave Lorde
March 31st, 1999, 04:09 AM
Ercan,
yesterday I gave you code that worked, today you post almost the same code, but seriously broken! What happened?
Dave
Ercan
March 31st, 1999, 05:10 AM
Hi DAVE,
The problem with the code was that in one area you put
int minNumber = INT_MAX;
int maxNumber = INT_MIN;
I dont know what these are as i have seen it before and also my
compiler gives me an error message in the same spot. My compiler
is called Tclite. Not very good but i guess good for a beginner.
I didnt know hwat to do with that part of the code...
So I tried to use some of your code and go another way but i
guess i got lost and confused on the way. hehehehe.
So what is the INT_MAX and INT_MIN??
What do i do to fix it in the compiler so i can run it.
Or is there another way of solving this problem???
As you might have guessed by now I am absolutely new at this stuff.
.O/
/| Ercan
-^-
Thank You for writting back to me Dave....
Ercan
March 31st, 1999, 05:34 AM
heheheheh I FOUND ITTTTTTTTTTTT.
Limits.h
that is what was missing in the code you gave me. I didnt even know it
existed.
Would you be kind enough to explain exactly what it does and how it works.
I have never used it or seen it before.
But the code works perfectly now hehehe.
Now it is time to study the code to fully understand how it works for
future refference.
Thank You very very much. This was a great help to me...
O O
\_/ A big smile on my face.
Ercan
Thank You again.
Dave Lorde
March 31st, 1999, 06:06 AM
Ercan,
glad you found it!
Limits.h is one of the standard header files supplied with C and C++ compilers. It contains definitions for the maximum and minimum values of various implementation dependent types. These are types whose actual size is not explicitly specified by the language standard, but basically depends on the kind of system it is running on.
For example, an int is intended to be the most suitable type for holding and manipulating integers on a given system, so on a 16-bit system like Windows 3.1, an int is likely to be 16 bits, but on a 32-bit system like Windows 95 or NT, it will be 32 bits. Obviously, a 32-bit int can have far higher values than a 16-bit int. When you're writing code that needs to check for values that might overflow the maximum or minimum possible, it's useful to have these maximum and minimum values available.
The values defined in limits.h give you the size limits for these types.
Here is a typical extract from limits.h:
#if !defined(_WIN32) && !defined(_MAC)
#error ERROR: Only Mac or Win32 targets supported!
#endif
#define SHRT_MIN (-32768) /* minimum (signed) short value */
#define SHRT_MAX 32767 /* maximum (signed) short value */
#define USHRT_MAX 0xffff /* maximum unsigned short value */
#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX 2147483647 /* maximum (signed) int value */
#define UINT_MAX 0xffffffff /* maximum unsigned int value */
#define LONG_MIN (-2147483647L - 1) /* minimum (signed) long value */
#define LONG_MAX 2147483647L /* maximum (signed) long value */
#define ULONG_MAX 0xffffffffUL /* maximum unsigned long value */
I hope this helps,
Dave
Ercan
March 31st, 1999, 06:30 AM
Thank you for time DAVE. You have been a great GREAT help.
I have learned quite abit from you. Thank you Thank you.
I UNDERSTAND now....... hehehehe
( Yeeeeeeeeeeee Haaaaaaaaaaa )
Thank You
Ercan
Dave Lorde
March 31st, 1999, 07:03 AM
Ercan,
I'm glad to hear you're making progress. It takes a while, but it gets better as you go on :-)
Good luck!
Dave
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.