Click here to Skip to main content
15,883,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i create the coding for accessing the element of the structure

coding:


#include "stdafx.h"
#include <vector>
#include <string>
using namespace std;
struct myStruct
{
	int iTemp1;
	int iTemp2;
};
int main(int argc, char* argv[])
{
	int iTemp = 0;
	int iLoop = 0;
	int iMark;
	vector <mystruct> *myvec;
	vector <mystruct>::iterator *it;
	cout<<"How many times wants to insert \n";
	cin>>iTemp;
	for(;iLoop<itemp;iloop++)>
	{
		myvec = new vector<mystruct>
		myStruct *mystr=new myStruct();
		cout<<"Enter the data for m1 \n";
		cin>>iMark;
		mystr->iTemp1 = iMark;
		iMark = 0;
		cout<<"Enter the data for m1 \n";
		cin>>iMark;
		mystr->iTemp2 = iMark;
		myvec->push_back(*mystr);
	}


	return 0;
}



This coding working without no error..

if i try to print the value the last entered element only coming..

please help me.


Reformatted with proper tags.
-E-
Posted
Updated 21-Jul-11 22:16pm
v4
Comments
code is missing i think.repost the full code.
Dhanasundhari 22-Jul-11 3:14am    
now i repost my code sir

Apart from what PrafullaVedante said, you are also needlessly creating a struct on the heap, and then forgot to delete it. Just create it on the stack like this:
myvec = new vector<mystruct>; // <-- see Solution 1
    for(;iLoop<itemp;iloop++)>
{
    myStruct mystr; // <-- do not create this with new!
    cout<<"Enter the data for m1 \n";
    cin>>iMark;
    mystr.iTemp1 = iMark; // <-- changed '->' to '.'
    iMark = 0;
    cout<<"Enter the data for m1 \n";
    cin>>iMark;
    mystr.iTemp2 = iMark; // <-- changed '->' to '.'
    myvec->push_back(mystr); // <-- removed '*'
}

Note that std::vector::push_back() will create a copy of the passed value anyway, so there is no need to create new instances of myStruct yourself.
 
Share this answer
 
v2
Comments
Richard MacCutchan 22-Jul-11 7:18am    
+5 for a very clear explanation.
Stefan_Lang 22-Jul-11 7:53am    
Thank you, Richard.
John R. Shaw 23-Jul-11 12:29pm    
5 - good answer
Stefan_Lang 25-Jul-11 3:51am    
Thank you
Brought following statement out of the for loop

myvec = new vector;



You are creating a new vector for each interation.


Your code should look like
myvec = new vector;

for(;iLoop<itemp;iloop++)>
{

myStruct *mystr=new myStruct();
cout<<"Enter the data for m1 \n";
cin>>iMark;
mystr->iTemp1 = iMark;
iMark = 0;
cout<<"Enter the data for m1 \n";
cin>>iMark;
mystr->iTemp2 = iMark;
myvec->push_back(*mystr);
}
 
Share this answer
 
v2
Comments
Dhanasundhari 22-Jul-11 5:28am    
i got it.. thanks
Stefan_Lang 22-Jul-11 5:34am    
Good catch. my 5
PrafullaVedante 22-Jul-11 7:06am    
Thanks Stefan :)
John R. Shaw 23-Jul-11 12:25pm    
Sorry 3 - it was a 4 for unneeded 'new' in the loop, but dropped to 3 because it will leak - no 'delete'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900