Solved

storing input from a user, to an array

Posted on 2003-11-12
13
366 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
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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now