• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
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?  
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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>
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
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!!
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:-))
PashaModCommented:
Thank you for your understanding :)
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.