January 28th, 2013, 11:05 PM
Hey ! Here is my code - So, my for loop is working however, at the end I am trying to print out my array but its not working.
For example, if I do an array of size 3, my output is
enter the size of your array: 3
enter number0: 1
enter number1: 4
enter number 2: 5
here is the problem, I want it to printout:
at the end but, its giving me a bunch of garbage. Dont understand what is going on !
January 28th, 2013, 11:07 PM
I dont know what is it that I am doing wrong ??
January 29th, 2013, 12:22 AM
> printf ("%d", array[i]);
You could put this inside your loop.
Or you could put it in another loop.
It needs to be in one loop or another, because it doesn't print the whole array.
January 29th, 2013, 12:51 AM
yes only for loop for printf was missing ..
and what is use of getchar here?
without it it works fine ...
btw how come compiler didnt warn or give error if we declare
int array[size]; after pritnf...
Last edited by swapy; January 29th, 2013 at 12:59 AM.
January 29th, 2013, 01:46 AM
That is legal code per C++ standards and C99 standards. Only older C compilers will complain about it.
Originally Posted by swapy
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
January 29th, 2013, 01:48 AM
You can also simplify your code a little bit.
You're using this to determine the number of elements in your array, but you already have a variable in your program where you stored that.
Doing this might not be necessary, but it could help you when you are rereading or debugging old code.
In C, you can use variables before they have been initialized. If you do this, the variable's value is most likely some garbage that had been stored on the stack earlier. That's why your compiler might compile your code, but it should at least give you a warning.
The getchar function returns a char from stdin. This means it's blocking your program since its waiting for some input.
January 29th, 2013, 03:05 AM
but scanf is doing the same thing as getchar ....
if scanf is used why next statement is getchar ?? i didnt get the purpose
January 29th, 2013, 03:54 AM
In this instance, the getchar() serves no purpose.
If the user types in 123 456 789 or
it makes no difference to the %d format, since it will automatically skip any whitespace (newline, spaces, tabs) before trying to convert the next number.
All the conversion formats of scanf, except %c and %[ have the property of skipping leading whitespace.
If however, you switch from scanf() to fgets(), then the semantics change, and you have to do some cleanup of the input stream - which is typically done using getchar() to read characters until a \n is seen (or EOF is seen).
January 29th, 2013, 05:09 AM
Change condition part of for loop
change the condition of for loop and no need of two conditions at a time either one or double value would be inserted by arrays.