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
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/swift/examples/output/ConsoleInput1.out
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
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/swift/examples/output/ConsoleInput2.out

Questions

Projects

More ★'s indicate higher difficulty level.

References