How do i combine this into text editor

Hello. Im not sure if i am able to create a post just after i got solved another one but i hope so. So here is my qestion. I need to connect somehow those codes.
So it should be something like microsoft notebook. Im not asking for full code, just some hints what am i supposed to do. I have no idea what to do exactly to be hohest

This is my main code:

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
  #include <iostream>
#include <fstream>
#define MAX_CHARS 4050
using namespace std;

/*
void openFile()
{
    ofstream file2("text2.txt"); // create and open text file
    file2 << "Hello there"; // write in file
    file2.close(); // close file
}
*/

void readFile(char text[MAX_CHARS])
{

    string line;
    ifstream myfile ("MyFile.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )   // do when true, false only when there are no lines
        {
            cout << line << endl;
        }
        myfile.close();
    }
    else
    {
        cout << "Unable to open file";
    }
}

using namespace std;

int main()
{

    char text[MAX_CHARS];

    ofstream file2("text2.txt");
    readFile(text);
    return 0;
}


This is a thing where i can type in coordinates:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <curses.h>

int main()
{
    initscr(); //

   
    mvprintw( 5, 5, "Hello, World!" );

    getch();  

    endwin(); 
    return 0;
}

And here i can get code for buttons:
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
  #include <curses.h>

int main()
{
    initscr();
    keypad(stdscr, true);  
    noecho();              
    halfdelay(100);         

    printw("Press F2 to exit.\n");

    bool ex = false;
    while ( !ex )
    {
        int ch = getch();

        switch ( ch )
        {
        case ERR:
            printw("Please, press any key...\n"); 
            break;
        case KEY_F(2): 
            ex = true;
            break;
        default:  
            printw("Code of pressed key is %d\n", ch);
            break;
        }

        refresh(); 

    }

    printw("Thank you. Good buy!");
    getch();
    endwin();
    return 0;
}
Last edited on
You have 3 main programs, so the first thing to do is change "main" to be a function of another name in 2 of the programs. You need to remove any "main program" junk (I/O that should be a parameter for example, or nonsense I/O like "thanks for using my code") and clean it up.

Then you can build a new main program that calls your functions and has all 3 pieces of code available to use.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <fstream>
#include <curses.h>

#define MAX_CHARS 4050
using namespace std;

/*
void openFile()
{
    ofstream file2("text2.txt"); // create and open text file
    file2 << "Hello there"; // write in file
    file2.close(); // close file
}
*/

void readFile(char text[MAX_CHARS])
{

    string line;
    ifstream myfile ("MyFile.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )   // do when true, false only when there are no lines
        {
            cout << line << endl;
        }
        myfile.close();
    }
    else
    {
        cout << "Unable to open file" << endl;
    }
}

void editFile()
{
    initscr();
    keypad(stdscr, true);   //Enable the function key reading mode
    noecho();               //Turn off the display of the input characters, you need to getch()
    halfdelay(100);         //Time limit

    printw("Press F2 to exit.\n");

    bool ex = false;
    while ( !ex )
    {
        int ch = getch();

        switch ( ch )
        {
        case ERR:
            printw("Please, press any key...\n"); //Remind that need to press a button
            break;
        case KEY_F(2): //exit if f2
            ex = true;
            break;
        default:  //if everything is fine, use button code
            printw("Code of pressed key is %d\n", ch);
            break;
        }

        refresh(); //show on screen

    }

    printw("Thank you. Good buy!");
    getch();
    endwin();
}

void coordinates()
{
    initscr(); //curses

    //coordinates to x,y
    mvprintw( 5, 5, "Hello, World!" );

    getch();  //wait for button press

    endwin(); //exit from curses

}

int main()
{

    char text[MAX_CHARS];


    readFile(text);


    return 0;
}


Uhm sorry, i got it into 1 program and at least no errors but
1. When i have only "readingFile" in main, i have my text on display line by line
2. When i add "editFile" or "coordinates" after "readingFile" i dont have my text anymore.
I dont really know how to explain but please, look on those screenshots:
https://gyazo.com/f19705c5bb5948c8a10811fea6d50545
https://gyazo.com/09f4e909d6d305ad3d92dfe52521ba76

Topic archived. No new replies allowed.