iDevGames Forums
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! - sealfin - Sep 13, 2005 02:37 PM

You can code GUI apps in C, look up Carbon; however (and I'm not trying to be discouraging here, we all started somewhere Wink) if you're encountering problems coding for 'toy' apps like the above, Carbon might be a little above you yet; you might want to gain a better understanding of C first.

Edit: akb825 got there first Wink


Help with some beginner C code! - Tyaedalis - Sep 13, 2005 02:42 PM

thats what i thought. but is there anyway of making it into an executable where you can double-click it and you can typpe the two numbers (lwngth and width) that way?


Help with some beginner C code! - akb825 - Sep 13, 2005 02:47 PM

If you double click it, it should open up the terminal and take your input and print out the output from there. You can grab the executable from your build folder.


Help with some beginner C code! - Tyaedalis - Sep 14, 2005 02:10 AM

ok, thanks. it works. anyway, i made 3 variations on the code, so it can do different shapes.

i quit doing that code for now, and am continuing to learn. i just learned my favorite thing! if and else and else if. i used them in BASIC and i liked them. they make a program so much better, and more fun to program.


Help with some beginner C code! - unknown - Sep 14, 2005 06:34 AM

Now that you can do if's ask the user to enter the name of the shape, merge the 3 programs together.


Help with some beginner C code! - akb825 - Sep 14, 2005 10:48 AM

If you put all that in a loop, you can do multiple calculations. If you're interested and don't know where to start, I can post some code to get you started.


Help with some beginner C code! - Tyaedalis - Sep 14, 2005 02:44 PM

ok, let me try frst, but be ready wit that code Wink

edit: im having a problem.

i cant get it to record like "circle" and then let me use that like

Code:
if (shape = circle)
     {
          printf( "enter the radius of the circle:  \n" );
          scanf( "%d" , radius );
     }

first it asks what shape you want, and gives you a list, but then when i enter "circle" as the shape to caculate, how do i get it to be brought up by it's char variable? (the variable is shape)


Help with some beginner C code! - akb825 - Sep 14, 2005 03:30 PM

First of all, you use a double equals sign (==) to check for equality. Second of all, when you enter the information, it's entered as a character array (char *). In order to check for equality between strings, you need to use strcmp from string.h. If strcmp is 0, the strings are the same. (so after you include string.h, you do if (!strcmp(shape, "circle")) or if (strcmp(shape, "circle") == 0)


Help with some beginner C code! - Tyaedalis - Sep 14, 2005 03:37 PM

ok, i changed it to another thing before your post.

Code:
#include <stdio.h>

int main()
{

    int area;
    int radius;
    int length;
    int depth;
    int width;
    int side;
    int pi = 3.14;
    char shape;
    
    printf( "\nWhat shape would you like to find the area of?\n(Circle, square, cube, cylinder, or parallelogram.)\n\n" );
    scanf( "%s" , shape );
    
    switch (shape) {
        case "circle":
            printf( "Enter the radius of the circle:  \n" );
            scanf( "%d" , radius );
            
            area = pi * radius * radius;
            printf( "The area of the circle is %d.\n" , area );
        break;
        }

    return (0);
    
}



Help with some beginner C code! - akb825 - Sep 14, 2005 03:40 PM

You can't do that, since it would only check the pointer, not the actual string. M_PI is a more precise version of pi, BTW. (you may need to include math.h)


Help with some beginner C code! - Tyaedalis - Sep 14, 2005 03:41 PM

ok, this doesn't work either:

Code:
#include <stdio.h>

int main()
{

    int area;
    int radius;
    int length;
    int depth;
    int width;
    int side;
    int pi = 3.14;
    char shape;
    
    printf( "\nWhat shape would you like to find the area of?\n(Circle, square, cube, cylinder, or parallelogram.)\n\n" );
    scanf( "%s" , shape );
    
    if (shape == "circle") {
            printf( "Enter the radius of the circle:  \n" );
            scanf( "%d" , radius );
            
            area = pi * radius * radius;
            printf( "The area of the circle is %d.\n" , area );
        }

    return (0);
    
}

and ill change the Pi thing. well, i would if i knew how to use it...


Help with some beginner C code! - akb825 - Sep 14, 2005 06:38 PM

me Wrote:Second of all, when you enter the information, it's entered as a character array (char *). In order to check for equality between strings, you need to use strcmp from string.h. If strcmp is 0, the strings are the same. (so after you include string.h, you do if (!strcmp(shape, "circle")) or if (strcmp(shape, "circle") == 0)

me Wrote:You can't do that, since it would only check the pointer, not the actual string.

(random text entered to get past length restriction)


Help with some beginner C code! - Tyaedalis - Sep 14, 2005 07:29 PM

i dont understand that.


Help with some beginner C code! - akb825 - Sep 14, 2005 08:45 PM

A variable that is a string (or character array, such as shape) isn't really a string. Rather, the string resides in a place in memory, and the variable points to that place in memory. It is called a pointer because it points to that location. Since it's a pointer, and not an actual string, using == compares the pointers, and not the strings. If you didn't understand that, it's OK, but what you do need to understand is the fact that in order to see if 2 strings are equal, you need to use the function strcmp. In order to use it, you need to import string.h, and it returns 0 if the 2 strings are equal. As I mentioned earlier, you can use it in an if statement by either doing

if (!strcmp(shape, "circle"))

or

if (strcmp(shape, "circle") == 0)

(basically, the first one uses the !, so if what strcmp returns is 0, the if evaluates as true, otherwise false, and the second one does an equality check to see if strcmp returns 0)


Help with some beginner C code! - Tyaedalis - Sep 14, 2005 09:14 PM

and where do i place this in the code?

edit:

i also have this that also has an error on the "return" line.

here's the code:

Code:
#include <stdio.h>

int main()
{

    int area;
    int radius;
    int length;
    int depth;
    int width;
    int side;
    int pi = 3.14;
    int shape;
    int repeat;
    
        do {
    
        printf( "\nWhat shape would you like to find the area of?\n" );
        printf( "Enter 1 for circle.\n" );
        printf( "Enter 2 for square.\n" );
        printf( "Enter 3 for cube.\n" );
        printf( "Enter 4 for cylinder.\n" );
        printf( "Enter 5 for parallelogram.\n\n" );
        scanf( "%d" , &shape );
    
        switch (shape) {
            case 1:
                printf( "Enter the radius of the circle:  \n" );
                scanf( "%d" , &radius );
            
                area = pi * radius * radius;
                printf( "The area of the circle is %d.\n" , area );
                    break;
            case 2:
                printf( "Enter the length of one side of the square:  \n" );
                scanf( "%d" , &side );
                
                area = side * side;
                printf( "The area of the square is %d.\n" , area );
                    break;
            default:
                repeat = 1;
                
            } while (repeat == 1);
        }
return 0;  [b][color=red]/* ERROR HERE */[/color][/b]
}