Thread: Storing data from a loop to a array?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Smile Storing data from a loop to a array?

    Code:
    
    
    Code:
    int getcard()
    { 
        srand((unsigned)time(0)); 
    
    
    	int x;
    	string mix[10];
    	string h;
    	do
    	{
       for(int index= 0; index<10; index++)
    	{ 
    		 x= rand() % 18;
    
    
    			 h = master[x];
    
    
    		cout << h << endl;
        } 
    
    
    	mix[10] = h;
    
    
    	} 
    	while (false);
    	
    	return 0;
    Hi, i'm new to c++ and my question is how do i store data from a loop cycle into a array, then print it?
    Do i need to make another loop? if so how do i go about that? also if what i ask is possible, when every time i re-run the compiler will the array above get re-written or erased(hope it does)?
    basic as possible please if not please explain thank you
    Last edited by Shaquille Daley; 11-10-2012 at 05:58 AM.

  2. #2
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    1)Create a array...
    2)increment it along with index..
    Code:
    int arr[10];
    int master[10];
    int i=0;
    for(int index=0;index<=10;index++,i=i+2)
    {
       master[index]=index;
    a[i]=index++;
    }
    0 2 6 8 10 indexes of 'a' array have corresponding index+1 values..

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Quote Originally Posted by thriller500 View Post
    1)Create a array...
    2)increment it along with index..
    Code:
    int arr[10];
    int master[10];
    int i=0;
    for(int index=0;index<=10;index++,i=i+2)
    {
       master[index]=index;
    a[i]=index++;
    }
    0 2 6 8 10 indexes of 'a' array have corresponding index+1 values..
    this gives me overflow error

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    There are a number of problems initially.
    Code:
    srand((unsigned)time(0));
    You are probably seeding too much in this function when you should be doing it somewhere else. Each time you seed, you restart the random number generator, which will probably result in not very random numbers. You might want to read this page: Eternally Confuzzled - Using rand()
    Code:
       for(int index= 0; index<10; index++)
        {
             x= rand() % 18;
     
     
                 h = master[x];
     
     
            cout << h << endl;
        }
     
     
        mix[10] = h;
    x is not chosen properly. It could be that x holds a value bigger than the acceptable range of master indexes. And there is no master[10]. Rather the size of the array comes from the possible indexes 0,1,2,3,4,5,6,7,8, and 9 being 10 numbers.

    Hi, i'm new to c++ and my question is how do i store data from a loop cycle into a array, then print it?
    Do i need to make another loop? if so how do i go about that?
    It depends on when you want to display the data mostly.
    Code:
    string master[4] = { "alpha", "beta", "gamma", "delta" };
    string s;
    for (int i = 0; i < 4; i++ )
    {
        cout << "Enter new string: ";
        cin >> s;
        master[i] = s;
        cout << i << ". " << master[i] << endl;
    }
    Something like that is as valid as a separate loop that just displays the data. It ultimately shouldn't matter if the index is randomly picked, the idea doesn't change.

    Also I should tell you that do ... while (false); will only ever run once.

    also if what i ask is possible, when every time i re-run the compiler will the array above get re-written or erased(hope it does)?
    There are few guarantees in the area. Given modern computers chances are that the array will be in a different place each time you run the program thanks to address space layout randomization. And also, since you are using automatic memory, you really don't need to worry about that memory returning to the system after the program ends. It will be taken care of for you, automatically.
    Last edited by whiteflags; 11-10-2012 at 08:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing data from text file into array?
    By flaris in forum C Programming
    Replies: 6
    Last Post: 12-02-2010, 02:19 PM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  4. storing data into an array
    By aama100 in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2008, 06:12 AM
  5. Replies: 5
    Last Post: 10-02-2005, 12:15 AM