Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,098 people online right now. Registration is fast and FREE... Join Now!




array help

 
Reply to this topicStart new topic

array help

asim0412
24 Oct, 2006 - 11:05 AM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 1


My Contributions
I need help with this array. am writing a program that randomly generates a four digit number using the rand generator function in C++. i need to work with each digit individual so after the numbers are generated I need to put them in an array. Here is my code could someone tell me what is wrong woth my array.

CODE

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int random_integer;
int storage[4];

int main()
{
    srand((unsigned)time(0));
  
    for(int index=0; index<4; index++)
    {
        random_integer = (rand()%5)+2;
        
        
        
        cout << random_integer <<endl;
        
        storage[4] = random_integer;
      
      
        
        
    }    
  
    cout << storage[4];
        
    

    
    
      
}

User is offlineProfile CardPM
+Quote Post

mattman059
RE: Array Help
24 Oct, 2006 - 11:15 AM
Post #2

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 340


Dream Kudos: 175
My Contributions
Instead of doing this : storage[4] = random_integer;
try storage[index] = random_integer;
that way the index goes up, and puts the next number into the next alloted space in storage. Then instead of saying cout << storage[4] (which will only display the 4th object in the array, put cout << storage (This will display the entire array)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:05AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month