Please help me. This is so irritating like i just want to take two integers and add them. Instead, it takes it in the form of a string and just concatenates them.
Hello, to do this, you will have to use the
print("Enter a number") var a = readLine()! var aint = Int(a) ?? 0 print("Enter another number") var b = readLine()! var bint = Int(b) ?? 0 var ans = aint + bint print(ans)
??. This is because if the user input was
a, you wouldn't be able to convert it to a number and the variable would become
You just have to convert it into
Int data type
Here is your code with explanation:-
// Taking input of first number and converting it into a Int Data Type print("Enter first number: ") var number = readLine()! var converting = Int(number) ?? 0 // Taking input of second number and converting it into a Int Data Type print("Enter second number: ") var number1 = readLine()! var converting1 = Int(number1) ?? 0 // Adding the both inputs var result = converting + converting1; // Printing the output print(result)
Hope this helps
I've never used swift before, but looking things up, you may need to convert the readline() output into an int. I may be totally and utterly wrong though.
print("Enter a number") var a=Int(readLine())! print("Enter another number") var b=Int(readLine())! var ans=a+b print(ans)