Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input for an array 1

Status
Not open for further replies.

RollerMan

Technical User
Jan 27, 2002
6
US
if I were to say:


int x;
const int number=10;//stating how many elements for the array
cout<<&quot;enter a value: &quot;;
cin>>x;

int values[number]=x //the array to grab what the user typed in


cout<<values;


How can I get the input from the user to go into an array??

 
#include <iostream.h>

int main()
{
int values[10];

cout << &quot;enter first value:&quot;;
cin >> values[0];

cout << &quot;enter second value:&quot;;
cin >> values[1];

// ...

cout << &quot;enter last value:&quot;;
cin >> values[9];


cout << values[0];
cout << &quot;\n&quot;;

cout << values[1];
cout << &quot;\n&quot;;

// ...

cout << values[9];
cout << &quot;\n&quot;;

return(0);
}
 
#define COUNT 10
void main(){
int values[COUNT];
for (int i = 0;i < COUNT; i++){
printf(&quot;Enter the Value %d : &quot;, i);
scanf(&quot;%d&quot;, &values);
}
}
 
Try that code it should handle what you want to do pretty nicely !

#include <iostream.h>
#include <stdlib.h>
#define MAXSIZE 100
void main()
{
int size = 0;
int i;
long Array[MAXSIZE];
cout<<&quot;Enter the size of the array: &quot;;
cin>>size;
if( size > MAXSIZE ) // if the size the user enter is too big
{
cout<<&quot;That size is too big !&quot;;
exit(0);
}
for( i = 0;i < size; i++ ) // Get the array values from the user
{
cout<<&quot;Value&quot;<<i<<&quot;:=&quot;;
cin>>Array;
}
cout<<&quot;Here is your array:&quot;<<endl; // Display the array
cout<<&quot;Array = {&quot;;
for( i = 0;i < size; i++ )
cout<<Array<<&quot;,&quot;;
cout<<&quot;}&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top