Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert *char type variable in vector object

Status
Not open for further replies.

rk0000

Technical User
Mar 15, 2002
33
IN
am using visual studio 6 on Windows Xp. I have problem while using Vector class:
I am reading a char* from the user input for say 10 times (num_InstanceIds = 10)
char* tempPtr ;
for(unsigned int i = 0; i < num_InstanceIds; i++)
{
fprintf (stdout, " Enter the Instance id %d:", i+1);
// GetChar will return *char type from the user input
tempPtr = GetChar(" ", "");
// tempPtr is not getting inserted in the vector ?
m_InstancedIds.push_back(tempPtr);
// I tried various things in the above line
}
STD::vector <char>::iterator v1_Iter;
for ( v1_Iter = m_InstancedIds.begin( ) ; v1_Iter != m_InstancedIds.end( ) ; v1_Iter++ )
{

fprintf (stdout, "\n");
fprintf (stdout, " Vector is %s:", *v1_Iter);

}
Authored by: rama_krishna
 
> GetChar will return *char type from the user input
But who allocates that space ?
Is it allocated at all?
Is it a unique allocation for each line of input? If it isn't a unique allocation for each line, then it won't work as expected.

> STD::vector <char>::iterator v1_Iter;
Why isn't this a char* iterator to match the vector ?
Why is STD in upper case?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
You may want to consider using:
Code:
vector<string>
instead of:
Code:
vector<char*>
Then you don't have to worry about managing the pointer allocations/deletions yourself.
 
My m_InstancedIds is declared like this
STD::vector<char> m_InstancedIds;
I just need to insert *char type variables into this vector m_InstancedIds.
 
Hi,
I have changed the m_InstancedIds definition.
STD::vector<STD::string> m_InstancedIds;
I have created str1 of type STD::string
Read the *char type return in to str1.
str1 = GetChar(" ", "");

STD::vector <STD::string>::iterator v1_Iter;
for ( v1_Iter = m_InstancedIds.begin( ) ; v1_Iter != m_InstancedIds.end( ) ; v1_Iter++ )
{

fprintf (stdout, "\n");
fprintf (stdout, " Vector is %s:", (*v1_Iter).c_str());

}

It is working fine now....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top