Question:
Two problems in this C program?
OHVicktoriaSteve
2009-06-04 22:37:00 UTC
I'm not sure where I went wrong, currently there are two problems:
1. When I press Cube or cylinder, it displays the word "cube" or "cylinder" but then takes me back to the "box" option
2. When I compute the digits for box there's alway an error at the end (before it give me the answer).

#include "simpio.h"
#include "genlib.h"
#include
#include
#include
#define strEqual

main()
{
string option;
int box, cylinder, cube, x, y, z, vol, pi;
pi=3.1415926;
printf("Enter 'box', 'cylinder', or 'cube'\n");
option=GetLine();

if (strEqual(option, "box"))
{
printf("For box:\n");
printf("Enter the first side of the box:\n");
x=GetInteger();
printf("Enter the second side of the box:\n");
y=GetInteger();
printf("Enter the height of the box:\n");
z=GetInteger();
vol =(x*y*z);
printf("Volume of the box is %s\n", vol);
}


else if (strEqual(option, "cube"))
{
printf("For cube\n");
printf("Enter the side of the cube:\n");
x=GetInteger();
vol = (x*x*x);
printf("Volume of the cube is %d\n",vol);
}



else if (strEqual(option, "cylinder"))
{
printf("For cylinder:\n");
printf("Enter the diameter of cylinder:\n");
x=GetInteger();
printf("Enter the length of the cylinder:\n");
y=GetInteger();
vol=(x * x* pi / (4 * y));
printf("Vomlume of the cylinder is %d\n",vol);
}

system("pause");
}
Four answers:
?
2009-06-05 22:44:11 UTC
This is a subtle one in two ways. When you #define strEqual this way:



#define strEqual



you're setting the compile-time variable "strEqual" to an empty string (the value after strEqual). A normal #define does something like this:



#define strEqual strcmp



Thus, when you make those tests, say



if (strEqual(option, "box"))



the C preprocessor substitutes an empty string for strEqual, producing this code for the compiler:



if ( (option, "box" ))



In other words, instead of a C function call, you have a sequential evaluation statement. C allows this, but uses as the value of the statement the value of the last element in it. In this case, that's the address of "box". Since this is a non-zero value, the if( ) statement takes that as a true value and executes the code inside the if clause.



The trouble all lies in that #define. As it's being used, it has to define strEqual AS something.



Hope that helps.
Topher F
2009-06-05 08:49:38 UTC
There's a number of issues here. vol and pi should be declared as doubles (or you can include and use M_PI, the pi constant). You should be using %d instead of %s when you print the "Volume of the box." Also, you should check to make sure you understand what strEqual() returns when the string matches. strcmp, a similar function, returns 0 on a match, which is evaluated as FALSE. If that's not your problem, make sure you're not getting a new-line character in your call to GetLine() (you could be getting "box\n" instead of just "box", which wouldn't match any of your options).



Good luck.
2009-06-05 05:54:52 UTC
First change the declaration of "vol" and "pi" variables to float or double because they includes points.

may be while computing vol variable the size increases the integer size

i.e. -32768 to 32767 in C



"then takes me back to the "box" option"

What do you mean by this as i know u r not using loop then why box option again.
?
2009-06-05 05:47:48 UTC
Why did u declared pi as int and then maked it equal to a float value?I don't know much C,but i cand write the progr for u in cpp.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...