Pure Programmer
Blue Matrix


Cluster Map

Console Input

L1

This page is under construction. Please come back later.

ConsoleInput1.swift
#!/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!
ConsoleInput2.swift
#!/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

Projects

More ★'s indicate higher difficulty level.

References