Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Christian Hughes
Christian Hughes
2,764 Points

Simple function to calculate the area of a rectangle gone horribly wrong.

My code is supposed to take user input, and pass it into a function to calculate the area of a rectangle. It is not working and I am not sure why.

The code is below:

import Foundation

while true {
var width_ = 0
var length_ = 0
func areaOfRectangle(length: Int, width: Int) -> Int {

    let area = length * width
    return(area)
    }

    print("Enter a value for length")
    if let typed1 = readLine()  {
        if let num1 = Int(typed1) {
            var length_: Int = num1
        } else {
            print("Not a valid input")
        }        
    }

    print("Enter a value for width")
    if let typed2 = readLine() {
        if let num2 = Int(typed2) {
            var width_ = num2
        } else {
            print("Not a valid input")
        }
    }    
    print("The area of your rectangle is")
    print(areaOfRectangle(length: length_, width: width_))
}