input file name and open file

Hi. My program asks the user the create a recipe name, enter ingredients then method. the program writes this information to a text file perfect, BUT I want the user to be able to search for previously created recipe files by recipe name and then display all information on the screen. Any help would be great. Thanks


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
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    char Menu,Create,Find;
    cout << "Recipe Database\n" << endl;
    cout << "Press 1 to create a new recipe: \n";
    cout << "Press 2 to find existing recipe: \n";
    cin >> Menu;
    cout << "\n\n";
        if (Menu == '1') {
                ofstream f;
                string filename;
                string ingredient;
                string method;
                cout << "PLEASE-SEPARATE-ALL-WORDS-WITH-A-HYPHEN - \n";
                cout << "PRESS $ FOR NEW LINE:\n\n";
                cout << "Please enter new recipe name: ";
                cin >> filename;
                filename += ".txt";
                f.open( filename.c_str() );
                f << filename;
                f << "\n\n";
                cout << "Please enter ingredients:";
                cin >> ingredient;
                f << "Ingredients: \n";
                string s = ingredient;
                replace( s.begin(), s.end(), '$', '\n' );
                f << s << endl;
                f << "\n\n";
                cout << "Please enter method: ";
                cin >> method;
                f << "Method: \n";
                string d = method;
                replace( s.begin(), s.end(), '$', '\n' );
                f << s << endl;
                f << "\n\n";
                f.close();
                cout << "Good job! Your recipe has been created.\n";
                }
                if (Menu == '2') {cout << "Enter Recipe Name: ";
                }


                cout << "\n\n\n\n";
                return 0;
                }
what you are doing is creating new file for each recipe which according to me is not necessary.(you can use classes too)But if you want that then...

1)open file for input.(as you have created new file for each recipe).If open fails then show message
2)As it is text file get text with simple "f>>"
3)and display that.

Note:Instead for asking user to enter words with "-",you can use getline function to get input.
Might wanna open this file with the ios::app flag, also.
Topic archived. No new replies allowed.