Console Input

This page is under construction. Please come back later.
#!/usr/bin/env swift;
/******************************************************************************
* This program demonstrates how to prompt the user for input.
*
* Copyright © 2020 Richard Lesh. All rights reserved.
*****************************************************************************/
import Foundation
import Utils
func main() -> Void {
let NAME:String = Utils.prompt("What is your name? ")
let FAVORITE_COLOR:String = Utils.prompt("What is your favorite color? ")
print(Utils.format("Hello, {0:s}! I like {1:s} too!", NAME, FAVORITE_COLOR))
exit(EXIT_SUCCESS)
}
main()
Output
$ swiftc ConsoleInput1.swift -I . -L . -lUtils
$ ./ConsoleInput1
What is your name? Rich
What is your favorite color? blue
Hello, Rich! I like blue too!
#!/usr/bin/env swift;
/******************************************************************************
* This program demonstrates how to prompt the user for input.
*
* Copyright © 2020 Richard Lesh. All rights reserved.
*****************************************************************************/
import Foundation
import Utils
func main() -> Void {
var favoriteInt:Int32 = 0
var favoriteLong:Int64 = 0
var favoriteDouble:Double = 0.0
do {
let FAVORITE_INT_INPUT:String = Utils.prompt("What is your favorite small integer? ")
try favoriteInt = Utils.stoi(FAVORITE_INT_INPUT)
let FAVORITE_LONG_INPUT:String = Utils.prompt("What is your favorite large integer? ")
try favoriteLong = Utils.stol(FAVORITE_LONG_INPUT)
let FAVORITE_DOUBLE_INPUT:String = Utils.prompt("What is your favorite floating point? ")
try favoriteDouble = Utils.stod(FAVORITE_DOUBLE_INPUT)
} catch let ex as NumberFormatError {
print("Bad input! " + ex.localizedDescription)
} catch {
print("Don't know what went wrong!")
}
let SUM:Double = Double(favoriteInt) + Double(favoriteLong) + favoriteDouble
print(Utils.format("All together they add up to {0:f}!", SUM))
exit(EXIT_SUCCESS)
}
main()
Output
$ swiftc ConsoleInput2.swift -I . -L . -lUtils
$ ./ConsoleInput2
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? 3.141926
All together they add up to 1000329.141593!
$ swiftc ConsoleInput2.swift -I . -L . -lUtils
$ ./ConsoleInput2
What is your favorite small integer? 326
What is your favorite large integer? abc
What is your favorite floating point? 3.141926
Bad input!
$ swiftc ConsoleInput2.swift -I . -L . -lUtils
$ ./ConsoleInput2
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? abc
Bad input!
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]
Pure Programmer


