Asking about project

I'm using Dev-C++, and i want to ask. I want to make a project with multiple C++ files. The project start with a Main Menu.cpp which create a list of things this project can do. And by enter (eg: number 1) will open file B1.cpp. How to do this? (i've created the Main Menu and B1, i just don't know how to open another .cpp file).
i just don't know how to open another .cpp file

You don't "open" .cpp files. You call functions located in .cpp files.

You want to have a B1.h file that contains declarations for functions in your B1.cpp file.
Include B1.h in your mainmenu.cpp file, so when you compile mainmenu.cpp, the compiler will know the calling sequence for any functions in B1.cpp.
And... may i ask how to do so? Sorry, i'm a very newbie...
Post what you have so far.
Last edited on
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
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main () {
	float x, y, z, a, b, min, max;
	int choice;
	while (1)
	{
		system("cls");
		printf ("\nBai Tap 3: (Made by Kuro Neko)\n");
		printf ("\nBan muon xem cau a hay cau b? (Nhap so de chon cau)\n");
		printf ("Luu y: Neu ban viet nhieu hon 1 so, he thong se uu tien so dung \ntruoc dung voi yeu cau\n");
		printf ("1. a\n");
		printf ("2. b\n");
		printf ("3. thoat\n");
		printf ("Lua chon cua ban la?\n");
		fflush(stdin);
		scanf ("%d", &choice);
		while (choice>3||choice<0)
		{
			printf ("\nBan chi duoc chon 1, 2 hoac 3 thoi! Xin hay chon lai!\n");
			scanf ("%d", &choice);
		}
		if (choice==1)
		{
			Again1:
			system("cls");
			printf ("\nBai Tap 3a: (Made by Kuro Neko)\n");
			printf ("\nDe: Cho 3 so x, y, z. Tim Max(x+y+z, x*y*z).\n");
			printf ("Nhap 3 so x y z:\n");
			fflush(stdin);
			scanf ("%f%f%f",&x,&y,&z);
			a=x+y+z;
			b=x*y*z;
			printf ("x+y+z=%.2f\n",a);
			printf ("x*y*z=%.2f\n",b);
			if (a>b) 
			{
				max=a;
				printf ("\nVay so lon hon la %.2f\n",max);
			}
			else if (a<b)
			{
				max=b;
				printf ("\nVay so lon hon la %.2f\n",max);
			}
			else printf ("\n2 so do bang nhau\n");
			printf ("\nBan muon lam gi tiep theo?\n");
			printf ("\n1. Tro ve trang chinh\n");
			printf ("2. Thu bai 3a lan nua\n");
			printf ("3. Thoat\n");
			scanf ("%d", &choice);
			while (choice>3||choice<0)
			{
				printf ("\nBan chi duoc chon 1, 2 hoac 3 thoi! Xin hay chon lai!\n");
				scanf ("%d", &choice);
			}
			if (choice==1) continue;
			else if (choice==2) goto Again1;
			else return 0;
		}
		if (choice==2)
		{
			Again2:
			system("cls");
			printf ("\nBai Tap 3b: (Made by Kuro Neko)\n");
			printf ("\nDe: Cho 3 so x, y, z. Tim Min^2((x+y+z)/2,x*y*z)+1.\n");
			printf ("Nhap 3 so x y z:\n");
			fflush(stdin);
			scanf ("%f%f%f",&x,&y,&z);
			a=(x+y+z)/2;
			b=x*y*z;
			printf ("(x+y+z)/2=%.2f\n",a);
			printf ("x*y*z=%.2f\n",b);
			if (a>b) 
			{
				min=b;
				printf ("\nVay ket qua cua phep tinh la: %.2f\n",min*min+1);
			}
			else if (a<b)
			{
				min=a;
				printf ("\nVay ket qua cua phep tinh la: %.2f\n",min*min+1);
			}
			else printf ("\n2 so do bang nhau\n");
			printf ("\nBan muon lam gi tiep theo?\n");
			printf ("\n1. Tro ve trang chinh\n");
			printf ("2. Thu bai 3b lan nua\n");
			printf ("3. Thoat\n");
			scanf ("%d", &choice);
			while (choice>3||choice<0)
			{
				printf ("\nBan chi duoc chon 1, 2 hoac 3 thoi! Xin hay chon lai!\n");
				scanf ("%d", &choice);
			}
			if (choice==1) continue;
			else if (choice==2) goto Again2;
			else return 0;
		}
		if (choice==3) return 0;
	}
}


This is what i have in my B1.cpp. In short, it's a simple math solving. My teacher asks me to create programs to solve lots of math, but i want to compile all of them in a project instead of just create lots of source file. And from the Main Menu.cpp, i'll use "case" to choose from all source file. That's the ideal.
p/s: don't mind my own country's language.
Last edited on
I was hoping you would post mainmenu.cpp also.

It's not clear if you you intend to call what you posted (you can't call main()).
It's also not clear if mainmenu.cpp also has a main() function.
Since lines 13-18 appear to be prompting for a choice, it's also not clear what purpose mainmenu.cpp serves.
Ah, yeah, my bad. My mainmenu.cpp also have int main (), it's the begin of everything anyway?
I have 30 different math exercise, and this B1 has 2 different one in its own. So yeah, i can make it all in just 1 source file, but it's too much, so i want to make a project to seperate it to 30 source file but can still use it all.
p/s: i can't really post my mainmenu.cpp since idk how to write it to link it with B1.
Last edited on
Still need you to post mainmenu,cpp.

