0

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?

Edited by Bigbrain99: n/a

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Bigbrain99
0

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.

This question has already been answered. 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.