Solved

storing input from a user, to an array

Posted on 2003-11-12
13
381 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:incevolebus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9737153
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
 

Author Comment

by:incevolebus
ID: 9737180
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9737311
>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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ssnkumar
ID: 9737490
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9737501
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 9737519
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 9737852
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
 

Expert Comment

by:PashaMod
ID: 9737859
Thank you for your understanding :)
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10400493
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question