ofstream myfile;
myfile.open("data.txt");
cout << "Update the existing data"<<endl;
std::cout << endl << "Please enter data : ";
std::string data;
getline(cin,data);
while (data.empty() || data!= data)
{
if (std::getline(std::cin, data))
{
if (data.empty())
{
std::cout << "You pressed enter without typing anything" << std::endl;
continue;
}
elseif (data != data){
std::cout << "There is no data. Please try again." << std::endl;
}
elseif (data== data){
//trying to update the data when the user enter a correct data.
}
}
myfile.close();
the reason is if the user enter the data that is not stored in the txt file , it will then prompt an error. Thats why I put data != data if the user enter a data that is not exist. The way you do does not apply myfile.open() btw. Just need to know how to update the data from my code.
> The way you do does not apply myfile.open() btw
I know.
But then again, neither were you.
But before we go anywhere near writing to the file, you need understand that comparing something with itself is trivially true - with the possible exception of floating point NaN's.
I tried my best on find the resources and do the code. However I'm still having an error which is [Error] no matching function for call to 'getline(std::ofstream&, std::string&)'. Need help here....
I think what you need to do it describe, or post the instructions you were given, what the program needs to do. Also post any input file(s) that the program will need.
I am thinking the best course is to re-plan and rewrite the program instead of trying to fix parts of the code that do not work.
ofstream myfile;
myfile.open("data.txt"); // <--- Opend file and erases anything in the file.
cout << "Update the existing data" << endl; // <--- I think this is what you want, but it is also misleading based on the code that follows.
std::cout << endl << "Please enter data : "; // <--- What kind of data?
std::string data;
getline(cin, data);
while (data.empty() || data != data) // <--- The or part is not needed and will always return false.
{
if (std::getline(std::cin, data)) // <--- Where is the prompt and why do you feel this is needed again this soon?
{
if (data.empty()) // <--- Already done in the while condition.
{
std::cout << "You pressed enter without typing anything" << std::endl;
continue;
}
elseif (data != data) // <--- Useless. Always false.
{
std::cout << "There is no data. Please try again." << std::endl;
}
elseif (data == data) // <--- Always true.
{
//trying to update the data when the user enter a correct data.
// <--- To soon to update data. You have done nothing to get something to update or changed anything.
}
}
myfile.close();
std::cout << endl << "Please enter data: ";
std::string data;
getline(cin, data);
ifstream file; // <--- Defined file stream, but never open file.
ofstream temp;
temp.open("temp.txt");
while (getline(myfile, data)) //this is the error i occured // <--- Where did you define and open "myfile"?
{
if (data != find) // <--- Where did you define "find"?
temp << data << endl;
}
myfile.close();
temp.close();
remove("data.txt");
rename("temp.txt", "data.txt");
std::cout << endl << "Please enter name your name : ";
std::string name;
getline(cin, name);
The comments should explain most of what is wrong.
If you are going to bury the definition of a variable in the middle of other code. Use some blank lines to set it off so it can be found.
In line 12 of the second code not only is "myfile" not defined you are reading from the file into "data" which you have already given a value to. So how do you expect to find any match?
You have been given good information which you have mostly ignored except for the parts that you have copied and I am not sure if you understand what you have copied.
That is why Im having problem when updating the file. I have already managed to insert the data into the file. I understand what you meant but just need time to digest.