read string and int in template

I am trying to read a file and search through the file to find the input but getting a little trouble with it

I have the template as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  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

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
40
41
42
43
44
45
46
47
48

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.
thats what i was afraid of i guess there is no easier way to write it
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.
Last edited on
Topic archived. No new replies allowed.