My first program

Hello, I am very new to C/C++ and any programming language and I am attempting to write a program. The code I am going to post is part of a larger program which is intended to be a free 'Math suite' for my Maths department as a leaving present for putting up with me- it's going to feature a quadratic equation solver, a simultaneous equation solver, a trig question solver and a circle/segment area and circumference solver.

Here is my probably horrific code for the circle solver:


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <cstdlib>

float r;
float pi;
int onec;
int twoc;
int choicecirc;
#define PI = 3.14159
using namespace std;


int main()
{
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");
cin >> choicecirc;
if (choicecirc == 1);

{

float cir,area;
float pi = 3.14159;
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);

if (choicecirc == 2);

float theta, segcir, segarea,segr;
float pi0 = 3.14159;
printf("Enter the radius of the segment: \t");
scanf("%f", &segr);
printf("Enter the angle size in degrees of the segment: \t");
scanf("%f", &theta);
segcir=(theta/360)*2*pi0*r;
segarea=(theta/360)*pi0*r*r;
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);
}
}


I am trying to program it so it if you press '1' the program solves for a complete circle but if you press '2' it solves for a segment of a circle.

When you enter any integer it sequentially goes through the program instead of just using the part of the program I want to use. For example, I want to find the area and circumference of a segment with a radius of 30 cm where theta is 45 degrees. The program makes me solve a circle first and then lets me solve for a segment. I get an output like:

Do you want to find the area/circumference of a segment or a whole circle?

Enter 1 for a whole circle
Enter 2 for a segment
2
Enter the radius of the circle: 20
The circumference of the circle is: 125.663605 cm

The area of the circle is: 1256.635986 cm^2
Enter the radius of the segment: 35
Enter the angle size in degrees of the segment: 45
The circumference of the segment is: 15.707951 cm
The area of the segment is: 157.079498 cm^2

Process returned 0 (0x0) execution time : 11.031 s
Press any key to continue.





Please help me, anything will be highly appreciated.
Just a few syntax issues: you seemed to be missing a brace or two. Since you #define PI as a global, you don't really need a new value for float pi; in each if/else if block.

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

float r;
float pi;          //You don't need this, because you #define PI a few lines below
int onec;
int twoc;
int choicecirc;
#define PI = 3.14159
using namespace std;


int main()
{
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");
cin >> choicecirc;
if (choicecirc == 1);   // there should be no ';' after the if statement
                                // and the code you want to execute should be
                                // enclosed by braces

{

float cir,area;
float pi = 3.14159;  // you can use PI instead
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);
}     //this closing brace concludes the code segment of the first if statement
if (choicecirc == 2);      //no ';' , also should be 'else if (choicecirc == 2)'
{     //opening brace for the second if block
float theta, segcir, segarea,segr;
float pi0 = 3.14159;
printf("Enter the radius of the segment: \t");
scanf("%f", &segr);
printf("Enter the angle size in degrees of the segment: \t");
scanf("%f", &theta);
segcir=(theta/360)*2*pi0*r;
segarea=(theta/360)*pi0*r*r;
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);
}        //closing brace for the else if code segment
}  // end main() 
Last edited on
I see two problems in your program.
Problem number 1 has to do with your IF. Your 1st if is fine you need to change your second if to else.. like this:

else
(choicecirc == 2);

The second problem has to do with your curly braces. I would do it like this

if (choicecirc == 1);

{

float cir,area;
float pi = 3.14159;
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);
}

else
{

float theta, segcir, segarea,segr;
float pi0 = 3.14159;
printf("Enter the radius of the segment: \t");
scanf("%f", &segr);
printf("Enter the angle size in degrees of the segment: \t");
scanf("%f", &theta);
segcir=(theta/360)*2*pi0*r;
segarea=(theta/360)*pi0*r*r;
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);
}
Thank you both very much for taking the time out of your days to reply to me, I massively appreciate it.

Both replies have helped me learn for the future and have helped me plan about tidying up my code which I shall do right away.

