Help with some beginner C code! - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: Help with some beginner C code! (/thread-5038.html) Pages: 1 2 3 4 Help with some beginner C code! - Tyaedalis - Sep 12, 2005 08:00 PM I am trying to make a calculator type thing to test my skills. here is the code. it just doesn't work...i dont know why. Code: ```#import    int main() {   int length;   int height;   int area = length*2+height*2;      printf("\nEnter the length of a quadrelateral to calculate the area:  ");   scanf("%d",&length);   printf("\nEnter the height of a quadrelateral to calculate the area:  ");   scanf("%d",&height);   printf("\nThe area of the quadrelateral is %d\n.",area);   return 0; }``` i know the formula for a quadralateral's area is: Area=(length*2)+(width*2) Help with some beginner C code! - longjumper - Sep 12, 2005 08:07 PM int length; //some random number because it is not initialized, let's say 8 int height; //some random number because it is not initialized, let's say 5 int area = length*2 + height*2; //area = 8 * 2 + 5 * 2 = 26 scanf("%d", &length); //length = whatever you typed in, height = 5, area still equals 26 scanf("%d", &height); //length = whatever you typed in before, height = whatever you typed in now, area still equals 26 printf("blah blah %d", area); //prints out 26, because you didn't recalculate area Sparing you the boring details of computer science and engineering, you'll need to recalculate the area once your lenght and height are at the values you want. There are ways to have it constantly keep track of the variables it uses, but they are above your head for the time being. Help with some beginner C code! - Tyaedalis - Sep 12, 2005 08:10 PM i still dont understand fully. what should i do to fix my code? i want the user to input the length and height. i am taking geometry this year, and some programs like this could help me a lot. Help with some beginner C code! - akb825 - Sep 12, 2005 08:42 PM You have to calculate the area after you read in the values. Since you haven't read the values yet with the way you currently did it, they aren't what you wanted them to be. Edit: BTW, you are calculating the perimeter of the quadrilateral, not the area. Area is width*height. (to be exact, this would be for a parallelogram) Help with some beginner C code! - Malarkey - Sep 12, 2005 08:43 PM Tyaedalis Wrote:i still dont understand fully. what should i do to fix my code? i want the user to input the length and height. Move the line that calculates the area to after the lines where the width and height is entered in by the user but before it's printed out. Help with some beginner C code! - Tyaedalis - Sep 12, 2005 08:50 PM i got this: Code: ```#import    int main() {   int length;   int height;      printf("\nEnter the length of a quadrelateral to calculate the area:  ");   scanf("%d",&length);   printf("\nEnter the height of a quadrelateral to calculate the area:  ");   scanf("%d",&height);   int area = length*2+height*2;   printf("\nThe area of the quadrelateral is %d\n.",area);   return 0; }``` it still doesn't work. Help with some beginner C code! - akb825 - Sep 12, 2005 09:15 PM What's the input and output? Edit: FYI, with all but the newest standard of C, you're supposed to put all declarations of variables before anything else, so older compilers might complain about that code. Help with some beginner C code! - Tyaedalis - Sep 12, 2005 09:16 PM Code: ```printf("\nEnter the length of a quadrelateral to calculate the area:  "); [b]scanf("%d",&length);[/b]   printf("\nEnter the height of a quadrelateral to calculate the area:  ");   [b]scanf("%d",&height);[/b]   printf("\nThe area of the quadrelateral is %d\n.",area);``` bold equals output. Help with some beginner C code! - akb825 - Sep 12, 2005 09:21 PM Actually, bold equals input, but that's not what I was talking about. I mean what do you put in as the values then what does it print out as the calculation. Help with some beginner C code! - Tyaedalis - Sep 12, 2005 09:43 PM oh yeah, those are input, sorry . anyway, you mean when i run it? ill give you the result, Code: ```[Session started at 2005-09-12 21:43:05 -0700.] Enter the length of a quadrelateral to calculate the area:  2.5 Enter the height of a quadrelateral to calculate the area:   The area of the quadrelateral is 4 . Executable â€œTesting codesâ€ has exited with status 0.``` Help with some beginner C code! - akb825 - Sep 12, 2005 10:32 PM First of all, since you are using ints, 2.5 probably breaks it. To allow it to take 2.5, you need to change all the ints to either float or double, then change %d to %f in all your printf and scanf functions. BTW, notice how the period is on the second line? (for your result) To fix that, put the \n after the . Help with some beginner C code! - Tyaedalis - Sep 12, 2005 11:13 PM oops, forgot aout the float and missed the period. fixed now. ill update more tomorrow. thanks for all the help! i planon getting more help on other days. just remember that! Help with some beginner C code! - w_reade - Sep 13, 2005 01:40 PM I don't mean to be a complete pedant, but I seem not to be able to help myself :-/. So: You can't tell the area of a quadrilateral with that information; in fact, since "length" and "height" don't necessarily apply usefully to any given quadrilateral, you're not calculating anything meaningful. Toy examples are fine, but really... ;-) Anyway, the area of a rectangle or parallelogram is length * height (but be careful how you measure them); the perimeter of a rectangle is 2*length + 2*height. This code: Code: ```#include int main(int argc, const char **argv)     {     float length;     float height;     float area;     float maybe_perimeter;     printf("\nTo calculate the area of a rectangle or parallelogram, first enter its length:\n> ");     scanf("%f", &length);     printf("\nNow enter its height:\n> ");     scanf("%f", &height);     area = length * height;     printf("\nThe area of the quadrilateral is %f; ", area);     maybe_perimeter = 2 * length + 2 * height;     printf("if it's a rectangle, its perimeter is %f.\n\n", maybe_perimeter);     return 0;     }``` gives this output: Code: ```william-reades-powerbook-g4-17:~ william\$ pico quad.c william-reades-powerbook-g4-17:~ william\$ gcc quad.c -o quad william-reades-powerbook-g4-17:~ william\$ ./quad To calculate the area of a rectangle or parallelogram, first enter its length: > 1.8 Now enter its height: > 2.5 The area of the quadrilateral is 4.500000; if it's a rectangle, its perimeter is 8.600000. william-reades-powerbook-g4-17:~ william\$``` Help with some beginner C code! - Tyaedalis - Sep 13, 2005 02:18 PM wow, thanks. i modified my code and now i understand everything! that really helped. (btw, i didnt copy and paste yours ) herre's my code: Code: ```#import    int main() {   float length;   float height;   float area;      printf( "\nTo calculate the area of a rectangle or parallelogram, enter it's length:  \n" );   scanf( "%f" , &length);   printf( "\nNow, enter it's width:  \n" );   scanf( "%f" , &height);   area = length * height;   printf( "\nThe area of the quadrelateral is %f.\n\n" , area );   return 0; }``` now, coiuld i make it a GUI? or is that not compatable with c? (im using xCode, btw) Help with some beginner C code! - akb825 - Sep 13, 2005 02:32 PM I definitely wouldn't jump from just beginning to understand a few functions and variable types to trying to have a GUI. You can have a GUI with C, but you have to use either an API such as GLUT or SDL (where you would have to do a lot of the stuff yourself, BTW) or use the Carbon API, which can also get quite complicated. You could also use Objective C with Cocoa. Either way, I wouldn't try it for a while.