Swift programming help

Hi,
I decided to go back to college to get my Associate Degree in SGD Programming, which is just programming for games. I took this class for swift to program IOS games. It's been one week and I am already stuck on the first assignment, and I really need help with a question that I can't get right.

The question is asking the user to input a number, n, and find the sum of the numbers that equal to the input. ex: 1+2+3+...+n.
this is all i have right now
print ("enter a number")
let n = readLine()!;
print("you entered:");
print(n);

It's not much, but I just moved on because I can't figure it out. Any help is appreciated.

Well, you can’t just follow readLine() with !. That’s saying “When the program reaches the end of input, crash.” You have to use if let (or guard let, if you’ve gotten so far) to find out whether there is more input or not, and follow accordingly.

I’ll give you one more. After you’ve done this, all you have is a String. You have to convert the String to an Int to do arithmetic with it. The tool to do that is Int(String)

2 Likes

Judging from the example Imad posted, they're just getting started, in which case I actually recommend using readLine()!. No need to go into optionals and if let here. If they're reading input from a terminal, readLine will just wait for input, not crash.

@programming-beginner Did you learn how to convert the input to an Int, as John showed? Did you also learn about loops, such as while and for? You'll need them to solve this exercise.

3 Likes

It will wait for input if there is nothing typed at all, but it will crash on a ^D (or perhaps that would be a ^Z on Windows). And handling a nil will be necessary anyway if the user types a non-number.

2 Likes

@svanimpe is right. Since I just started I never learned about if let or guard let, so that did confuse me. @svanimpe I did learn how to convert and I learned about loops too.

Thanks for replying and trying to help too, I'm still trying to figure it out.

Another way of talking about this is to say that you need a loop, in which you ask for numbers, you must add those numbers together, you must look for a signal from the user that they are done putting in numbers, and then you must show the sum.

A while loop looks like this:

while(/* some test */) {
    //some statements
}

In this case, you might want to use a time honored construction:

while(true) { 
    /// do some stuff
    if /* some condition is true */ {
        break
    }
}

An important swiftism, alluded to above: since readLine returns an "optional", which is simply a regular type but could be "nil", you have to test for that. Swift demands that you handle this. Here's a way to do that:

If let inp = readLine() {
// do things with inp
} else {
 // user entered an EOF
}

This sets the variable inp to a non-optional type, but only if the readLine() returns something other than nil. if it does return nil, the else clause is run instead.

You could use that as a signal, but if the user runs the program on the command line, they might not know how to send an EOF, so if might be better to use a character as a signal, like "=" or "q".

A word about optionals: An Int type can contain any integer from Int.min to Int.max. An Int? can contain and integer from Int.min to Int.max, or the special value nil. This means that a function which returns an integer has a way to say that it doesn't have a good answer, rather than giving you a wrong one.

Int.min, by the way is -9223372036854775808 on my machine.
Int.max is 9223372036854775807

I hope these clues help.

Well, I meant that to go to OP, but there it is.