merging programs??

So i have three ncurses programs (each one has a main, a cpp and a few header files and text files) and one oop (with text files and one main).

This is the idea of how the finished ONE program should look:

choose option:
1
2
3

if option 1 is chose then 'x' ncurses program is shown
if option 2 then 'y' ncurses program is shown
and so on.

How would you suggest I merge these programs?
Write a fourth program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char** argv)
{
  if (argc != 2) 
  { 
    print_usage(argc, argv);
    return 1;
  }

  if (argv[1] == "1"sv)
    system("run x"); // eg
  if (argv[1] == "2"sv)
    system("run y");
  // ...
}
Last edited on
@mbozzi

so if argv[1] == "1"sv
system("run x")
means that if the user selects option 1 then this ncurses program is run?


Yes. It's a sketch; you'll need to adapt the code for your own environment.

Or preferably, just write this fourth program in shell: that's what it exists for.
Last edited on
You can also just paste together the "main.cpp" (or equivalent) files into one program, rename functions or use namespaces as necessary to avoid name conflicts, and then do the logic mbozzi wrote, but change it to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    // get option from user
    if (option A)
    {
        mainA();
    }
    else if (option B)
    {
        mainB();
    }
    else if (option C)
    {
        mainC();
    }
}

And you'd need to copy any other header/implementation files.
Last edited on
thank you! @ganado
Hi so these are long long programs like these. Is it still possible to do it using if..else if...else if?

forgotpass.cpp

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <curses.h> 
#include <assert.h>   
#include "coord.h"  
#include <string> 
#include <fstream>
using namespace std;
fstream pass;
char c;  
string newpassword; 
string confpassword;
int movement;
enum movement {ebox,pbox,obox,cbox};
coord frontcursorloc[4]={coord(22,4),coord(22,7),coord(20,13),coord(38,13)}; 

void cycletab(){
    int i=movement++%4;
    move(frontcursorloc[i].my,frontcursorloc[i].mx);
    cout << frontcursorloc[i].my;
} 

void nxtbox(){
noecho();
keypad(stdscr, TRUE);  
cycletab();

while (int c=getch()){
int tab=(movement%4==0)?3:movement%4-1;
assert(tab >= 0 && tab < 4);
switch (c){ 

	case 10:
  case -53:
		//if (c==' '){ 
			if(tab==obox)return;
			if(tab==cbox){
   	 	    	newpassword="<no input>"; 
					confpassword="<no input>";
			return;
			}
      break;
		//}

	case KEY_RIGHT:
		cycletab();
		break;
 
	default:
    switch(tab){ 
      case ebox:
      if(int(c)!=127)printw("%c",'*');
       newpassword.append(1,c);
      break;
			case pbox:
       if(int(c)!=127)printw("%c",'*');
       confpassword.append(1,c);
      break;
        }

}
}
}

 
void displayresetpass(){  
	initscr(); 
	int x=0, y=1, w=60, h=15;
	WINDOW *win = newwin(h,w,y,x);
	refresh();
	move(1, 27);
	attron(A_BOLD);
	printw("RESET PASS");
	box(win, 0, 0);
	wrefresh(win);
}

void displaynewpass(){
	mvprintw(4,4,"new pass");
	WINDOW* ebox = newwin(3, 35, 3, 20);
	refresh();
	box(ebox, 0, 0); 
	wrefresh(ebox);
}

void displayconfPassword(){
	mvprintw(7, 4, "confirm new pass");
	WINDOW* pbox = newwin(3, 35, 6, 20);
	refresh();
	box(pbox, 0, 0);
	wrefresh(pbox);
}

void displayOkCancel(){
	WINDOW*  obox = newwin(3, 10, 12, 14);
	refresh();
	box(obox, 0, 0);
	attron(A_BOLD);
	mvwprintw(obox, 1, 1, "   OK");
	attron(A_BOLD);
	wrefresh(obox);


	WINDOW* cbox = newwin(3, 10, 12, 34);
	refresh();
	box(cbox, 0, 0);
	attron(A_BOLD); 
	mvwprintw(cbox,1, 1, " CANCEL");
	attron(A_BOLD);
	wrefresh(cbox);
}  

void comparepass(string newpassword, string confpassword){
	    if (newpassword != confpassword) 
        cout << "error: passwords do not match";
			else if (newpassword == confpassword) {
      	cout << "passwords match. pass has been reset.";
				pass.open("newpass.txt");
				pass<<confpassword;
				pass.close();
			}
			else
			cout<<"error";
}


