For loop problem

Hello guys

It's just a registration form for which I am taking a person name and the person phone number and store it in a data file I use a loop in this I want if a 1st user register in the data file the person record store to show that it's the first member then if a next person register it store in data file that the user registered on a second number and so on
But it has a problem that when I register 1st person in a file it shows that this person is first and when I register second person for the second person it still shows 1st person.
Here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<fstream>
using namespace std;
main()
{
	string name,phone_number;
	cout<<"Enter your name: ";
	cin>>name;
	cout<<"Enter your phone number: ";
	cin>>phone_number;
	ofstream data("data.txt",ios::app);
	for(int i=1;i<100;i++)
	{
		data<<i<<'\t'<<name<<'\n';
		data<<" \t"<<phone_number<<'\n';
		data.close();
	}
}



When I compile this code in data file it store in a data file in this format
1
2
3
4
1    1st_name
     111111111
1    2nd_name
     222222222


But I want it show me in a data file like in This format.
1
2
3
4
1    1st_name
     111111111
2    2nd_name
     222222222
You are entering your data one time, and then the program TRIES to append the same data to the file 99 times.

Restructure your program, move lines 6-10 to after the beginning of the for loop at line 12.

And move the file close statement after the end of the for loop.
Last edited on
I see that you execute this line 100 times.
data.close();

That doesn't seem right.
Hello furry guy
Can you please show me an example through which I can take help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>  // for std::string

int main()
{
	std::ofstream data("data.txt", std::ios::app);

	// this for loop will loop 99 times, not 100
	for (int i = 1; i < 100; i++)
	{
		std::cout << "Enter your name: ";
		std::string name;
		std::cin >> name;

		std::cout << "Enter your phone number: ";
		std::string phone_number;
		std::cin >> phone_number;

		data << i << '\t' << name << '\n';
		data << " \t" << phone_number << '\n';
	}
	data.close();
}

Are you really wanting the user to enter 99 records and adding all 99 to the file? If you are adding ONE record a for loop is not needed.

If you are wanting to add a number of records as determined by the user then a while or do-while loop might be a better choice. Each loop iteration you ask the user if they want to enter another record.
Hello furry Guy
Thank you so much for showing interest in my problem

No actually I want a one record I want the user put one record and then the program close and it store in a data file on the 1st number then when a user open the program again and register then the user data store on 2nd number
No actually I want a one record I want the user put one record and then the program close and it store in a data file on the 1st number then when a user open the program again and register then the user data store on 2nd number


So what's with the 100 loop? If you just want one record to be entered and appended to the file each time the program is run, just remove the for loop.
One record entered, appended to the end of the file. You do not need a for loop.

With no for loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>
int main()
{
   std::ofstream data("data.txt", std::ios::app);

   std::cout << "Enter your name: ";
   std::string name;
   std::cin >> name;

   std::cout << "Enter your phone number: ";
   std::string phone_number;
   std::cin >> phone_number;

   data << name << '\n';
   data << phone_number << '\n';
   
   data.close();
}

Now your file format is:
name
number
name
number
.......(and so on)
Hello furry guy
Can you show me an example
I want if user run the program and register the one 1st person in a program It store in a data file in this format
1
2
1    1st_name
       11111111


Then when the program close and the user open the program again and register 2nd person it save in data file in this format
1
2
3
4
1    1st_name
       11111111
2    2nd_name
       11111111


As like that and so on as how many times the user open the program and register someone it's execute the numbers
And save the peoples data on the numbers like 1,2,3 and so on
Last edited on
> And move the file close statement after the end of the for loop.
get rid of the .close(), let the destructor do its job

> I see that you execute this line 100 times.
99


@OP: you'll need to persist the number of records that are on the file
for example, save it to disk
Hello ne555
I don't want to execute the data 99 time
It's just a loop to track the numbers that this person is register on that number
For example
1
2
1    Ali
     111111


Ali is register on 1st number

1
2
3
4
1    Ali
     111111
2    bob
     222222



As like that
Like Ali is register on the 1 number and Bob is register on the 2 number
Last edited on
Without storing the number of extant records you can open the data file to read all the records and close. Then ask the user for the new record. Open the file for appending and write the new data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>

int main()
{
   std::string data_file { "data.txt" };

   // open file for reading
   std::fstream data(data_file, std::ios::in);

   std::string temp_str;
   unsigned index { 1 };

   // read each index #, name and number from the file until the end
   while (data >> temp_str >> temp_str >> temp_str) { index++; }
   data.close();

   std::cout << "Enter your name: ";
   std::string name;
   std::cin >> name;

   std::cout << "Enter your phone number: ";
   std::string phone_number;
   std::cin >> phone_number;

   // open the file for appending
   data.open(data_file, std::ios::app);

   data << index << '\t' << name << '\n';
   data << '\t' << phone_number << '\n';

   // no need to explicitly close the file
}

Why read the file three times for each record? Whitespace is a delimiter for std::cin, a tab character is whitespace.
Thank you so much For helping me Furry Guy
it is working great what i was expecting.
A couple of things to note about that code.

1. File errors. I don't check for problems when opening the file, or reading already stored data. Good code would check for that, and 'fail gracefully' if the file can't be opened or properly read. For reading and writing.

2. What if the name (or number) the user enters has spaces in it? (Hint: whitespace) std::cin will read the characters up to the space and it's done. Any extra characters in the stream are still there. If the user enters a name like "Abu Ben Fooey" std::cin will read only "Abu" leaving the rest to be read for the number. The number will be read automatically as "Ben." The user will not be able to enter a number.

Something similar with entering a number as a string. User enters "123 456 7890" and your file records "123."

To get data input with whitespace as valid characters you can use std::getline() with the string.
https://en.cppreference.com/w/cpp/string/basic_string/getline

That could lead to another problem when you mix using std::cin and std::getline().
https://www.learncpp.com/cpp-tutorial/4-4b-an-introduction-to-stdstring/
@Furry Guy - yes you're right about your code limitations. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	const std::string data_file {"datatel.txt"};
	unsigned index {1};

	// open file for reading
	std::fstream data(data_file, std::ios::in);

	if (data.is_open()) {
		for (std::string temp_str; (data >> temp_str) && std::getline(data, temp_str) && std::getline(data, temp_str); ++index);
		data.close();
	}

	std::cout << index - 1 << " existing records found\n\n";

	std::string name, phone_number;

	while ((std::cout << "Enter your name: ") && (!std::getline(std::cin, name) || name.empty())) {
		std::cout << "Invalid name\n";
		std::cin.clear();
	}

	while ((std::cout << "Enter your phone number: ") && (!std::getline(std::cin, phone_number) || phone_number.empty())) {
		std::cout << "Invalid phone number\n";
		std::cin.clear();
	}

	// open the file for appending
	data.open(data_file, std::ios::app);

	data << index << '\t' << name << '\n';
	data << '\t' << phone_number << '\n';

	// no need to explicitly close the file
}

Topic archived. No new replies allowed.