After following both answers I have ran into another bug. The program now behaves correctly (When 1 is pressed it goes to the circle calculator but when 2 is pressed it goes to the segment calculator) however when the segment calculator is used the program always gives me two answers of 0.

For example:



Do you want to find the area/circumference of a segment or a whole circle?

Enter 1 for a whole circle
Enter 2 for a segment
2
Enter the radius of the segment: 30
Enter the angle size in degrees of the segment: 45
The circumference of the segment is: 0.000000 cm
The area of the segment is: 0.000000 cm^2
Press ENTER to terminate the program

Process returned 0 (0x0) execution time : 22.234 s
Press any key to continue.


The code is: (Don't worry, I shall tidy it up and use the defined constant rather than floating variables)


#(all the includes and stuff here)

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");
cin >> choicecirc;
if (choicecirc == 1)
{
float cir,area;
float pi = 3.14159;
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);
}
else if (choicecirc == 2)
{
float theta, segcir, segarea,segr;
float pi0 = 3.14159;
printf("Enter the radius of the segment: \t");
scanf("%f", &segr);
printf("Enter the angle size in degrees of the segment: \t");
scanf("%f", &theta);
segcir=(theta/360)*2*pi0*r;
segarea=(theta/360)*pi0*r*r;
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);
}


What have I done wrong?

Again, thanks very much!
On the topic, instead of creating a new thread, I would like to ask another question.

I have found a lovely piece of code which has been created by somebody called Ryan Parker which can solve quadratics with positive discriminants. I would like to adapt this code to allow it to solve quadratics with imaginary/complex solutions- is there a way to solve negative square roots in C++ and if so, how can this be done?

Thanks again
Sorry it took so long to reply; I'm not as familiar with C as I'd like to be...

Here's where the problem lies:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
else if (choicecirc == 2)
{
float theta, segcir, segarea,segr;
float pi0 = 3.14159;
printf("Enter the radius of the segment: \t");
scanf("%f", &segr);   //notice the variable name you're reading the value into
printf("Enter the angle size in degrees of the segment: \t");
scanf("%f", &theta);
segcir=(theta/360)*2*pi0*r; //then look what variable you're using the calculations
segarea=(theta/360)*pi0*r*r;
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);
}


The variable 'r' has not been initialized (it's not even supposed to be used) in this block. So when you try to use it in the calculation, it's given the default value of 0, which is causing the rest of the code to calculate the wrong results.
Last edited on
I feel embarrassed now. :P How could I have been so stupid?

Thanks a bunch; you are brilliant
I think that everyone has has moments when they completely overlook something that later turns out to be obvious. Just glad I could help.
Right, I have adapted some code for the quadratic solver here it is: (Again, I need to tidy it up- things are unconventionally done)

I must stress that the majority of this code was not made by me but made by a person named Ryan Parker. The original code can be found at: http://www.cplusplus.com/forum/beginner/59933/

I have merely corrected some spelling mistakes and failed at adding support for 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
#include <iostream>
#include <math.h>

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;
double imag;
double imaganswer1;
double imaganswer2;
int bottom;
double answer1;
double answer2;


{
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);
printf("\n:x= %f+%fi",  imaganswer1/bottom);// printf("\nRoot 1 : %f+%fi",((-b)/(2*a)),d);                                              //printf("\nRoot 2 : %f-%fi",((-b)/(2*a)),d);}
printf("\n:x= %f+%fi",  imaganswer2/bottom);
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;
}
Last edited on
Bump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

....
if (dis < 0)
{
imag = sqrt(dis);
imaganswer1 = (-b+imag);
imaganswer2 = (-b-imag);
double ximag1;
double 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
}
... 



When I run the program and the discriminant is less than 0 I get the following output in the console:
x= -1.IND00+0.000000i
x= -1.IND00+0.000000i
--
Apparently this is a problem due to dividing by 0, is there a way I can fix this?

Edit: I should also add that imag, imaganswer1, imaganswer2, ximag1 and ximag2 are doubles. I have tried them as floats but to no avail.
Last edited on
Topic archived. No new replies allowed.