Arrays in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ouwet5775
    New Member
    • Oct 2006
    • 9

    #1

    Arrays in C++

    Hey guys,

    i am new to C++ and i was wondering if somone could help me out.
    i am suposed to write a function that would do the folowing :

    "Reads up to n integers from cin ( stopping when n integers have been read or when the user stops supplying more input, wichever comes first) and stored them in the array; returns the number of integers which were actually read."


    Now i have learned in class how to do this by pre defining the values of the array but i have never tried inputting values into it.

    This is what i have so far, but it will automaticcly read 0 till 100.. :(

    Code:
    #include<iostream>
    using namespace std;
    
    
    
    int main ()
    {
    	int array[100];
    	int t;
    
    
    
    	for (t=0; t<100; ++t)
    		array[t]=t;
    
    	for ( t=0;t<100; ++t)
    		cout<<"This is array ["<< t << "] :"<<array [t]<<"\n";
    
    
    	return 0;
    }

    SO i tried creating something else and thats what i got out of it :

    Code:
    include<iostream>
    using namespace std;
    
    
    
    int main ()
    {
    	int array[100];
    	int n;
    
    
    
    	for ( n=0; n<100; n)		// No incrementation :(
    		array[n]=n;
    	cout<<" Enter the values for your array:\n";
    	cin>>n;				// is this even doable ???
    
    	return 0;
    }

    Thanks for any help :)
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Typical input into arrays is done with a simple for loop - you were on the right path!

    Code:
    for (int r = 0; r < (upper bound of array); r++) cin >> array[r];
    It's up to you to finish the rest of the program!

    Comment

    • Ouwet5775
      New Member
      • Oct 2006
      • 9

      #3
      Thanks alot i think am getting the hang of it :)

      Comment

      Working...