Hi guys, actually i'm trying to write a program which will write the user input into dat file and start to count the user input from 1. When user inputs second value,
it will go to other line and put 2 in front of user value. Any idea how to modify the below codes in order to make it like a counter that will put 1 at first input and then 2 at second input? Thanks!! Need to use this in project...:S

int addname(string name)
{
	char aline; 
	int a = 1;
	
	ifstream myfile;
	myfile.open (CONTACTS);
	if (myfile.is_open())
	{
		while (myfile.good())
		{
			aline = myfile.get();
			if (aline == '\n')
			{
				a++;}
			}
			myfile.close();
		}
	else
  {
	ofstream myfile(CONTACTS, ios::app);
	myfile<<a<<name<<endl;
	myfile.close();
	}
	return a;  
}

Just do something like this :

1 : open file to read
2 : open file to write
3 : create a counter variable starting at 1
4 : get user's input
5 : write to file the value of counter variable
6 : write to file the value of the user's input
7 : write to file a newline character
8 : increment the counter variable
9 : repeat 4-8 until done
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.