Thread: creating new objects in loop

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    creating new objects in loop

    I'm trying to figure out how to create new objects based on user input. Here is a sample output to help my description:

    Enter value for object: 4 // this would set a value for object1 as 4
    Do you want to enter another? yes
    Enter value for object: 3 // this would set a value for object2 as 3
    repeat...

    Here is what I have without any logic for creating the new object in the loop (this program just overwrites the "object" object in each loop instead of making object1, object2, object3 etc...:

    Thanks for your time
    Hail to the Redskins.

    Code:
    #include <iostream> 
    using namespace std; 
    
    class CRecord
    {
    public:
    	CRecord();		//default constructor
    	int getnum();	//get function
    	int num;
    };
    
    CRecord::CRecord() //default constructor
    {	
    	num=0;
    }
    
    int CRecord::getnum() // a get utility function
    {
    	return num;
    }
    
    
    
    int main()
    {
    	int counter =1;
    	
    	while ( counter <= 5 ) {
    	
    		CRecord object;
    	
    		cout << "enter value for object: " ;
    		cin >> object.num;
    		cout << endl << object.getnum() << endl;
    		counter++;	
    	}
    
    	return 0;
    
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Don't make data members public within a class. Make them private (or protected) and then overload the input operator.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    If you want different objects, you must keep track of the different objects. In your example, you only create one object at a time. Technically speaking, you create 5 objects, but each one goes away before you create the next one.

    One solution is a vector or array of CRecord objects. In your code example you would make it a size of 5 and create it before your loop. Then, use the counter variable to index the array and set the values appropriately. Be careful, though, because you start counter at 1 and arrays are indexed from 0 to size-1.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Smile

    Thanks guys, - I think I see the light. I'll try the array of objects and reply again if I have more questions.

    I kept everything public because I was trying to make keep my example code simple.

    Thanks so much for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  2. Creating pthreads in for loop
    By 0624167 in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2008, 12:10 PM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM