dev c++ need help

hi, i have created a maths app, int choice is the menu, i was wondering after say the user clicks number 1 and has done the area of a circle how can i get it to ask the user if they want to go back to the meanu and how do i get it to go back to the menu?

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
#include <iostream>

using namespace std;

int main()
{
    int choice;
    cout<< "Maths Helper";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Area of a circle ";
    cout<< "\n 2 - Circumference of a circle ";
    cout<< "\n 3 - Factorial of a number ";
    cout<< "\n 4 - Hypotenuse of a triangle ";
    cout<< "\n 5 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return:";
    cin>>choice;
    
    switch (choice)
{
    case 1:
    cout<< "\n\nArea of a circle ";
 
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
 

{
float area, radius;

// user radius input
cout<<"\n\nEnter a radius of a circle: ";
cin>>radius;
// circle area = PI*radius*radius
area = PI * radius * radius;
 
// circle area
cout<<"\nCircle area = "<<area<<endl;




    break;
    case 2:
    cout<< "Circumference of a circle ";
    break;
    case 3:
    cout<< "Factorial of a number ";
    break;
    case 4:
    cout<< "Hypotenuse of a triangle ";
    break; 
    case 5:
    cout<< "Exit"; 
    break;
    default:
    cout<< "\nNot a valid choice.";
    cout<< "\nchoose again";
    cin>> choice;
    break;
}
}
return 0;
}



Last edited on
closed account (9wqjE3v7)
Hi,

Have you looked at subroutines (functions) yet? If so, you can have the menu as a function, and inside, you request input from the user to choose an operation. For instance, if they choose 2, the return value of the function would be 2.

For instance, you could have something along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int menu() {
...
int choice;
cout << "Please choose one of the following:\n";
...
cin >> choice // The user inputs 2

return choice; // return the value 2
}

//You can then do your case statements, though instead check the return value of the function:

int r_choice = menu() // r_choice would be 2, since the user choice was 2

*EDIT

int main() {

switch (r_choice) {
...
}
return 0;
}



After this you could request user input as to whether they wish to return to the main menu. If the input is 'y', call the menu function again, though if the input is 'n', return from main() and exit the process.

A less flexible, though simple alternative would be to utilise a while or do-while loop, with the loop only being broken if the user wishes to exit. Hope this helps!
Last edited on
hi

thanks for your help, is this what it should look like? and if so it doesnt like line 24 any ideas?

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
#include <iostream>

using namespace std;

int menu()
{
    int choice;
    cout<< "Maths Helper";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Area of a circle ";
    cout<< "\n 2 - Circumference of a circle ";
    cout<< "\n 3 - Factorial of a number ";
    cout<< "\n 4 - Hypotenuse of a triangle ";
    cout<< "\n 5 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return:";
    cin>>choice;
    return choice;
}
int r_choice = menu()

*Edit
int main()
{   
    switch (r_choice)

    case 1:
    cout<< "\n\nArea of a circle ";
 
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
 

{
float area, radius;

// user radius input
cout<<"\n\nEnter a radius of a circle: ";
cin>>radius;
// circle area = PI*radius*radius
area = PI * radius * radius;
 
// circle area
cout<<"\nCircle area = "<<area<<endl;




    break;
    case 2:
    cout<< "Circumference of a circle ";
    break;
    case 3:
    cout<< "Factorial of a number ";
    break;
    case 4:
    cout<< "Hypotenuse of a triangle ";
    break; 
    case 5:
    cout<< "Exit"; 
    break;
    default:
    cout<< "\nNot a valid choice.";
    cout<< "\nchoose again";
    cin>> choice;
    break;
}
}
return 0;
}
closed account (9wqjE3v7)
Remove the '*edit', that is the problem. Sorry, I did not mean for that to be part of the code, I was trying to state where I edited my post. Also line 21 should be in main(). Please note that I did NOT compile the code. It was merely a demonstration of what you could implement so a few changes would obviously need to be made.
Last edited on
haha sorry, i have removed it and it still doesnt like it, ive tried a few things but nothing?
closed account (9wqjE3v7)
Try to look at where you have placed blocks. Also, you need to call menu() again later on, otherwise your program will simply terminate.
this may be a stupid question but whats blocks?
i think he is referring to the "blocks" of code -> codes inside { and } or possibly the "brackets" -> { }
ive tried a few thing but nothing this is what it is saying
24 C:\Users\ben\Console App\test - edit.cpp expected `,' or `;' before "int"
73 C:\Users\ben\Console App\test - edit.cpp expected declaration before '}' token


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
#include <iostream>

