Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (lgbasallote):

is it possible to put a character in an if-else condition in C?

OpenStudy (lgbasallote):

here's my idea... i give the user a formula. And then the user picks a variable to solve from the formula. How do I make C understand the input variable?

OpenStudy (anonymous):

Yes there is /: if ( condition ) { expr_set1; } else { expr_set2; } for example : If given condition is TRUE, expr_set1 will get executed. If given condition is FALSE (not TRUE), expr_set2 will get executed. if..else example Following example will find out large number from given input: #include<stdio.h> int main(){ int x,y; printf("Enter value for x :"); scanf("%d",&x); printf("Enter value for y :"); scanf("%d",&y); if ( x > y ){ printf("X is large number - %d\n",x); } else{ printf("Y is large number - %d\n",y); } return 0;

OpenStudy (anonymous):

http://www.cyberciti.biz

OpenStudy (anonymous):

@lgbasallote a medal

OpenStudy (anonymous):

thanks @MathTHEORY

OpenStudy (lgbasallote):

character...not integer

OpenStudy (lgbasallote):

and asking for medals is low....

OpenStudy (lgbasallote):

and the answer you copy-pasted from here http://www.cyberciti.biz/faq/if-else-statement-in-c-program/ has a different question than mine

OpenStudy (anonymous):

well,you first have to declare it (at the beginning of the program you can use something like "char i" , i being the variable ) and then you can have the user insert a value for it (by asking for it with a "cout" and then using a "cin" in order to receive the input ). That's kinda all that i can tell you for now. However,you will have to let me see the code in order to be more specific. Well,hope this helped you,but if you have any further questions please message me !

OpenStudy (lgbasallote):

here's what i did ``` #include <stdio.h> #include <conio.h> #include <stdlib.h> main() { char variable printf("V = u + at\n\nwhere:\nV = final velocity\nu = initial velocity\na = acceleration\nt = time"); printf("Which variable would you like to solve? "); scanf("%c", &variable); if (variable==v) { /*code for solving v*/ } getch(); } ``` that's my plan

OpenStudy (anonymous):

well,firstly,you have to declare all the variables you're using...(if you've tried running your code you will get that error.. ),and by all i mean EVERYTHING !!! secondly,if you're making this for another user,then use more lines...make that formula more clearer (i already have a headache when i'm looking at it... ) then,you can use a structure such as "if......else if" for the variable cases that you're writing ! oh,and another question( just curious) .....does the user have to write the code to solve that variable,or do you write it and then display it for the user (as in a lesson !? ) Well,hope this answers your question ! Message me if you didn't understood something or if you see anything mistaken in my answer. Cheers !

OpenStudy (anonymous):

And finally regarding your variable,i think my first answer still stands,and you have to declare your variable FIRST and then use it as you see fit !

OpenStudy (lgbasallote):

if (variable==v) means that the program will solve for v

OpenStudy (lgbasallote):

that's my plan...but the program says v is undeclared

OpenStudy (anonymous):

AND,i was wrong,correction,we were wrong.....the variable should be declared as an integer...

OpenStudy (lgbasallote):

yeah..that's my problem

OpenStudy (lgbasallote):

i need to know how to use a character in the if condition

OpenStudy (anonymous):

as the term of your formulas seem to hold big values ( such as velocity) you should use something like "long v"

OpenStudy (lgbasallote):

what do you mean?

OpenStudy (anonymous):

well,v,or any other term from your formula,holds a integer value,right ? (i'm guessing very big)

OpenStudy (lgbasallote):

the user gives the value of u, a and t and then the program solves for v

OpenStudy (anonymous):

in this context,you should use a value that holds an integer (call it a variable,if you prefer ).The way you introduce these variable differs and has to resemble with the range of the value they hold. For something more detailed you should look here : http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx

OpenStudy (anonymous):

so,at the beginning of your program,you should type something such as " int v,u,t ; " ,structure in which you can replace "int" with terms from the link i've sent you,all of this process depending on the values your variables will hold (as i'm sure you will see,there are different types of introducing a variable in a program for different ranges of values). Cheers !

OpenStudy (lgbasallote):

i'll try that thanks!

OpenStudy (anonymous):

You're welcome !

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!