Have you learned how to make functions?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main () {
	int choice;
	system("cls");
	printf ("\nPlease choose what you want to see:\n");
	printf ("\n1. B1\n");
	printf ("\n2. B2\n");
	printf ("\n3. B3\n");
	printf ("\n4. B4\n");
	printf ("\n5. B5\n");
	scanf ("%d", &choice);
	switch (choice)
	{
		case '1': //this is where i want to open B1.cpp
		case '2': //of course, this one is B2.cpp
		case '3': //of course, this one is B3.cpp
		case '4': //of course, this one is B4.cpp
		case '5': ; //of course, this one is B5.cpp
	}
	return 0;
}

How about this? This is my ideal of main menu.cpp
Last edited on
Okay. So now I know what I'm dealing with.

You want main to look something like this:
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    switch (choice)
    {
    case '1':  function_b1();
                   break; 
    case '2':  function_b2(); 
                  break;
    case '3':  function_b3 (); 
                  break;
    case '4':  function_b4 (); 
                  break;
    case '5':  function_b5 (); 
                  break;
    default:  cout << "Not a valid choice" << endl;
    }


main() needs to know how to call each of those functions. Normally the declarations for functions b1-b5 would be placed in respective header files. However, I would suggest keeping this simple and create a single header file:
1
2
3
4
5
6
//  header.h
void function_b1 ();
void function_b2 ();
void function_b3 ();
void function_b4 ();
void function_b5 ();

Add an include in mainmenu.cpp for header.h

Now, you want to break up B1.cpp into separate functions.
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
//  B1.cpp
#include "header.h"

//  Other includes
void function_b1 ()
		{	system("cls");
			printf ("\nBai Tap 3a: (Made by Kuro Neko)\n");
			printf ("\nDe: Cho 3 so x, y, z. Tim Max(x+y+z, x*y*z).\n");
			printf ("Nhap 3 so x y z:\n");
			fflush(stdin);
			scanf ("%f%f%f",&x,&y,&z);
			a=x+y+z;
			b=x*y*z;
			printf ("x+y+z=%.2f\n",a);
			printf ("x*y*z=%.2f\n",b);
			if (a>b) 
			{
				max=a;
				printf ("\nVay so lon hon la %.2f\n",max);
			}
			else if (a<b)
			{
				max=b;
				printf ("\nVay so lon hon la %.2f\n",max);
			}
		}


Do the same for function_b2, etc.
Last edited on
May i ask, what is "void"? not "int"?
void says the function does not return anything.
Ah, btw, then how to return to Main Menu.cpp from a function?
Simply fall through the bottom of the function, or if you want to return earlier, return; Note no value if function is type void.
Strange... Am i wrong or something?
This is my main menu now. But when i input 1, it's show a "not a valid choice" and return instantly.
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
#include "header.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main () {
	int choice;
	system("cls");
	printf ("\nPlease choose what you want to see:\n");
	printf ("\n1. B1\n");
	printf ("\n2. B2\n");
	printf ("\n3. B3\n");
	printf ("\n4. B4\n");
	printf ("\n5. B5\n");
	scanf ("%d", &choice);
	switch (choice)
	{
		case '1': B1 (); //this is where i want to open B1.cpp
		case '2': //of course, this one is B2.cpp
		case '3': //of course, this one is B3.cpp
		case '4': //of course, this one is B4.cpp
		case '5': ; //of course, this one is B5.cpp
		default: printf("not a valid choice");
		return 0;
	}
}


This is the header.h:
void B1 ();

And this is B1:
1
2
3
4
5
6
7
#include "header.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void B1 () {
	float x, y, z, a, b, min, max;
	int choice;
Last edited on
You've forgotten the break statements at the end of each case. So all the cases are dropping through to the default case.
Changed into this and still don't work:
1
2
3
4
5
6
7
8
9
	{
		case '1': {B1 (); break;} //this is where i want to open B1.cpp
		case '2': //of course, this one is B2.cpp
		case '3': //of course, this one is B3.cpp
		case '4': //of course, this one is B4.cpp
		case '5': ; //of course, this one is B5.cpp
		default: printf("not a valid choice");
		return 0;
	}


Ah, nvm. I fixed this just by changed '1' -> 1. Btw, then how to return to main menu.cpp? return; will end it immediately, not return to main menu.cpp. I'm talking about, after call function b1 from mainmenu.cpp, how to recall mainmenu.cpp from function b1?
Last edited on
then how to return to main menu.cpp?

When B1() returns, you're back in mainmenu.cpp.
What you want is a loop in main().
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
#include "header.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main () {
	int choice;
	
	while (true)
	{   system("cls");
	    printf ("\nPlease choose what you want to see:\n");
	    printf ("\n1. B1\n");
	    printf ("\n2. B2\n");
	    printf ("\n3. B3\n");
	    printf ("\n4. B4\n");
	    printf ("\n5. B5\n");
	    scanf ("%d", &choice);
	    switch (choice)
	    {
		case '1': B1 (); break; 
		case '2': //of course, this one is B2.cpp
		case '3': //of course, this one is B3.cpp
		case '4': //of course, this one is B4.cpp
		case '5': ; //of course, this one is B5.cpp
		default: printf("not a valid choice");
		         return 0;
	    }  // end switch
	} // end while
}

Don't forget an exit case so you can exit the program.

Last edited on
Topic archived. No new replies allowed.