What's new

Closed Understanding The Code

Status
Not open for further replies.

tata_Lino

Eternal Poster
Joined
Aug 16, 2017
Posts
533
Reaction
141
Points
261
1571145680681.pngMga sir, ano po ibigsabihin neto? bakit po nagset to default yung weight = -1? and bakit yung operands nya naka set sa 1 2 and 3? newbie lang
 

Attachments

Elementary math lang to, dine-determine lang nito yung order of execution ng operation by applying the PEMDAS rule. Yung weight variable yun yung nag-iindicate ng priority level kung anung operation ang mauunang iperform.

Kung given na ang icacalculate ay ganito:

correct output:
1 + 2 x 3 = 7

wrong output
1 + 2 x 3 = 9
 
Please enclose your code between code tags next time.

Hindi mo ipinost yung function call, so I call only infer from here. Looking at the function definition, ang gusto mo mangyari (i think) is for your function to test if the char function argument is either of of '*', '/', '+', or '-'. Then return their corresponding weight value. And you initialized weight=-1.

The reason you are getting the default weight value -1 is because your function argument is neither (o hindi isa man) sa selection mo. Hence, the switch-block is skipped and went straight into return.

If you want to force the user to input only the accepted choices, then you must do input validation prior to entering switch-block.
 
Status
Not open for further replies.

Similar threads

Back
Top