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.
#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.
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
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.
#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.
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."