0

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;  
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by firstPerson
0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.