int main(){
initscr();
noecho();
cbreak();
	displayresetpass();
	displaynewpass(); 
	displayconfPassword(); 
	displayOkCancel();
	nxtbox();
	getch();
	endwin();
cout << "\n\nnew-passw received was : " << newpassword << endl;
cout << "confirmed-passw obtained was : " << confpassword << endl;
comparepass(newpassword, confpassword);
	return 0;
}
oop.cpp

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
using namespace std;
#include <fstream>
#include <string>
#include <limits.h>

class user {
	public:
		char choice1;		
		string newpass;
		fstream usernames;
		fstream pswd;
		fstream npswd;
		fstream rnusernames;
		fstream rnpass;
		fstream rmaide;
};

class olduser : public user {
	private:
	string temp;
	string temp1;
	string temp2;
	string username;
	string userpass;
	string newuserpass;
	string loginyn;
	string mynewpass;

	public:
		int checkusername(){
		usernames.open("names.txt");
		cout << "\nplease enter your username   ";
		cin >> username; 
		while ((usernames >> temp)) { 
			if (temp.compare(username) == 0) {
				usernames.close();
				return 1;
			}
		}
		usernames.close();
		return 0;
		}
		
		int checkpassword(){
		cout<<"please enter your password   ";
		cin>>userpass;
		pswd.open("pass.txt");
			while (pswd >> temp1) { 
				if (temp1.compare(userpass) == 0){
					cout<<"\n[ "<<username<<" ] having password [ "<<userpass<<" ] has been found in the database.\n\n";
					pswd.close();
					return 1;
				}
			}
		pswd.close();
		return 0;
		}
		int checknewpassword(){
		cout<<"please enter your password   ";
		cin>>newuserpass;
		npswd.open("newpass.txt");
			while (npswd >> temp2) { 
				if (temp2.compare(newuserpass) == 0){
					cout<<"Congratulations!!! You have successfully logged in with your new password.";
					npswd.close();
					return 1;
				}
				else{
					cout<<"ACCESS DENIED";
				}
			}
		npswd.close();
		return 0;
		}
		
		int creatnewpass(){
		cout<<"Password: ";
		cin>>mynewpass;
		npswd.open("newpass.txt");
		npswd<<mynewpass;
		npswd.close();
			cout<<"\nLogin now? (y/n) ";
		cin>>loginyn;
			if(loginyn=="y"){
				cout<<"\n        [LOGIN PROCESS STARTING...]\n";
				checkusername();
				checknewpassword();
			}
		return 0;
		}
		private:
};

class newuser :  public user {
	private:
	string nusername;
	string rpass;
	string maiden;

	public:
		void askusername(){
			cout<<"\nUsername: ";
			cin>>nusername;
			rnusernames.open("newnames.txt");
			rnusernames<<nusername;
			rnusernames.close();
		}

		void askuserpass(){
			cout<<"Password: ";
			cin>>rpass;
			rnpass.open("newpass.txt");
			rnpass<<rpass;
			rnpass.close();
		}

		void askMMN(){
			cout<<"Mother's maiden name: ";
			cin>>maiden;
			rmaide.open("maidennames.txt");
			rmaide<<maiden;
			rmaide.close();
		}
};

int main() {
newuser r;
user u;   
olduser verify; 
int attempts, success;
cout<<"\nAre you a new user? (y/n) ";
cin>>u.choice1;
	if (u.choice1 == 'n'){
		for (attempts = 1; attempts < 4; attempts++){
			success = verify.checkusername();
				if (success == 1) { 
					break; 
				}
			}
		if (success == 0){
			cout<<"\nACCESS DENIED\n";
			cin.ignore();
			abort();
		}
		verify.checkpassword();
		cout<<"would you like to create a new password? (y/n) ";
		cin>>u.newpass;
			if (u.newpass=="y"){
				verify.creatnewpass();
			}
			else if (u.newpass=="n"){
				cin.ignore();
				abort();
			}
	}
	else if (u.choice1 == 'y'){
		r.askusername();
		r.askuserpass();
		r.askMMN();
		cin.ignore();
		abort();
	}
	else {
		cout<<"Error";
	}
return 0;
}
Regardless of how complicated the individual programs are, they will all eventually start with a main() function. This is the function that needs to be repurposed, renamed into "main2" and then called from the real main() function that you make. The real main function can be quite short, and just calls the other main functions, which do the heavy lifting.

If this is too much of a burden, then just go with what mbozzi said; there's nothing wrong with it, I was just suggesting an alternative.
Last edited on
Topic archived. No new replies allowed.