Math Problem, entering an answer

Okay, I give up.

I am making a Flash card game where the app will randomize two numbers, then the user would enter the answer. The app will then tell the user if he/she is correct. But I cannot seem to figure out how to get it to add two numbers and check if the user's correct or not.

Any help is greatly appreciated!

Here's the code:

Code Block import UIKit
import AVFoundation
import Foundation
class YouSolveIt: UIViewController {
  
  @IBOutlet var topProblem: UILabel!
  @IBOutlet var botProblem: UILabel!
  @IBOutlet var youAnswer: UITextField!
  @IBOutlet var actualCard: UIImageView!
  
  let speakit = AVSpeechSynthesizer()
  
  var problem1 = Int(0)
  var problem2 = Int(0)
  var answer = 0
  
  var ranA = 0
  var ranB = 0
    override func viewDidLoad() {
        super.viewDidLoad()
      // Load Flash Card image:
      actualCard.image = UIImage(named: "FlashCard")
      
      solveitproblem()
  }
  
  func solveitproblem()
  {
    
    let ranA = Int(arc4random_uniform(UInt32(9)))
    let ranB = Int(arc4random_uniform(UInt32(9)))
    
    topProblem.text = String(ranA)
    botProblem.text = String(ranB)
    youAnswer.text = String(answer)
    
    let speakProblem = AVSpeechUtterance(string: "What is \(topProblem.text! + ", plus ," + botProblem.text!)")
     speakit.speak(speakProblem)
  }
  
  @IBAction func btncheckAnswer(_ sender: Any)
  {
    
    if (ranA) + (ranB) == answer
    {
      
    correctAnswer()
    
    }
    
    else {
      
      wrongAnswer()
    }
  
    
  }
  
  func correctAnswer()
  {
    let right = [1,2,3,4,5]
    
    let randomIndex = Int(arc4random_uniform(UInt32(right.count)))
    
    switch(randomIndex)
    {
      case 1:
        let speakRight = AVSpeechUtterance(string: "That is correct")
        speakit.speak(speakRight)
        youAnswer.text = ""
        solveitproblem()
        
      case 2:
        let speakRight = AVSpeechUtterance(string: "You're right!")
        speakit.speak(speakRight)
        youAnswer.text = ""
        solveitproblem()
        
      case 3:
        let speakRight = AVSpeechUtterance(string: "Correct!  Let's try.")
        speakit.speak(speakRight)
        youAnswer.text = ""
        solveitproblem()
        
      case 4:
        let speakRight = AVSpeechUtterance(string: "You are right!  Next Try.")
        speakit.speak(speakRight)
        youAnswer.text = ""
        solveitproblem()
        
      case 5:
        let speakRight = AVSpeechUtterance(string: "Great answer!")
        speakit.speak(speakRight)
        youAnswer.text = ""
        solveitproblem()
        
      default:
        let speakRight = AVSpeechUtterance(string: "Very good!")
        speakit.speak(speakRight)
        youAnswer.text = ""
        solveitproblem()
    }
  }
  
  func wrongAnswer()
  {
    let wrong = [1,2,3,4,5]
    
    let randomIndex = Int(arc4random_uniform(UInt32(wrong.count)))
    
    switch(randomIndex)
    {
      case 1:
        let speakRight = AVSpeechUtterance(string: "That is wrong")
        speakit.speak(speakRight)
        
      case 2:
        let speakRight = AVSpeechUtterance(string: "You're wrong, please try again.")
        speakit.speak(speakRight)
        
      case 3:
        let speakRight = AVSpeechUtterance(string: "No, that's not it.  Please try again.")
        speakit.speak(speakRight)
        
      case 4:
        let speakRight = AVSpeechUtterance(string: "No")
        speakit.speak(speakRight)
        
      default:
        let speakRight = AVSpeechUtterance(string: "I'm sorry, no.  Please try again.")
        speakit.speak(speakRight)
  }
}
}


Can you explain what is the main issue with your current code?
When you post code, you should use Paste and Match Style to avoid all the extra lines that make reading difficult.

Your problem seems to be here:
Code Block
@IBAction func btncheckAnswer(_ sender: Any) {
if (ranA) + (ranB) == answer {
correctAnswer()
} else {
wrongAnswer()
}
}

Where do you get answer ?
With this code it is always 0
Line 34 should probably be the other way:
Code Block
answer = Int(youAnswer.text) ?? 0

Note: no need to enclose randA and randB in parentheses.

