template <class T>
T find_replace(T* input, T file) {
ifstream file;
T line;
if (file.is_open())
{
while (getline(file, line) {
if (line[i] == line[i]) {
return line;
}
else {
return"not in the file ";
}
}
}
else {
return"file did not open try again ";
}
};
when to call the file in the main function I get this I'm getting errors am I just missing something simple here any help will be appreciated
string del_name, del_phone_num, del_date;
int del_order_num, del_choice;
cout << "delete from the file enter 0 if you wish to exit anytime " << endl;
cout << "delete: \n name:1 \n order number:2 \n phone number:3 \n date:4 \n exit:0 \n" << endl;
cin >> del_choice;
bool quit = false;
while (!quit) {
switch (del_choice) {
case 1:
cout << "which name you would like to delete and/or replace" << endl;
cin >> del_name;
find_replace <string>(&del_name, "file.txt");
break;
case 2:
cout << "which order number you would like to delete and/or replace" << endl;
cin >> del_order_num;
find_replace <int>(&del_order_num, "file.txt");
break;
case 3:
cout << "which phone number you would like to delete and/or replace" << endl;
cin >> del_phone_num;
find_replace <string>(&del_phone_num, "file.txt");
break;
case 4:
cout << "which date you would like to delete and/or replace" << endl;
cin >> del_date;
find_replace <string>(&del_date, "file.txt");
break;
case 0:
cout << "you would like to exit goodbye " << endl;
quit = true;
break;
default:
cout << "invalid input" << endl;
break;
}
}
find_replace<int> means that every T in the find_replace template becomes an int, including the return type since you have the return type being T.
Returning "not in the file" (a string/const char*) makes no sense if the return type is int.
Perhaps you should just write two separate functions, one for strings, and one for ints.
If you show us how the file is formatted, someone might be able to help with a more robust solution. I didn't mention other things wrong with your function, e.g. the fact that you never even use your input variable, and that you are comparing line[i] (a character) with itself on line 9.
Also, it's not clear what "delete and/or replace" means in your instructions. At least, not to me.
Given a name, for instance, what would determine whether it's "replaced" or "deleted"? Replaced with what?
Furthermore, you don't do anything with your return value in find_replace, so you really don't even need it to begin with.