Making a 'Menu' for my math program

Pages: 12
1/2

Hello, I am very new to C/C++. I am trying to make a free 'Math suite' for the maths department that has taught me as a gift to them.


Here is the code: (It is in a desperate need of tidying up and is probably horrible)

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
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <cstdlib>


using namespace std;
int choice;
int onea;
int twoa;
int threea;
int foura;
//This part of the program is for the decision making (which part of the program the user will go into)

float x1plus;
float x2plus;
int one;
int two;
int three;
int dude;
int x;
int a;
int b;
int c;
double power;
double bob;
double dis;
double joemama;
float imag;
float imaganswer1;
float imaganswer2;
int bottom;
double answer1;
double answer2;

//These are the declarations for Ryan Parker's lovely Quadratic solver
float r;
float cir,area;
float theta, segcir, segarea,segr;
int choicecirc;
#define PI 3.14159
//These are for the circle calculator

 double s,u,v,a1,t;
 int equationChoice;
//This is for the SUVAT solver

int main()
{
    printf("This program assumes that you enter length in centimeters \n");
    printf("Enter: \n 1 for Gauss \n 2 for a Quadratic solver \n 3 for a circle calculator \n 4 for a SUVAT problem solver \n"); //Gaus is made by me but the Quadratic solver is made by Ryan Parker. It is a beautiful program. Thankyou very much for writing it.
    onea = 1;
    twoa = 2;
    cin >> choice;
    if (choice == 1)
   {
   //This program is named "Gauss" due to the famous tale of the Mathematician Gauss and his teacher.
    int n;
    int sum;
    printf("This program sums all integers less than or equal to n but greater than or equal to one. \n");
    printf("Enter your desired integer: \t"); //Don't worry, I am not that pretentious in real life.
    scanf("%d", &n );
    sum = (n*(n+1)/2); //Doing maths, like a gaus!
    printf("The sum of all preceeding integers from one to and including n is %d.\n",sum);
    printf("Press ENTER to terminate the program"); // Without this the program terminates instantly.
    fflush(stdout);
    getchar();
    getchar();
    return 0;
{
}
} else if (choice == 2)
{
one = 1; //This part of the program can be found at http://www.cplusplus.com/forum/beginner/59933/
two = 2; //I have corrected some of the spelling mistakes and tidied up some of the code.
cout << "Quadratic Equation Solver \n" << endl;
cout << "Type 1 and press enter if your problem is in standard form" << endl;
cout << "Type 2 if it is in (x+a)(x+b) form." << endl;
cin >> dude;
if (dude == 1)
{
for (x = 0; x <= 40; x++)
{
cout << "ax^2+bx+c" << endl;
cout << "Input the 'a' variable: " << endl;
cin >> a;
cout << "Input the 'b' variable: " << endl;
cin >> b;
cout << "Input the 'c' variable: " << endl;
cin >> c;
bottom = 2*a;
power = pow (b,2);
bob = 4*a*c;
dis = power - bob;
cout << "The discriminent is: " << dis << endl;
if (dis < 0)
{
imag = sqrt(dis);
imaganswer1 = (-b+imag);
imaganswer2 = (-b-imag);
float ximag1;
float ximag2;
ximag1 = imaganswer1/bottom;
ximag2 = imaganswer2/bottom;
printf("\n x= %f+%fi", -ximag1);
printf("\n x= %f+%fi", -ximag2);
cout << "\n" << endl;
cout << "\n" << endl;                            //Negative roots part of code here
}
else
{
joemama = sqrt(dis);
answer1 = (-b+joemama);
answer2 = (-b-joemama);
cout << "x=" << answer1 / bottom << endl;
cout << "x=" << answer2 / bottom << endl;
cout << "\n" << endl;
cout << "\n" << endl;
}
}
}
else if (dude == 2)
{
for (x = 0; x <= 40; x++)
{
cout << "imput the number after x1" << endl;
cin >> x1plus;
cout << "imput the number after x2" << endl;
cin >> x2plus;
cout << "in standard form: x^2+" << x1plus+x2plus << "x+" << x1plus*x2plus << endl;
}
}
else
{
cout << "come on, only 1 or 2";
}
return 0;
}
if (choice == 4)
{ 

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
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
// List the equations they can choose from:
    std::cout << "Equation :: [Required Values]:\n" << std::endl;
    std::cout << "\t1) s = ut + 1/2(at^2) \t\t\t:: [u,t,a]\n";
    std::cout << "\t2) u = (s - 1/2at^2)/t \t\t:: [s,a,t]\n";      //I did not w write this part of the program. I will credit the source and writer of the program.
    std::cout << "\t3) a = (2s - ut)/t^2 \t\t:: [s,u,t]\n\n";

    std::cout << "\t4) s = 1/2(u+v)t \t\t\t:: [u,v,t]" << std::endl;
    std::cout << "\t5) u = (2s - v)/t \t\t:: [s,v,t]\n";
    std::cout << "\t6) v = (2s - u)/t \t\t:: [s,u,t]\n";
    std::cout << "\t7) t = 2s / u+v \t\t:: [s,u,v]\n\n";

    std::cout << "\t8) v = u + at \t\t\t\t:: [u,a,t]" << std::endl;
    std::cout << "\t9) u = v - at \t\t\t:: [v,a,t]\n";
    std::cout << "\t10) a = (v - u)/t \t\t:: [v,u,t]\n";
    std::cout << "\t11) t = (v - u)/a \t\t:: [v,u,a]\n\n";

    std::cout << "\t12) v^2 = u^2 + 2as \t\t\t:: [u,a,s]" << std::endl;
    std::cout << "\t13) v = sqrt(u^2 + 2as) \t:: [u,a,s]\n";
    std::cout << "\t14) u = sqrt(v^2 - 2as) \t:: [v,a,s]\n";
    std::cout << "\t15) a = sqrt((v^2 - u^2)/2s) \t:: [v,u,s]\n";
    std::cout << "\t16) s = (v^2 - u^2)/2a \t\t:: [v,u,a]\n" << std::endl;

    // Find out what equation they would like to use:
    std::cout << "Please choose an equation(1-16): "; std::cin >> equationChoice;

    // Gather Variables:
    std::cout << "Please enter the values you have below(If unknown use 0):\n" << std::endl;
    std::cout << "S: "; std::cin >> s;
    std::cout << "U: "; std::cin >> u;
    std::cout << "V: "; std::cin >> v;
    std::cout << "A: "; std::cin >> a;
    std::cout << "T: "; std::cin >> t;
    std::cout << std::endl;

    switch(equationChoice)
    {
        case 1:
            std::cout << "S = " << (u*t) + ((a*pow(t,2))/2) << std::endl;
            break;

        case 2:
            std::cout << "U = " << ((s - ((a*pow(t,2))/2))/t) << std::endl;
            break;

        case 3:
            std::cout << "A = " << ((2*s-u*t)/pow(t,2)) << std::endl;
            break;

        case 4:
            std::cout << "S = " << (((u+v)*t)/2) << std::endl;
            break;

        case 5:
            std::cout << "U = " << (((2*s)-v)/t) << std::endl;
            break;

        case 6:
            std::cout << "V = " << (((2*s)-u)/t) << std::endl;
            break;

        case 7:
            std::cout << "T = " << ((2*s)/u+v) << std::endl;
            break;

        case 8:
            std::cout << "V = " << (u + (a*t)) << std::endl;
            break;

        case 9:
            std::cout << "U = " << (v - (a*t)) << std::endl;
            break;

        case 10:
            std::cout << "A = " << ((v-u)/t) << std::endl;
            break;

        case 11:
            std::cout << "T = " << ((v-u)/a) << std::endl;
            break;

        case 12:
            std::cout << "V^2 = " << ((pow(u,2))+(2*a*s)) << std::endl;
            break;

        case 13:
            std::cout << "V = " << sqrt((pow(u,2))+(2*a*s)) << std::endl;
            break;

        case 14:
            std::cout << "U = " << sqrt((pow(v,2))-(2*a*s)) << std::endl;
            break;

        case 15:
            std::cout << "A = " << (((pow(v,2))-(pow(u,2)))/(2*s)) << std::endl;
            break;

        case 16:
            std::cout << "S = " << (((pow(v,2))-(pow(u,2)))/(2*a)) << std::endl;
            break;

        default:
            std::cout << "It appears you have made an error, please try again.\n\n" << std::endl;
            main();
            break;
    }
    std::cin.sync();
    std::cin.get();
    return 0;
}
    if (choice == 3)
{
    printf("Do you want to find the area/circumference of a segment or a whole circle?\n");
    printf("\n Enter 1 for a whole circle \n Enter 2 for a segment \n");                      //Menu for the circle calculator
    cin >> choicecirc;
    if (choicecirc == 1)
{
    printf("Enter the radius of the circle: \t");
    scanf("%f",&r);
    cir = 2 * PI * r;
    area = PI * r * r;
    printf("The circumference of the circle is: \t %f cm \n",cir);
    printf("\n");
    printf("The area of the circle is: \t %f cm^2 \n ",area);
    printf("Press ENTER to terminate the program"); // Without this the program terminates instantly.
    fflush(stdout);
    getchar();
    getchar();
    return 0;
}
    else if (choicecirc == 2)
{
    printf("Enter the radius of the segment: \t");
    scanf("%f", &segr);                                              //This part of the circle calculator calculates segments
    printf("Enter the angle size in degrees of the segment: \t");
    scanf("%f", &theta);
    segcir = (theta/360) * 2 * PI * segr;
    segarea=(theta/360) * PI * segr * segr;
    printf("The circumference of the segment is: \t %f cm \n", segcir);
    printf("The area of the segment is: \t %f cm^2 \n", segarea);
    printf("Press ENTER to terminate the program"); // Without this the program terminates instantly.
    fflush(stdout);
    getchar();
    getchar();
    return 0;
}


// More code to come here



    if (choice > 4)
    printf("There aren't that many features in the program yet!\n ");
    printf("Press ENTER to terminate the program"); // Without this the program terminates instantly.
    fflush(stdout);
    getchar();
    getchar();
    return 0;
}

}

