• C

storing input from a user, to an array

hi.  i'm new to 'c programming'.  i've got a simple task, which my head cannot get around it.  i'm supposed to create an array, that can store 5 user inputs.  these inputs are integers.  these integers are then stored to the array, which was created, and later printed to the screen.

actual question:
"takes 5 integers one at a time from the keyboard and places them in sequential locations in the array.  prints the array elements to the screen one at a time, placing each element on seperate line."

thanks.
incevolebusAsked:
Who is Participating?
 
sunnycoderCommented:
>void main(void){
it is
int main (int argc, char * argv[])

>scandf("%d", &elements[i]);
scanf and not scandf

>printf("the numbers in the array location are " + elements[i]);
printf("the numbers in the array location are %d", elements[i]);
                                                                        ^   ^
                                                    format specifier   comma

>when the counter reaches 5, it stops and displays all the numbers that the user input.  
currently your program reads in a number and prints it ... for doing it the way you states, you need two loops

>do i need a pointer for the elements in the array?  
No
0
 
sunnycoderCommented:
Hi incevolebus,

sorry we cannot do your assignment for you ... show us the efforts you have made so far and we will try to help

Cheers!
Sunny:o)
0
 
incevolebusAuthor Commented:
so far i've done...

#include <stdio.h>
void main(void){

int elements[5];

for(int input=0; input<5; i++){
printf("Enter number for array location");
scandf("%d", &elements[i]);

printf("the numbers in the array location are " + elements[i]);
}
}

Message:
what i need is code, which will get the user input and store it to the array (called elements).  instead of typing down printf 5 times, for user input, i've looped it 5 times.  when the counter reaches 5, it stops and displays all the numbers that the user input.  do i need a pointer for the elements in the array?  
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
ssnkumarCommented:
You have made small mistakes in the program that you have written!
In the for loop, you are declaring an integet input.
ANSI C doesn't allow this! You have to do all the declarations at the begining of the block.
And in the same statement, you are using a variable 'i', which is not declared. Actually 'i' has to be changed to 'input'.
And the spelling for reading user input is not scandf....it has to be changed to scanf().
And, first read all the elements in a loop and after this start another loop and print the stored input onto the terminal.
Hope this works for you.
One more suggestion: main() function has to return int and it can take some parameters. So, it will be good if you remove void from the parameter list and change return value from void to int. It works even without making changes, but the problem will be, you will not be able to make out the exit status of the program, if the main() function is returning void!
I have changed your code below, so that it works for you:

<code removed by PashaMod - answer to homework>
0
 
sunnycoderCommented:
ssnkumar, I had posted
>sorry we cannot do your assignment for you ... show us the efforts you have made so far and we will try to help
Experts should not do the homework assignments... its against the membership agreement
0
 
ssnkumarCommented:
sunnycoder,
I don't think I am doing any homework/assignment!
And he has attempted to do this program and has posted the program he has written (may  be with small mistakes). So, there is noting wrong in correcting him.......!
If you want, you can check with EE or post a complaint in EE and find out if I have violated any rule......:-)
So, let them decide about the violations!!
0
 
ssnkumarCommented:
Thanks PashaMod.
But, still I don't accept that, I did the homework for him.
He gave the code that he had written and I corrected it!
If he had not provided the code that he has written, then I would have accepted that I have done wrong!
OK now.......you have removed the code and so, we can stop the debate:-))
0
 
PashaModCommented:
Thank you for your understanding :)
0
 
ssnkumarCommented:
My suggestion is to split the points (though it is very small):-)
For those who abandone the questions they themselves have asked, there must be some way to penalise them......
He can be made to cough up more points to experts, for wasting their time.....

-ssnkumar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.