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

// Begin Main
var name:String
name = Utils.prompt("What is your name? ")
var favoriteColor:String
favoriteColor = Utils.prompt("What is your favorite color? ")
print(Utils.format("Hello, {0:s}!  I like {1:s} too!", name, favoriteColor))

exit(EXIT_SUCCESS)

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

// Begin Main
do {
	var favoriteInt:Int
	var favoriteLong:Int64
	var favoriteDouble:Double
	var favoriteIntInput:String
	favoriteIntInput = Utils.prompt("What is your favorite small integer? ")
	try favoriteInt = Utils.stoi(favoriteIntInput)
	var favoriteLongInput:String
	favoriteLongInput = Utils.prompt("What is your favorite large integer? ")
	try favoriteLong = Utils.stol(favoriteLongInput)
	var favoriteDoubleInput:String
	favoriteDoubleInput = Utils.prompt("What is your favorite floating point? ")
	try favoriteDouble = Utils.stod(favoriteDoubleInput)
	let sum:Double = Double(favoriteInt) + Double(favoriteLong) + favoriteDouble
	print(Utils.format("All together they add up to {0:f}!", sum))
} catch let ex as NumberFormatError {
	print("Bad input! " + ex.localizedDescription)
} catch {
	print("Don't know what went wrong!")
}

exit(EXIT_SUCCESS)

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