Hello, can you guys tell me what's wrong with my code? I am supposed to get 21 inputs, but the output is messed up. I don't know why. xD Here's the code: http://64.19.142.12/oi42.tinypic.com/1zxshn9.jpg Here's the output: http://oi42.tinypic.com/2ihbbsk.jpg
I'm using Turbo C compiler btw.
wow c lang ..... well i am not able to open ur link ..and this is my weakest ..program ,,,sorry ,...if i will get will sure help u
I'm gonna attach the files, hold on.
i too got the same .. out put ....
there is nothing wrong with the logic now lets analyze few points -> we you are getting input (via getchar()) it requires the user to press enter key(interpreted as '\n'), so the char before that is stored pID[x] but remember '\n' is still present in the buffer(input) -> so when next time it has to take char (via getchar()) it reads '\n' and finishes the process and therefore and prompting the user for input -> for this correction there is a function fflush(stdin) and it`s fucntion is to clear the buffer (stdin stands for standard input) ->before getchar(); add this line fflush(stdin);
Also, you are declaring things to a size of 20, then looping from 0 to 20, which is 21 elements. That is where the garbage from memory is popping into your output.
Just a little down this page is shows what I am talking about better: http://www.cs.nyu.edu/courses/spring05/V22.0201-001/c_tutorial/classes/String.html S[0] = 'a'; S[1] = 'b'; S[2] = 'c'; S[3] = 'd'; S[4] = 'e'; S[5] = 'g'; S[6] = '0'; S[7] = 0; That is an eight character string.
And to make that more clear, if I wanted to declare an 8 character string to hold that, it would be: char S[8] The declaration is of the total number, not the point of the last number. To take in 21 things, you need: <type> <var>[21]
great! thank you for your response guys, that helped me a lot! :D
Join our real-time social learning platform and learn together with your friends!