Problem interpreting data

I have a data base that looks like the following:

7.410510 0.624145 0.099997 21.497558 0.152266 -0.426159
6.812658 0.597852 0.099995 21.596834 0.161156 -0.434175
6.216810 0.595847 0.099995 21.694645 0.175570 -0.449149
5.486792 0.730018 0.099985 21.792595 0.189309 -0.463781
4.731234 0.755558 0.099993 21.890338 0.203853 -0.479055
3.834733 0.896501 0.099976 21.986879 0.222474 -0.497176
+ proton 0.099995 21.694645 0.175570 -0.449149
3.712991 0.121742 0.013676 22.000000 0.225076 -0.500024
+ neutron 0.099995 21.694645 0.175570 -0.449149
2.581185 1.131805 0.099960 22.096001 0.244601 -0.519888
1.155000 1.426186 0.086050 22.178597 0.260305 -0.538215

I am writing a program that reads in this data and performs some calculations. My program uses a while loop to interpret this data. The program will perform the same operation as it reads in the data. However, as you can see, occasionally a line will begin with "+ proton" or "+ neutron" in which case I need my program to perform a different operation in which it will look in different data bases. How do I properly read in each character of each line so that my program can properly 'know' when it needs to look in another data base? I read in the data with a while loop which starts like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// read in data
ifstream myfile("C:\\Data");

while (myfile.good())
	{
		getline(myfile, line);

		if (&line.at(0) == "+", &line.at(2) == "n")
			neutron_function(); 
			
		else if (&line.at(0) == "+", &line.at(2) == "p")
			proton_function();

		else if (line == "end")
			break;
		
		else if (line == "")
		{
               .
               .
               .


Can anyone think of a better way to interpret each character within a line so that my program is aware when the data reads "+ proton" or "+ neutron"?
Last edited on
Since you are using std::string's, you can use the std::string::find() method to find a specific substring. The method will return the index where the occurrence starts, so in your case you need to use if (line.find("+ proton") == 0)....
thanks that seems to work
Topic archived. No new replies allowed.