Quick add: execution aborted, reason: EXC_BAD_INSTRUCTION (code = EXC_l386_INVOP, subcode = 0x0)

I am trying to add two numbers to Swift and print its sum

import Foundation func solveMefirst(firstNo: Int , secondNo: Int) -> Int { return firstNo + secondNo } let num1 = readLine() let num2 = readLine() var IntNum1 = Int(num1!) ** Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0). ** var IntNum2 = Int(num2!) let sum = solveMefirst(IntNum1!, secondNo: IntNum2!) print(sum) 

But, unfortunately, this error appears and stops execution on the playground.

 Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0). 

enter image description here

I could not understand what was wrong with that?

UPDATE

Also explain how to run this command line program on the playground?

How can I accept input on the playground?

-2
source share
2 answers

It crashes because you force the nil value, which leads to an error; As you can read on the left num1= nil . He does not know what he should be, but there is a mistake. . When you try to force the value of num1 (which does "!"), It will work because num1 does not matter.

+1
source

I launched it using the command line. I think you are using the playing field, but you did not accept input for num1 and num2. After starting the project, enter the input for number 1, then press enter. Then enter the input for the second number and press enter. This will give you the desired result. Your code is working fine. I run it here.

Command Line Based Project Output

+1
source

All Articles