How to label the first input of user as 1 ,second input 2, third input 3..etc by using ofstream append? Everytime when I used terminal to run it
e.g.
./addcontact.cpp name1
./addcontact.cpp name2

so that inside dat file it will has number of sequence in front of new name?
e.g. in dat file

1 name1
2 name2
3 name3

int main(int argc, char* argv[])
{
	int i =0;
	vector <string> input;
	Contact addingname;
	
if (argc == 2)
{
	input.push_back(argv[1]);
	cout<<"Ok, contact "<<i+1<<" "<<input.at(i)<<" added "<<endl;
	string newname = input[i];
	int size= input[i].size();
	
	for(int j = 0; j<size; j++)
	{
		if (isalpha(newname[j]))
		{
		addname(newname);
		
		;}
	}
void addname(string name)
{
i++;
  ofstream myfile(CONTACTS, ios::app);
  if (myfile.is_open());
  {
  myfile<<i<<name<<endl;
  myfile.close();
  }

}

I used these codes but they are not working? and gave me undesired output...any idea how to modify it?

Recommended Answers

All 2 Replies

First I would be really careful about calling a variable 'size'. It could definitely clash with other things.

Let me recommend that you add comments to the major portions of your code. This will not only help us read what is going on in your code, but I bet once you translate that code to English you will understand what is going wrong.

ok, thanks for the advice anyway:)

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.