View Full Version : Help with some beginner C code!
Tyaedalis
2005.09.13, 12:00 AM
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.
#import <stdio.h>
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)
LongJumper
2005.09.13, 12:07 AM
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.
Tyaedalis
2005.09.13, 12:10 AM
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.
akb825
2005.09.13, 12:42 AM
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)
Malarkey
2005.09.13, 12:43 AM
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.
Tyaedalis
2005.09.13, 12:50 AM
i got this:
#import <stdio.h>
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. :(
akb825
2005.09.13, 01:15 AM
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.
Tyaedalis
2005.09.13, 01:16 AM
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);
bold equals output.
akb825
2005.09.13, 01:21 AM
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.
Tyaedalis
2005.09.13, 01:43 AM
oh yeah, those are input, sorry :P.
anyway, you mean when i run it? ill give you the result,
[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.
akb825
2005.09.13, 02:32 AM
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 .
Tyaedalis
2005.09.13, 03:13 AM
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!
w_reade
2005.09.13, 05: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 (http://www.answers.com/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:
#include <stdio.h>
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:
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$
Tyaedalis
2005.09.13, 06:18 PM
wow, thanks. i modified my code and now i understand everything! :D that really helped. (btw, i didnt copy and paste yours :P)
herre's my code:
#import <stdio.h>
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)
akb825
2005.09.13, 06: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.
sealfin
2005.09.13, 06: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 ;)) 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 ;)
Tyaedalis
2005.09.13, 06: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?
akb825
2005.09.13, 06: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.
Tyaedalis
2005.09.14, 06: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.
unknown
2005.09.14, 10:34 AM
Now that you can do if's ask the user to enter the name of the shape, merge the 3 programs together.
akb825
2005.09.14, 02:48 PM
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.
Tyaedalis
2005.09.14, 06:44 PM
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
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)
akb825
2005.09.14, 07: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)
Tyaedalis
2005.09.14, 07:37 PM
ok, i changed it to another thing before your post.
#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);
}
akb825
2005.09.14, 07: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)
Tyaedalis
2005.09.14, 07:41 PM
ok, this doesn't work either:
#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...
akb825
2005.09.14, 10:38 PM
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)
You can't do that, since it would only check the pointer, not the actual string.
(random text entered to get past length restriction)
Tyaedalis
2005.09.14, 11:29 PM
i dont understand that.
akb825
2005.09.15, 12:45 AM
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)
Tyaedalis
2005.09.15, 01:14 AM
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:
#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; /* ERROR HERE */
}
akb825
2005.09.15, 02:15 AM
Ok, if you want to do it that way, with a number based system, you need to replace
switch (shape)
with
switch (shape[0])
to check the first character entered to see what number was entered. Also, you need to put single quotes around the numbers (so have it case '1': and case '2':) because it's checking for a character, not just a number.
The error is because you need to put the while (repeat == 1) after the second bracket.
A few hints:
first of all, you really should initialize repeat to 0 when you declare it, because you don't know what it's going to start out as. Second of all, I recommend repeat being 1 all the time until you press q (have a case 'q': in your switch statement) to quit. The way it's currently, if you enter the wrong thing once, it will loop around for ever and never quit (since repeat will then be 1)
ThemsAllTook
2005.09.15, 10:52 AM
Actually, no. Notice that shape is now an int, and he's scanf'ing for %d, not %s. Just trying to head off some misinformation here...
akb825
2005.09.15, 02:43 PM
Oops, I didn't see that he changed it from %s to %d :blush:
unknown
2005.09.15, 03:52 PM
You cant do testing for string equality in a case statement can you?
akb825
2005.09.15, 05:24 PM
No, it must be able to be tested by a == (or it might be a primitive, I'm not sure which it is for C/C++, but I think it's the former. I know it's the latter for Java :shudder: )
Edit: if you're talking about my method of checking if it was kept as %s and shape was still a char *, since I was taking the index of one of the chars, it would have been fine, since it was just a char being compared.
TomorrowPlusX
2005.09.15, 06:56 PM
I'm ... pretty certain... that you can't switch on anything but an int. As in, no std::string or anything else, even if it's got an overloaded == operator. I'm at work, though, and can't test what I'm saying.
Malarkey
2005.09.15, 08:19 PM
I'm ... pretty certain... that you can't switch on anything but an int. As in, no std::string or anything else, even if it's got an overloaded == operator. I'm at work, though, and can't test what I'm saying.
Correct. Or something that resolves to an int. From the MSDN documentation (since I don't have any C++ books handy at the moment...):
The switch statement allows selection among multiple sections of code, depending on the value of expression. The expression must be of an integral type or of a class type for which there is an unambiguous conversion to integral type
Tyaedalis
2005.09.16, 06:45 PM
yay! it works now. thanks guys. ill finish it and post it.
Tyaedalis
2005.09.16, 08:36 PM
hmm.. there are a few bugs in it...
1. when i do a circle, it doesn't bring up the shape selectoin menu again (and maybe this happens with another, but i havent tried them all.)
2. when i do a cube, it skips some of the input commands like "Enter the length: " or something.
#include <stdio.h>
int main()
{
int area;
int radius;
int length;
int depth;
int width;
int height;
int side;
int pi = 3.14;
int shape;
int repeat = 0;
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( "\nEnter the radius of the circle: \n" );
scanf( "%d" , &radius );
area = pi * radius * radius;
printf( "\nThe 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;
case 3:
printf( "\nEnter the height of the cube: \n" );
scanf( "%d" , &height );
printf( "Enter the width of the cube: \n" );
scanf( "%d" , &width );
printf( "Enter the depth of the cube: \n" );
scanf( "%d" , &depth );
area = height * width * depth;
printf( "The area of the cube is %d.\n" , area );
break;
case 4:
printf( "Enter the height of the cylinder: " );
scanf( "%d" , &height );
printf( "Enter the radius of the cylinder: \n" );
scanf( "%d" , &radius );
area = 2 * pi * radius * radius + 2 * pi * radius * height;
printf( "The area of the cylinder is %d." , area );
break;
default:
repeat = 1;
}
} while (repeat == 1);
return 0;
}
unknown
2005.09.16, 08:55 PM
To improve it slightly you could try using less variables, you use radius for :1 and side for :2. Why not use the same variable as they are both the same type?
A good thing to learn to program loops is to make a program that will print out this kind of thing:
*
***
*****
*******
*****
***
*
Where the user enters the number of rows.
Tyaedalis
2005.09.17, 06:52 PM
i already know how to do that, but i need to know what is wrong with my code. i have looked it over many times, but i can't find anything wrong wth it.
Byron Clarke
2005.09.17, 09:00 PM
In my opinion the best way to learn, at first, is to follow either from a course, or from one the many great books on C. It takes faith, but you'll thank yourself later. Imo experimenting alone yields nothing. Basically, have fun, but learn while doing it
unknown
2005.09.17, 09:16 PM
I wouldn't agree, ive spent my whole life experimenting with languages and books have been the least help for me. I think its down to the dedication of the individual.
Byron Clarke
2005.09.17, 09:49 PM
unknown- good point
My first experiences with programming were pretty experimental. yet I was somehow able to
- create a UI (IB)
- load and display 3D models
- created a tile based movement system
At the time I didn't think it was anything special, but now I look back and can't believe I did it. I suppose it's a matter of preference and choice. Do whatever feels right.
unknown
2005.09.17, 10:14 PM
yeah, I agree. Its really stange looking back, some of the acheivments seem astounding but some of the mistakes seem blindly obvious.
Tyaedalis
2005.09.17, 11:34 PM
well, i finished everything, except i still can't figure out the problem with the cube skipping input lines and the circle not repeating.
#include <stdio.h>
int main()
{
int area;
int radius;
int length;
int depth;
int width;
int height;
int side;
int pi = 3.14;
int shape;
int repeat = 0;
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( "\nEnter the radius of the circle: \n" );
scanf( "%d" , &radius );
area = pi * radius * radius;
printf( "\nThe 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;
case 3:
printf( "\nEnter the height of the cube: \n" );
scanf( "%d" , &height );
printf( "Enter the width of the cube: \n" );
scanf( "%d" , &width );
printf( "Enter the depth of the cube: \n" );
scanf( "%d" , &depth );
area = height * width * depth;
printf( "The area of the cube is %d.\n" , area );
break;
case 4:
printf( "Enter the height of the cylinder: \n" );
scanf( "%d" , &height );
printf( "Enter the radius of the cylinder: \n" );
scanf( "%d" , &radius );
area = 2 * pi * radius * radius + 2 * pi * radius * height;
printf( "The area of the cylinder is %d." , area );
break;
case 5:
printf( "Enter the height of the parallelogram: \n" );
scanf( "%d" , &height );
printf( "Enter the width of the parallelogram: \n" );
scanf( "%d" , &width );
area = length * width;
printf( "The area of the parallelogram is %d." , area );
break;
default:
repeat = 1;
}
} while (repeat == 1);
return 0;
}
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.