Reformatted code:
Code Block
import AVFoundation
import Foundation
class YouSolveIt: UIViewController {
@IBOutlet var topProblem: UILabel!
@IBOutlet var botProblem: UILabel!
@IBOutlet var youAnswer: UITextField!
@IBOutlet var actualCard: UIImageView!
let speakit = AVSpeechSynthesizer()
var problem1 = Int(0)
var problem2 = Int(0)
var answer = 0
var ranA = 0
var ranB = 0
override func viewDidLoad() {
super.viewDidLoad()
// Load Flash Card image:
actualCard.image = UIImage(named: "FlashCard")
solveitproblem()
}
func solveitproblem() {
let ranA = Int(arc4random_uniform(UInt32(9)))
let ranB = Int(arc4random_uniform(UInt32(9)))
topProblem.text = String(ranA)
botProblem.text = String(ranB)
youAnswer.text = String(answer)
let speakProblem = AVSpeechUtterance(string: "What is \(topProblem.text! + ", plus ," + botProblem.text!)")
speakit.speak(speakProblem)
}
@IBAction func btncheckAnswer(_ sender: Any) {
if (ranA) + (ranB) == answer {
correctAnswer()
} else {
wrongAnswer()
}
}
func correctAnswer() {
let right = [1,2,3,4,5]
let randomIndex = Int(arc4random_uniform(UInt32(right.count)))
switch(randomIndex) {
case 1:
let speakRight = AVSpeechUtterance(string: "That is correct")
speakit.speak(speakRight)
youAnswer.text = ""
solveitproblem()
case 2:
let speakRight = AVSpeechUtterance(string: "You're right!")
speakit.speak(speakRight)
youAnswer.text = ""
solveitproblem()
case 3:
let speakRight = AVSpeechUtterance(string: "Correct! Let's try.")
speakit.speak(speakRight)
youAnswer.text = ""
solveitproblem()
case 4:
let speakRight = AVSpeechUtterance(string: "You are right! Next Try.")
speakit.speak(speakRight)
youAnswer.text = ""
solveitproblem()
case 5:
let speakRight = AVSpeechUtterance(string: "Great answer!")
speakit.speak(speakRight)
youAnswer.text = ""
solveitproblem()
default:
let speakRight = AVSpeechUtterance(string: "Very good!")
speakit.speak(speakRight)
youAnswer.text = ""
solveitproblem()
}
}
func wrongAnswer() {
let wrong = [1,2,3,4,5]
let randomIndex = Int(arc4random_uniform(UInt32(wrong.count)))
switch(randomIndex) {
case 1:
let speakRight = AVSpeechUtterance(string: "That is wrong")
speakit.speak(speakRight)
case 2:
let speakRight = AVSpeechUtterance(string: "You're wrong, please try again.")
speakit.speak(speakRight)
case 3:
let speakRight = AVSpeechUtterance(string: "No, that's not it. Please try again.")
speakit.speak(speakRight)
case 4:
let speakRight = AVSpeechUtterance(string: "No")
speakit.speak(speakRight)
default:
let speakRight = AVSpeechUtterance(string: "I'm sorry, no. Please try again.")
speakit.speak(speakRight)
}
}
}

It will say right answer no matter what answer someone puts in.
Could you print values of ranA, ranB and answer ?
Please report and tell what you entered in the textField

Code Block
@IBAction func btncheckAnswer(_ sender: Any) {
print("randomA", ranA, "randomB", ranB, "answer", answer, "text", youAnswer.text)
if ranA + ranB == answer {
correctAnswer()
} else {
wrongAnswer()
}
}


It will say right answer no matter what answer someone puts in.

Thanks, that saves us much time to explore your code.

You have 3 instance variables ranA, ranB and answer, but there is no code updating them.

I guess you need to update two methods:
Code Block
func solveitproblem() {
ranA = Int.random(in: 0...9) //<- remove `let `
ranB = Int.random(in: 0...9) //<- remove `let `
topProblem.text = String(ranA)
botProblem.text = String(ranB)
//youAnswer.text = String(answer) //<- Do you really need this?
let speakProblem = AVSpeechUtterance(string: "What is \(topProblem.text! + ", plus ," + botProblem.text!)")
speakit.speak(speakProblem)
}
@IBAction func btncheckAnswer(_ sender: Any) {
//↓ Get user's answer into `answer`
guard let answer = Int(youAnswer.text ?? "") else {
//Do something when user does not input an integer value...
//...
return
}
self.answer = answer
if ranA + ranB == answer {
correctAnswer()
} else {
wrongAnswer()
}
}

(You usually do not use arc4random_uniform, but changing it is not mandatory.)
Math Problem, entering an answer
 
 
Q