You have enter wrong datatype when comparing the choice (int type) with a string text. In C/C++ unlike dynamic programming language, you cannot compare int with strings without properly converting either of them. You use strcpy for comparing string and == for comparing int.
Then your programming will run.
Answer from Khalid Ahmad Nori on Stack OverflowVideos
How to use an if statement in C?
Why do we use if statement in C?
Can an if statement have multiple conditions in C?
You have enter wrong datatype when comparing the choice (int type) with a string text. In C/C++ unlike dynamic programming language, you cannot compare int with strings without properly converting either of them. You use strcpy for comparing string and == for comparing int.
Then your programming will run.
I'm not sure what your program is trying to do, but let me concentrate on the few obviously incorrect lines.
First, in
int Choice;
scanf ("%s", &Choice);
you have the wrong type for Choice: it is "int" whereas it should be a static array of char (let's say char Choice[32]). In this case you also have to remove the "&" before "Choice" in the scant, so that the code becomes:
char Choice[32];
scanf ("%s", Choice);
Moreover, in
else if(Choice == "Energy") //This isnt working in my compiler.
you are trying to compare two strings with the operator "==". This does not work in C. You should use the function "strcmp" the following way:
#include<string.h>
[...]
else if(strcmp(Choice, "Energy")==0)
You'd even better use the following to prevent any buffer overflow
else if(strncmp(Choice, "Energy", 32)==0)
(replace 32 with the maximum number of elements in Choice)
Edit Note that you should change the first comparison too from
if(Choice == 2817)
to
if(strncmp(Choice, "2817", 32))
because Choice is not an int anymore...
Remove semi-colon
remove semi-colon this lines
if (&radius > -1); {
should be
if (radius > -1) {
Should do this for easier tracking if-else statement
change these lines
printf("Area = %d\n", area);}
return(0); }
to
printf("Area = %d\n", area);
}
return(0);
}
Here is style for if-else statement, I think it's easier for you to track your code
if (condition) {
statements;
}
else if (condition) {
statements;
}
else {
statements;
}
The C compiler doesn't care about formatting, in theory you can do whatever you like, and there's no consensus for what is/isn't "proper formatting".
However, most programmers stick to a specific style so that it's easier to read the source code; and if you're working in a team then it's nice if all programmers on the team use the same style so that all of the source code is consistent. To achieve that, there may be a formal "style guide", and if there is you should follow it.
Beyond that, there are some common rules that almost everyone follows:
nested blocks that are delimited by braces are indented somehow (with either "N space characters" or "N tab characters") relative to the parent block
cases of a switch statements will be exceptions to the indentation rules. Typically each case's statements are indented even though there's no braces; and the case keyword itself may or may not be indented by the parent switch's braces.
either all braces are always on a line by themselves; or starting braces are at the end of the line and ending braces may be at the start of a line containing a related statement
when a block consists of a single statement; either it always uses braces and is takes up a line by itself, or it never uses braces and shares the same line as its parent.
the
else ifpair is always an exception to the "block consists of a single statement" rule (theifis a single statement that is never treated as a separate block, and people pretendelse ifis a singleelseifkeyword).
What this means is that (depending on who got their way when arguing about it) this might acceptable:
int main() {
int area;
printf("Enter radius: ");
scanf("%d", &radius);
switch(radius) {
case 0:
return 0;
case 1:
return 1;
}
if (&radius > -1) {
area = (int)(3.14159 * radius * radius);
printf("Area = %d\n", area);
} else return -1;
return area;
}
..and this might also be acceptable:
int main()
{
int area;
printf("Enter radius: ");
scanf("%d", &radius);
switch(radius)
{
case 0:
return 0;
case 1:
return 1;
}
if (&radius > -1)
{
area = (int)(3.14159 * radius * radius);
printf("Area = %d\n", area);
}
else
{
return -1;
}
return area;
}