This is the temperature conversion project. Compiler says the variables "pre_temp_d" and "pre_temp" are not found in scope at the end of the program and I have no idea why. ANY help would be appreciated. And no, I do not know how to properly paste code onto this forum.
fn main() {
loop {
println!("Convert from what degrees? F or C");
let mut pre_temp_d = String::new();
io::stdin()
.read_line(&mut pre_temp_d)
.expect("Failed to read line.");
let pre_temp_d: char = match pre_temp_d.trim().parse() {
Ok(char) => char,
Err(_) => continue,
};
match pre_temp_d {
'F' | 'C' => break,
_ => continue,
}
}
loop {
println!("Please input temperature.");
let mut pre_temp = String::new();
io::stdin()
.read_line(&mut pre_temp)
.expect("Failed to read line.");
let pre_temp: f32 = match pre_temp.trim().parse() {
Ok(f32) => break,
Err(_) => continue,
};
}
let conversion: f32;
if pre_temp_d = 'F' {
conversion = ({pre_temp} - 32) / 1.8;
println!("That converts to: {conversion}C");
} else {
conversion = ({pre_temp} * 1.8) - 32;
println!("That converts to: {conversion}F");
}
}```
When you declare a variable (let variable = ...) within a block (like inside loop { ... }), then it can only be used in the block -- unless you do something to move the value outside of the block. Your pre_temp_d and pre_temp variables are declared within the loops.
To see how to fix that part, let's try something simpler. From here:
loop {
let message = "Hello!";
match message.len() {
0 => continue,
_ => break,
}
}
println!("{message}");
let message = loop {
// ^^^^^^^^^^^^^^ new
let message = "Hello!";
match message.len() {
0 => continue,
_ => break message,
// ^^^^^^^^ new
}
};
// ^ new
println!("{message}");
Try applying this to your code. You'll probably still need to say let pre_temp: f32 = ... for the second loop. There are other things you'll have to fix in your code, but see if the new error messages are good enough for you to figure out the problems.