Vinod Rathod
Vinod Rathod

Reputation: 822

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

I'm trying to add two numbers in 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 comes out and stops the execution in playground.

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

enter image description here

Could not understand, what is wrong in this?

UPDATE

Also please explain how to run this command line program in playground?

How can I take input from playground??.

Upvotes: 3

Views: 11551

Answers (2)

Shehzad Ali
Shehzad Ali

Reputation: 1845

I ran it using command line. I think you are using play ground but you didn't take input for num1 and num2. After running the project take input for number 1 then press enter. Then take input for number two and press enter. It will give you the desired result. Your code is working fine. I run it here.

Output of Command Line Based Project

Upvotes: 2

Eric
Eric

Reputation: 1213

It crashes because you force a nil value which results in an error; As you can read on the left num1= nil. It don't know what it is supposed to be but there is the error. Because when you try to force a value to num1(that's what "!" does) it crashes because num1 has no value.

Upvotes: 1

Related Questions