I have written a code for getting inputs from a file. I need to parse the input string and then putput it to another file. The input file contains multiple lines. I am not sure what I am doing wrong. Please see the attached code.

Code:
****************************************************



#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

string line;
string sn;
string cn;
int pos7 = 0;


ifstream myfile;
myfile.open("passwd");
if (myfile.is_open())
{
		while (! myfile.eof())
		//while (! getline (myfile,line));
			{
			getline (myfile,line);
			int pos1=line.find(":",0);
			string uid=line.substr(0,pos1);
			int pos2=line.find(":",pos1+1);
			int pos3=line.find(":",pos2+1);
			int pos4=line.find(":",pos3+1);
			int pos5=line.find(":",pos4+1);
			if (pos5 == pos4+1) 
			{
						cn=uid;
				}
				else{
			cn=line.substr(pos4,pos5-pos4);
				}
				int pos6=cn.find("",0);
				if (pos6!=0)
					{
							pos7=cn.find("",pos6+1);
							if (pos7!=0)
							{
								sn=cn.substr(pos7,pos7-pos5);
							}
							else 
							{
								sn =cn.substr(pos6,pos6-pos5);
							}
						}
				else {
					 sn=cn;
				}
########################################
The output gives a abnormal program termination. when i try and print out all the values everything is fine except the pos7 value which is shown as negative. also the input line is only taken as the last line of the file. Please any kind of guidance will be welcome.