//Writen by Cameron who is a beginner at C/C++
//Started development on the 12Th April 2012 


I would like to make a "Main menu" so that when you complete a calculation the program does not terminate but goes back to the beginning.

I have some knowledge of .bat where you can make something like
:Menu
//Code here
:1
//code here
Goto Menu

How can I do something like that in C++
Massive thanks, Cameron

2/2
create a switch statement

and have the user pick between the switch to go to your desired part of the code

and put all of this inside a while loop and make it so it asks to continue with another calculation at the end of the operation by saying yes or no, and have a check for something like

while(go_again != 'n')
Thanks for replying, I do value the time you have taken out of your day to reply to my question.

Being a beginner, I would not know how to do such a thing, would it be okay if you could give a more explicit explanation of your suggested method?

Thanks again
May I ask another question?

I am using the Code blocks IDE and I am stuck on a Windows computer for now.

I would like to be able to have a custom icon for the .exe file rather than the generic blue bordered window.

How can I compile the program to include an icon?
1
2
3
4
5
6
7
8
char go_again = 'y';

while(go_again != 'n'){

//code here
cout << "Would you like to go again? y or no";
cin >> go_again;
}
Last edited on
Thanks so much, you are awesome.

The program still seems to be terminating regardless of the input.

here is a portion of the modified 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



