I have created two classes:

class dm_Step
{
public:
	char m_plus;
	char m_particle;
	float m_energy_sec;
	float m_xpos_sec;
	float m_ypos_sec;
	float m_zpos_sec;
};
class dm_Track
{
public:
	vector<dm_Step> m_track;
}

I have a data base with hundreds of lines of code in the following format:

+ gamma 2.306013 -0.213225 0.928826 0.303014
+ electron 1.640281 0.197209 -0.866881 -0.457850
+ gamma 2.472920 -0.760897 0.647127 -0.047567
+ proton 18.087228 0.834184 -0.333168 0.439472
+ neutron 1.765848 0.872917 -0.381746 -0.303787
+ proton 38.739559 0.927977 0.165262 0.333986

I need to use my class to read in each line of the code using the sscanf function. Here is what I have so far:

dm_Track current_track;

while (myfile.good())
	{

         getline(myfile, line);
         sscanf(line.c_str(), "%c %c %f %f %f %f %f", &(current_step.m_plus), &(current_step.m_particle), &(current_step.m_energy_sec), &(current_step.m_xpos_sec), &(current_step.m_ypos_sec), &(current_step.m_zpos_sec));
          current_track.m_track.push_back(current_step);
        }

I need to do it this way so I can access various part of the data base. For example, if I wanted to know the type of particle on the second line of the data data base I could do:

cout << "particle_sec_line = " << current_track.m_track.at(1).m_particle << endl;

which would print 'electron'. When I try to this, it will only print the first character of the particle name. More significant however, when I try to access m_energy_sec it will print something like 8.0e9 which just seems like an uninitialized value. Is anybody familiar with the sscanf function and can tell me what I can do to fix this problem? I know there might other ways to do this but the way the rest of my code is setup, it would make it much simpler to do what I need to do using this exact method.

When I try to this, it will only print the first character of the particle name...

That's because your scanning a character

sscanf(line.c_str(), "%c %c %f %f %f %f %f", &(current_step.m_plus), &(current_step.m_particle), &(current_step.m_energy_sec), &(current_step.m_xpos_sec), &(current_step.m_ypos_sec), &(current_step.m_zpos_sec));

In you format string the second specifier is %c which is character. Try using %s for the second specifier.

sscanf(line.c_str(), "%c %s %f %f %f %f %f", &(current_step.m_plus), current_step.m_particle, &(current_step.m_energy_sec), &(current_step.m_xpos_sec), &(current_step.m_ypos_sec), &(current_step.m_zpos_sec));

Just noticed

class dm_Step
{
public:
	char m_plus;
	char m_particle;//you'll have to change this to a character array like char m_particle[20]; 
	float m_energy_sec;
	float m_xpos_sec;
	float m_ypos_sec;
	float m_zpos_sec;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.