1

everyone! I'm a new member of Stack Overflow, just like I'm beginner on swift programming. I'm making this post to find out a solution for the following case:

I'm creating on Swift an app using the Command Line Tool for inputing data. The app basically works as an authenticator. As an example, if someone types USA for the country name and the age is 17, so the program will return a message like "You can not apply to this position". Otherwise, if the country name is USA and the age is equal or higher than 18, so the message returns is "You can forward to the next step". I've tried many times to set this conditions, but it's not working. I'm already knows that the function readLine() is an Optional String, but how can I make my program work correctly? It follows my code above to you understanding my thoughts.

I really appreciate any help. Again, I'm beginner and I'm already studying Swift languages, but I'm seeking some solution that handles Integers and Strings and comparing both data types. Thank you very much!

My code is:

import Foundation

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var age = readLine()

if var country, var age = readLine(){
    if country == "USA" && age < "18" {
        print("You're not allowed to apply to this position.")
    } else {
        print("You can forward to the next step.")
    }
    
}


PS: As you see, I'm using wrongly the variable age as an String, but I want to convert it to an Int type and then, check if the country name is the same than the value I assigned to or the age is equal or higher than 18. But not found a solution so far.

I'm trying to find a solution that compares two different types on Swift, using Command Line Tool and the readLine() function to check if a condition is true or not. If it's true, an output message will show that the user can proceed to the next step, otherwise he will not be permitted to follow. I'm keeping for an explanation on internet since few days, but not found anything that might help me. I hope to get some help using the Stack Overflow forum to some useful answer.

Heber Neto
  • 13
  • 2
  • Does this answer your question? [How to convert Swift 3 output of readLine() to Integer?](https://stackoverflow.com/questions/38167580/how-to-convert-swift-3-output-of-readline-to-integer) – HangarRash Nov 19 '22 at 00:32
  • I've tried to do something else just like shown in the topic you've mentioned, but I'm still facing errors on my code. And also, I can see that one of both conditions (one string, another integer) are happening, even if one of them are false. This is my problem. But I thank you for your attention. I'm keep going to find a solution on this case. – Heber Neto Nov 19 '22 at 01:06

3 Answers3

2

First one, readline() means you read the current to the end of current line . As your code when you check condition you call readLine() again. The wrong part is here.

I recommend you to read first then do all your logic. You just need to read only one time at first

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

Next, check if it is nil or not ( because option value which is value can be nil)

if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

Then check if can convert to Int or not. Reach here you sure that the ageString is not nil because you have checked above. So you just simply convert

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

Then after all, you just check your condition

Full code will be like this

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

if country == "USA" && age < 18 {
    print("You're not allowed to apply to this position.")
} else {
    print("You can forward to the next step.")
}

Other methods is use if let to achieve so no force unwrap

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

if let ageString = ageString {
    if let age = Int(ageString) {
        if country == "USA" && age < 18 {
            print("You're not allowed to apply to this position.")
        } else {
            print("You can forward to the next step.")
        }
    } else {
        print("Error age not a number")
        fatalError()
    }
}
Thang Phi
  • 1,641
  • 2
  • 7
  • 16
  • Thank you very much for your grateful help. I was trying to understanding how to convert a string into an int number. It helped me a lot. Thank you again. I'm gonna get your code and study it to improve my skills on Swift. May God bless you, man! – Heber Neto Nov 19 '22 at 01:54
  • Good answer except for the force unwrap of `ageString`. I wouldn't show that to a beginner. It's too dangerous. Make proper use of `if let`. – HangarRash Nov 19 '22 at 02:02
  • Dear @HangarRash, updated with ``if let`` methods for SO to have different approaches – Thang Phi Nov 19 '22 at 02:07
0

SOLUTION SOLVED!

Hey, guys, first of all I want to thank you all for your helpful answers, which helped me a lot. I've got finally a solution, and am going to share it with you.

What did I done? I just created two variables, one String and another Integer. Then, using the if var to force unwrapping of the Int variable, I've made an if statement to check if the both conditions are true (in the case, if the person is from USA and has an age equals or higher than 18). Now, the program is running on the same way I just wanted. If you are from USA but has no 18 years, output returns a message that you can not apply. Otherwise, you're able to forward.

I'll let the code above. If you want to make some comments or any suggestions, it'll be welcome.

Again, thank you very much for all your answers.

var countryCheck = "USA"
var ageCheck: Int = 18

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var age = readLine()

if var countryCheck = country, var ageCheck = Int(age!) {
    if countryCheck == "USA" && ageCheck >= 18 {
        print("You can apply.")
    } else {
        print("You can not apply to this position.")
    }
}
Heber Neto
  • 13
  • 2
-5

I hope this help you :)

import Foundation

print("Enter your country: ")
if let country = readLine() {
  if let num = Int(country) {
      print(num)
      }
    }
      let country = readLine()
let age = readLine()


if let USA = country, 
    let num1 = Int(USA), 
    let num2 = Int(USA) {
    print("The age of \(num1) and \(num2) is \(num1 + num2)")
}
reema
  • 1
  • 3
  • Thanks a lot for your help. I'm trying to solve this problem for a while and now I got a better understanding about this. – Heber Neto Nov 19 '22 at 01:59
  • 4
    Why was this answer accepted? It has too many calls to `readLine`. It tries to convert the country to an `Int`. It tries to add an age and a country. This answer never explained anything. You really should accept the other answer. – HangarRash Nov 19 '22 at 01:59
  • 3
    Dear reema, What is USA here? Seems like you miss understand about this specific question – Thang Phi Nov 19 '22 at 02:00