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:
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.
#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
usingnamespace 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()
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);
}
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?
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
elseif (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.
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.
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.