using namespace std;

int menu()
{
    int choice;
    cout<< "Maths Helper";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Area of a circle ";
    cout<< "\n 2 - Circumference of a circle ";
    cout<< "\n 3 - Factorial of a number ";
    cout<< "\n 4 - Hypotenuse of a triangle ";
    cout<< "\n 5 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return:";
    cin>>choice;
    return choice;
}
int r_choice = menu()


int main{
 
    switch (r_choice){


    case 1:
    cout<< "\n\nArea of a circle ";
 
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
 
}

float area, radius;

// user radius input
cout<<"\n\nEnter a radius of a circle: ";
cin>>radius;
// circle area = PI*radius*radius
area = PI * radius * radius;
 
// circle area
cout<<"\nCircle area = "<<area<<endl;




    break;
    case 2:
    cout<< "Circumference of a circle ";
    break;
    case 3:
    cout<< "Factorial of a number ";
    break;
    case 4:
    cout<< "Hypotenuse of a triangle ";
    break; 
    case 5:
    cout<< "Exit"; 
    break;
    default:
    cout<< "\nNot a valid choice.";
    cout<< "\nchoose again";
    cin>> choice;
    break;
}

}
}
return 0;
}
closed account (9wqjE3v7)
Read those compile errors, you are missing a ';' on line 21 after your initialization of r_choice. Why do you have a curly brace '}' at line 27?
Last edited on
try this instead, i've just edited indentations and some misplaced braces and function

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
#include <iostream>

using namespace std;

int menu()
{
    int choice;
    cout<< "Maths Helper";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Area of a circle ";
    cout<< "\n 2 - Circumference of a circle ";
    cout<< "\n 3 - Factorial of a number ";
    cout<< "\n 4 - Hypotenuse of a triangle ";
    cout<< "\n 5 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return:";
    cin>>choice;
    return choice;
}

int main
{
    int r_choice = menu()
    
    switch (r_choice)
    {
        case 1:
            cout<< "\n\nArea of a circle ";
     
            // define identifier PI with a constant
            #define PI 3.14159
            // define identifier TWO with a constant
            #define TWO 2.0

            float area, radius;

            // user radius input
            cout<<"\n\nEnter a radius of a circle: ";
            cin>>radius;
            
            // circle area = PI*radius*radius
            area = PI * radius * radius;
     
            // circle area
            cout<<"\nCircle area = "<<area<<endl;
        break;
        case 2:
            cout<< "Circumference of a circle ";
        break;
        case 3:
            cout<< "Factorial of a number ";
        break;
        case 4:
            cout<< "Hypotenuse of a triangle ";
        break; 
        case 5:
            cout<< "Exit"; 
        break;
        default:
            cout<< "\nNot a valid choice.";
            cout<< "\nchoose again";
            cin>> choice;
    } // the closing brace for switch


    return 0;

} // the closing brace for main() 
Last edited on
have tried that thanks but in line 23 its says 23 invalid function declaration can some one help?
There were a couple of small problems with the previous code. Line 22 should read int main() rather than just int main, and at line 63 choice has not been defined.

But also I think there should be a while or do-while loop around the code in main(), in order to go back to the menu each time. And it's better to use const rather than #define for values such as PI.

Perhaps like this:
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
#include <iostream>

using namespace std;

char menu()
{
    char choice;
    cout<< "\nMaths Helper";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Area of a circle ";
    cout<< "\n 2 - Circumference of a circle ";
    cout<< "\n 3 - Factorial of a number ";
    cout<< "\n 4 - Hypotenuse of a triangle ";
    cout<< "\n 5 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return: ";
    cin >> choice;
    return choice;
}

int main()
{
    const double PI = 3.1415926535897932385;
    double radius, area;
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nArea of a circle "
                     << "\n\nEnter the radius: ";
                cin >> radius;
                area = PI * radius * radius;
                cout << "\nCircle area = " << area << endl;
                break;
                
            case '2':
                cout<< "Circumference of a circle ";
                break;
            case '3':
                cout<< "Factorial of a number ";
                break;
            case '4':
                cout<< "Hypotenuse of a triangle ";
                break; 
            case '5':
                cout<< "Exit"; 
                break;
            default:
                cout<< "\nNot a valid choice.";
        }
    } while (choice != '5');

    return 0;
} 
Last edited on
Topic archived. No new replies allowed.