.....
char = go_again = 'y';
......
int main()
{
while(go_again != 'n')
{
    printf("This program assumes that you enter length in centimeters \n");
    printf("This program assumes Pi to be equal to 3.14159 \n");
    printf("\n");
    printf("Enter: \n 1 to find the sum of all preceeding integers from 1 to n  \n 2 for a Quadratic solver \n 3 for a circle calculator \n 4 for a SUVAT problem solver \n 5 to find the nth term of the fibonacci sequence \n"); //Gaus is made by me but the Quadratic solver is made by Ryan Parker. It is a beautiful program. Thankyou very much for writing it.
    onea = 1;
    twoa = 2;
    cin >> choice;
    if (choice == 1)
   {
   //This program is named "Gauss" due to the famous tale of the Mathematician Gauss and his teacher.
    int n;
    int sum;
    printf("This program sums all integers less than or equal to n but greater than or equal to one. \n");
    printf("Enter your desired integer: \t"); //Don't worry, I am not that pretentious in real life.
    scanf("%d", &n );
    sum = (n*(n+1)/2); //Doing maths, like a gaus!
    printf("The sum of all preceeding integers from one to and including n is %d.\n",sum);
    cout << "Would you like to go again? y for yes or n for no";
    cin >> go_again;
    printf("\n");
    printf("Press ENTER to terminate the program"); // Without this the program terminates instantly.
    fflush(stdout);
    getchar();
    getchar();
    return 0;
{
}
.....




Thanks a bunch for the information about the icons, I'll try that in a second.

Many regards, Cameron
you have char = go_again

remove the = lol
That was a typo :P

The actual line was char go_again = 'y';

Would have been a funny mistake if it was that though
put char go_again in main
but outside the while statement
Last edited on
I tried it to no avail, I am probably doing something wrong. In the meantime I have tried to find a solution by using a different method which I'll share in a moment. :)

Thanks again
I couldn't figure out how to do it :(

Oonej, thanks a lot for your suggestion however it didn't work.

If anyone else has any suggestions, whatever it is, please let me know :)
Also does anyone know how to write a program to solve negative square roots in C++ which outputs complex solutions?

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
#include <iostream>
#include <cmath>
#include <complex>
#include <stdio.h>
#include <cstdlib>
#include <ctime> // The seemingly unneeded libraries are for other parts of the larger program 


float x1plusb;
float x2plusd;
int one;
int two;
int three;
int quadchoice;
int x;
int a;
int b;
int c;
double bsquared;
double fourac;
double dis;
double rootdis;
double imagpart;
float realpart;
float imaganswer1;           //Imagianary/complex soloutions do not calculate correctly yet.
float imaganswer2;
int bottom;
double answer1;
double answer2;
float x1plusa;
float x2plusc;
int main()
using namespace std;
{
 //The original code can be found at http://www.cplusplus.com/forum/beginner/59933/
 //I have corrected some of the spelling mistakes, tidied up some of the code, tried support for complex solutions, added support for (ax+b)(cx+d).
cout << "Quadratic Equation Solver \n" << endl;
cout << "Type 1 and press enter if your problem is in standard form" << endl;
cout << "Type 2 to expand brackets in (ax+b)(cx+d) form." << endl;
cin >> quadchoice;
if (quadchoice == 1)
{
for (x = 0; x <= 40; x++)
{
cout << "ax^2+bx+c" << endl;
cout << "Input the 'a' variable: " << endl;
cin >> a;
cout << "Input the 'b' variable: " << endl;
cin >> b;
cout << "Input the 'c' variable: " << endl;
cin >> c;
bottom = 2*a;
bsquared = pow (b,2);
fourac = 4*a*c;
dis = bsquared - fourac;
cout << "The discriminent is: " << dis << endl;
if (dis < 0)
{
 
imagpart = sqrt(dis)/bottom;
realpart = -b/bottom;                                                    //I tied to add support for negative roots
cout << "x=" << realpart <<"+" <<imagpart <<endl;
cout << "x=" << realpart <<"-" <<imagpart <<endl;
cout << "\n" << endl;
cout << "\n" << endl; 
                //Negative roots part of code here
}
else
{
rootdis = sqrt(dis);
answer1 = (-b+rootdis);
answer2 = (-b-rootdis);
cout << "x=" << answer1 / bottom << endl;
cout << "x=" << answer2 / bottom << endl;
cout << "\n" << endl;
cout << "\n" << endl;
}
}
}
else if (quadchoice == 2)
{
for (x = 0; x <= 40; x++)
{
cout << "Input the a variable:" << endl;
cin >> x1plusa;
cout << "Input the b variable:" << endl;
cin >> x1plusb;
cout << "input the c variable:" << endl;     //I added (ax+b)(cx+d) support
cin >> x2plusc;
cout << "Input the d variable:" << endl;
cin >> x2plusd;
cout << "The brackets expanded:" << x1plusa*x2plusc << "x^2+" << (x1plusb*x2plusc)+(x1plusa*x2plusd) << "x+" << x1plusb*x2plusd << endl;
}
}
else
{
cout << "Only 1 or 2 can be chosen!";
}
return 0;
}


For example, if I input the a, b and c constants to be 1, 2 and 4 respectively then I get x=-1+Nan
x= -1-Nan
instead of the proper solution which is:
x=-1+1.7321 i
x= -1-1.7321 i


Please help me fix this :( I am starting to feel desperate now.
Last edited on
Bump
sqrt of a negative number is not defined so you get NaN a value.
How do I define it?
I have changed the imaginary part of the code to this:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

.....

if (dis < 0)
{
    complex<double> imagpart;
    imagpart = sqrt(dis)/bottom;
    realpart = -b/bottom;                                                    //I have tried to add support for negative roots
    cout << "x=" << realpart <<"+" <<imagpart.imag() << "i"<<endl;
    cout << "x=" << realpart <<"-" <<imagpart.imag() <<  "i" <<endl;
    cout << "\n" << endl;
    cout << "\n" << endl;                                                   //Negative roots part of code here
}

.......



The program now outputs x= -1+0i
Last edited on
Update: I have made myself a menu so that part of my question is solved however the complex roots of a quadratic still don't calculate correctly
Pages: 12