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?

Dani AI

Generated

Two quick, concrete reasons the file gets wrong output and how to fix them (based on the code in the thread):

  • The character loop validates each character and calls the writer repeatedly. That means a valid name will cause N writes (one per character) instead of one. Validate the whole string once, then call the write routine a single time.
  • The counter used for the sequence (your i) is in the wrong scope. Maintain the next ID by reading the file (or a dedicated counter file) before appending, or pass/return the counter explicitly.
  • A stray semicolon after if (file.is_open()) makes the check useless (the block always runs). Also write a separator (space) between the number and the name.

A small, robust approach: read the existing file to find the last numeric ID, validate the command-line name in one check, then open the file in append mode and write id + ' ' + name. Handle file-open failures and validate characters with std::all_of (use static_cast<unsigned char> when calling std::isalpha). Example pattern:

// compute next id by scanning lines and parsing the leading number
int nextId(const std::string& path);

// validate name in one pass; allow spaces/hyphens if you want
bool validName(const std::string& s);

// append a single line like: 42 John Doe
void appendContact(const std::string& path, const std::string& name);

Troubleshooting tips: run the program multiple times and inspect the file after each run; check return codes when opening files; trim newline/CR characters if the file was created on another platform. If concurrent runs are possible, add a file-lock (flock or platform-specific locking) or use a single process to serialize updates.

’s tip about sensible variable names and comments is spot on — rename counters and sizes to descriptive types (use size_t, int for IDs) and comment intent. For : remove the per-character write, fix the scope of the counter (or compute it from the file), fix the if (is_open) usage, and append a space between number and name.

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.