And one more suggestion: Instead of defining Pi, use the already available function in math.h: M_PI.
And one more suggestion: Instead of defining Pi, use the already available function in math.h: M_PI.
Useless postcount increasing posts deleted
Click it, read it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You have to include the math library
@fried A way of posting at once and answering more than one quotes can be done like this :Code:#include <math.h>
On the botom of every post, on the right corner there is something like a border with double quotes as its contents.If you click it a "tick" will appear on the corresponding post.You can do that for multiple posts.Then go to the last you "ticked" and click on Reply With Quote, as would do normally .Then the forum automatically will create all the quotes you ticked in one answer.You can see that every "ticked"-post is appearing on your answer-post form wrapped with QUOTE tags
sure thingI removed #define PI 3.14159 and changed my PI variables to M_PI and it didn't allow it as a constant,Code:#include <stdio.h> #include <math.h> #define PI 3.14159 int main(void) { double sum, first_Num, sec_Num, third_Num, diff, product, quotient, srt, area, rad1, rad2, base, height; int choice, selection; do { printf("\nChoose what calculation to perform\n"); printf("\t1 - Addition\n"); printf("\t2 - Subtration\n"); printf("\t3 - Multiplication\n"); printf("\t4 - Division\n"); printf("\t5 - Square Root\n"); printf("\t6 - Exponents\n"); printf("\t7 - Absolute Value\n"); printf("\t8 - Area of Shapes\n"); printf("(Enter a zero to exit) \n"); printf("\nMake a selection: "); scanf("%d", &choice); switch (choice) { case 1: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); sum = first_Num + sec_Num; printf("\nThe sum of %f and %f is %.4f\n\n", first_Num, sec_Num, sum); break; } case 2: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); diff = first_Num - sec_Num; printf("\nThe difference of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, diff); break; } case 3: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); product = first_Num * sec_Num; printf("\nThe product of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, product); break; } case 4: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); quotient = first_Num / sec_Num; printf("\nThe quotient of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, quotient); break; } case 5: { printf("\nEnter number: "); scanf("%lf", &srt); { while (srt <= 0) { printf("ERROR!! Value must be a positive number\n\n"); printf("Enter number: "); scanf("%lf", &srt); } printf("\nThe square root of %.4f is %.4f\n", srt, sqrt(srt)); } break; } case 6: { printf("\nEnter base number: "); scanf("%lf", &first_Num); printf("Enter exponent: "); scanf("%lf", &sec_Num); printf("\n%.4f to the power of %.4f is %.4f\n\n", first_Num, sec_Num, pow(first_Num, sec_Num)); break; } case 7: { printf("\nEnter number: "); scanf("%lf", &first_Num); printf("\nThe absolute value of %.4f is %.4f\n\n", first_Num, fabs(first_Num)); break; } case 8: { do { printf("\nFIND THE AREA OF A SHAPE\n\n"); printf("Which shape?\n"); printf("\t1 - Square\n"); printf("\t2 - Rectangle\n"); printf("\t3 - Parallelogram\n"); printf("\t4 - Trapezoid\n"); printf("\t5 - Circle\n"); printf("\t6 - Ellipse\n"); printf("\t7 - Triangle\n"); printf("\t8 - Go back to previous menu\n"); printf("\nMake a selection: "); scanf("%d", &selection); switch (selection) { case 1: { printf("\nArea Of A Square\n"); printf("\nEnter length of a side of the square: "); scanf("%lf", &first_Num); while (first_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter length of a side of the square: "); scanf("%lf", &first_Num); } area = first_Num * first_Num; printf("\nThe area of a square with a side of %.4f is %.4f\n\n", first_Num, area); break; } case 2: { printf("\nArea Of A Rectangle\n"); printf("\nEnter width: "); scanf("%lf", &first_Num); while (first_Num <=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter width: "); scanf("%lf", &first_Num); } printf("Enter height: "); scanf("%lf", &sec_Num); while(sec_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &sec_Num); } area = first_Num * sec_Num; printf("\nThe area of a rectagle with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area); break; } case 3: { printf("\nArea Of A Parallelogram"); printf("\nEnter base: "); scanf("%lf", &first_Num); while (first_Num <=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base: "); scanf("%lf", &first_Num); } printf("\nEnter height: "); scanf("%lf", &sec_Num); while(sec_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &sec_Num); } area = first_Num * sec_Num; printf("\nThe area of a parallelogram with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area); break; } case 4: { printf("\nArea Of A Trapezoid"); printf("\nEnter base-1: "); scanf("%lf", &first_Num); while (first_Num <=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base-1: "); scanf("%lf", &first_Num); } printf("\nEnter base-2: "); scanf("%lf", &sec_Num); while(sec_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base-2: "); scanf("%lf", &sec_Num); } printf("\nEnter height: "); scanf("%lf", &third_Num); while(third_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &third_Num); } area = (third_Num/2) * (first_Num + sec_Num); printf("\nThe area of a trapezoid with a base-1 of %.4f, a base-2 %.4f, and a height of %.4f is %.4f\n\n", first_Num, sec_Num, third_Num, area); break; } case 5: { printf("\nArea Of A Circle\n"); printf("\nEnter the radius: "); scanf("%lf", &rad1); while(rad1 <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter the radius: "); scanf("%lf", &rad1); } area = (rad1 * rad1) * PI; printf("The area of a circle with a radius of %.4f is %.4f\n\n", rad1, area); break; } case 6: { printf("\nArea Of An Ellipse"); printf("\nEnter radius-1: "); scanf("%lf", &rad1); while(rad1 <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter radius-1: "); scanf("%lf", &rad1); } printf("\nEnter radius-2: "); scanf("%lf", &rad2); while(rad2 <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter radius-2: "); scanf("%lf", &rad2); } area = (rad1 * rad2) * PI; printf("The area of an ellipse with a radius-1 of %.4f and a radius-2 of %.4f is %.4f", rad1, rad2, area); break; } case 7: { printf("\nArea Of A Triangle"); printf("\nEnter base: "); scanf("%lf", &base); while(base<=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base: "); scanf("%lf", &base); } printf("Enter height: "); scanf("%lf", &height); while(height<=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &height); } area = (base * height) / 2; printf("The area of a triangle with a base of %.4f and a height of %.4f is %.4f", base, height, area); break; } default: { if (selection !=8) { printf("\nYour selection was not from the menu\n"); printf("Please try again.\n\n"); } break; } } break; }while (selection != 8); break; } default: { if (choice != 0) { printf("\nYour selection was not from the menu\n"); printf("Please try again.\n\n"); } break; } } } while (choice != 0); return (0); }
400+ lines of unindented code - no thanks!
Here, read this
SourceForge.net: Indentation - cpwiki
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
oh geez, sorry that my formatting was messed up while pulling this code of my gmail drive.....happy?Code:#include <stdio.h> #include <math.h> #define PI 3.14159 int main(void) { double sum, first_Num, sec_Num, third_Num, diff, product, quotient, srt, area, rad1, rad2, base, height; int choice, selection; do { printf("\nChoose what calculation to perform\n"); printf("\t1 - Addition\n"); printf("\t2 - Subtration\n"); printf("\t3 - Multiplication\n"); printf("\t4 - Division\n"); printf("\t5 - Square Root\n"); printf("\t6 - Exponents\n"); printf("\t7 - Absolute Value\n"); printf("\t8 - Area of Shapes\n"); printf("(Enter a zero to exit) \n"); printf("\nMake a selection: "); scanf("%d", &choice); switch (choice) { case 1: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); sum = first_Num + sec_Num; printf("\nThe sum of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, sum); break; } case 2: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); diff = first_Num - sec_Num; printf("\nThe difference of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, diff); break; } case 3: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); product = first_Num * sec_Num; printf("\nThe product of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, product); break; } case 4: { printf("\nEnter first number: "); scanf("%lf", &first_Num); printf("Enter second number: "); scanf("%lf", &sec_Num); quotient = first_Num / sec_Num; printf("\nThe quotient of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, quotient); break; } case 5: { printf("\nEnter number: "); scanf("%lf", &srt); { while (srt <= 0) { printf("ERROR!! Value must be a positive number\n\n"); printf("Enter number: "); scanf("%lf", &srt); } printf("\nThe square root of %.4f is %.4f\n", srt, sqrt(srt)); } break; } case 6: { printf("\nEnter base number: "); scanf("%lf", &first_Num); printf("Enter exponent: "); scanf("%lf", &sec_Num); printf("\n%.4f to the power of %.4f is %.4f\n\n", first_Num, sec_Num, pow(first_Num, sec_Num)); break; } case 7: { printf("\nEnter number: "); scanf("%lf", &first_Num); printf("\nThe absolute value of %.4f is %.4f\n\n", first_Num, fabs(first_Num)); break; } case 8: { do { printf("\nFIND THE AREA OF A SHAPE\n\n"); printf("Which shape?\n"); printf("\t1 - Square\n"); printf("\t2 - Rectangle\n"); printf("\t3 - Parallelogram\n"); printf("\t4 - Trapezoid\n"); printf("\t5 - Circle\n"); printf("\t6 - Ellipse\n"); printf("\t7 - Triangle\n"); printf("\t8 - Go back to previous menu\n"); printf("\nMake a selection: "); scanf("%d", &selection); switch (selection) { case 1: { printf("\nArea Of A Square\n"); printf("\nEnter length of a side of the square: "); scanf("%lf", &first_Num); while (first_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter length of a side of the square: "); scanf("%lf", &first_Num); } area = first_Num * first_Num; printf("\nThe area of a square with a side of %.4f is %.4f\n\n", first_Num, area); break; } case 2: { printf("\nArea Of A Rectangle\n"); printf("\nEnter width: "); scanf("%lf", &first_Num); while (first_Num <=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter width: "); scanf("%lf", &first_Num); } printf("Enter height: "); scanf("%lf", &sec_Num); while(sec_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &sec_Num); } area = first_Num * sec_Num; printf("\nThe area of a rectagle with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area); break; } case 3: { printf("\nArea Of A Parallelogram"); printf("\nEnter base: "); scanf("%lf", &first_Num); while (first_Num <=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base: "); scanf("%lf", &first_Num); } printf("\nEnter height: "); scanf("%lf", &sec_Num); while(sec_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &sec_Num); } area = first_Num * sec_Num; printf("\nThe area of a parallelogram with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area); break; } case 4: { printf("\nArea Of A Trapezoid"); printf("\nEnter base-1: "); scanf("%lf", &first_Num); while (first_Num <=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base-1: "); scanf("%lf", &first_Num); } printf("\nEnter base-2: "); scanf("%lf", &sec_Num); while(sec_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base-2: "); scanf("%lf", &sec_Num); } printf("\nEnter height: "); scanf("%lf", &third_Num); while(third_Num <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &third_Num); } area = (third_Num/2) * (first_Num + sec_Num); printf("\nThe area of a trapezoid with a base-1 of %.4f, a base-2 %.4f, and a height of %.4f is %.4f\n\n", first_Num, sec_Num, third_Num, area); break; } case 5: { printf("\nArea Of A Circle\n"); printf("\nEnter the radius: "); scanf("%lf", &rad1); while(rad1 <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter the radius: "); scanf("%lf", &rad1); } area = (rad1 * rad1) * PI; printf("The area of a circle with a radius of %.4f is %.4f\n\n", rad1, area); break; } case 6: { printf("\nArea Of An Ellipse"); printf("\nEnter radius-1: "); scanf("%lf", &rad1); while(rad1 <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter radius-1: "); scanf("%lf", &rad1); } printf("\nEnter radius-2: "); scanf("%lf", &rad2); while(rad2 <= 0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter radius-2: "); scanf("%lf", &rad2); } area = (rad1 * rad2) * PI; printf("The area of an ellipse with a radius-1 of %.4f and a radius-2 of %.4f is %.4f", rad1, rad2, area); break; } case 7: { printf("\nArea Of A Triangle"); printf("\nEnter base: "); scanf("%lf", &base); while(base<=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter base: "); scanf("%lf", &base); } printf("Enter height: "); scanf("%lf", &height); while(height<=0) { printf("ERROR!! Distance must be a positive number\n\n"); printf("Enter height: "); scanf("%lf", &height); } area = (base * height) / 2; printf("The area of a triangle with a base of %.4f and a height of %.4f is %.4f", base, height, area); break; } default: { if (selection !=8) { printf("\nYour selection was not from the menu\n"); printf("Please try again.\n\n"); } break; } } break; } while (selection != 8); } default: { if (choice != 0) { printf("\nYour selection was not from the menu\n"); printf("Please try again.\n\n"); } break; } } } while (choice != 0); return (0); }
I would like to add something to this ,which obviously finds me in an agreement.
Friendly tip : You have an error.Everybody has been into a situation like that
So how to help us help you.Post the code that has the error!Not the code that you had before the error.Report what the compiler says.Copy paste the messages he sends to you!
Moreover posting all the code of yours might be not a must. To be more clear, posting your whole code should the last thing to do.
The ideal thing to do is to post as few lines of code as possible to fully describe your problem.
This of course takes some experience, but you have to get yourself thinking like this...
Example
Assume that you are into a situation that your compiler throws an error of undefined variable in a large program..
You want to post , because after trying to solve it yourself multiple times, you didn't succeed it.What you should do?Post the whole code?
No, try to isolate the error .Also post the error message too.
The errorCode:#include <stdio.h> int main(void) { char buf[10]; int i; printf("Please type an alpharithmetic input\n"); fgets(bof , 10 , stdin); ..... /* 64387648367 lines of code */ return 0; }
Wasn't it better that i isolated the problematic code from the rest of my huge program?Code:Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px px.c: In function ‘main’: px.c:9: error: ‘bof’ undeclared (first use in this function) px.c:9: error: (Each undeclared identifier is reported only once px.c:9: error: for each function it appears in.)
Of course it is not so simple as that every time , but i think you got my point
EDIT : We answered the same time.Read my post too and think twice of what you should post
Not particularly, you've used some pre-coloured code, and in doing so, suppressed the boards ability to line number the code for you.
Can you do "paste as text" in future.
> printf("\nFIND THE AREA OF A SHAPE\n\n");
FWIW, all of this code should be reduced to
And all 8 cases which make up the area calculations moved into a new function along the lines ofCode:case 8: { doShapeAreas(); break; }
Code:void doShapeAreas ( void ) { do { printf("\nFIND THE AREA OF A SHAPE\n\n"); printf("Which shape?\n"); printf("\t1 - Square\n"); printf("\t2 - Rectangle\n"); printf("\t3 - Parallelogram\n"); printf("\t4 - Trapezoid\n"); printf("\t5 - Circle\n"); printf("\t6 - Ellipse\n"); printf("\t7 - Triangle\n"); printf("\t8 - Go back to previous menu\n"); printf("\nMake a selection: "); scanf("%d", &selection); switch (selection) { case 1: // and so on } while ( selection != 8 ); }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
there is no error and my areas work fine. the only problem was when i tried to use the M_PI in place of my defined PI value, which isn't a big deal anyways. my teacher wants me to write my own code and algorithms and not just use what is included in the math.h header file, there is no problem here. it might be a longer code that i can probably condense but memory is not an issue here.
i would have placed just a few lines of code but you asked to see my code, you did not specify what you wanted to see so i gave it all to you. my main goal from where my code is now, to where i would want it to be is being able to input a string of characters (i.e. 2+5/2*8 + (2-5) ) and be able to solve. the problem i am having with that code is i cant seem to convert from a char* to int or double.
Code:#include<stdio.h> #include<math.h> #include <stdlib.h> #include <string.h> int indexOf(char input[],char search) { int d; int index = -1; for(d=0;d<strlen(input) && index==-1;d++) { if(input[d]==search) index = d; } return index; } int twoVariableExecution(char input[]) { int d; char operation = '-'; int operand_index = -1; int plus_index = indexOf(input,'+'); int sub_index = indexOf(input,'-'); int mul_index = indexOf(input,'*'); if(plus_index > -1) { operation = 'A'; operand_index = plus_index; } else if(sub_index > -1) { operation = 'S'; operand_index = sub_index; } else if(mul_index > -1) { operation = 'M'; operand_index = mul_index; } char first[operand_index-1]; memcpy(first,input,operand_index); char second[55]; memcpy(second,input + (operand_index+1),(strlen(input)-operand_index)); double dbl_second; double dbl_first; dbl_first = first; dbl_second = second; printf("first is %e and second is %e", dbl_first, dbl_second); switch(operation) { case 'A': printf("you chose addition jackass"); break; case 'S': printf("you chose subtraction jackass"); break; case 'M': break; } return(1); } int main(void){ char user_input[255]; printf("Print yoru equation:"); scanf("%s",user_input); printf("\nyour chosen equation was: %d\n",twoVariableExecution(user_input)); printf("%d",indexOf("........ you",'u')); return(0); }
Last edited by friedsandwich; 11-15-2012 at 02:06 PM.
About the char* you said, take a look at the last post here..Read the fgets and atoi part
Yes i agree that for your code, using your own defined π is not a problem.However where you have the problem with the M_PI is not at all visible by me, because you did not post the code where you get the relative error