Help with some beginner C code!
You can code GUI apps in C, look up Carbon; however (and I'm not trying to be discouraging here, we all started somewhere
) 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
) 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
Mark Bishop
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?
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.
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.
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.
Now that you can do if's ask the user to enter the name of the shape, merge the 3 programs together.
Sir, e^iπ + 1 = 0, hence God exists; reply!
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.
ok, let me try frst, but be ready wit that code 
edit: im having a problem.
i cant get it to record like "circle" and then let me use that like
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)

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)
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)
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);
}
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)
ok, this doesn't work either:
and ill change the Pi thing. well, i would if i knew how to use it...
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...
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)
i dont understand that.
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)
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)
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:
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]
}
