summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs
blob: 971811eabafe02c82afb957ef4b1d4ef9eb5a798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::io;
use std::process;

fn read_input() -> i32 {
    let mut input = String::new();

    io::stdin()
        .read_line(&mut input)
        .expect("failed to read line");

    let input = match input.trim().parse() {
        Ok(input) => input,
        Err(_) => {
            println!("not a number!");
            process::exit(1);
        },
    };
    input
}

fn main() {
    println!("Convert temperatures between Fahrenheit and Celsius");
    println!("");

    loop {
        println!("Please choose converting method:");
        println!("1) Fahrenheit to Celsius          2) Celsius to Fahrenheit");
        println!("3) exit");

        let choice = read_input();

        if choice == 0 {
            let _choice = read_input();
        }   else if choice == 1 {
            fah_to_cel();
        }   else if choice == 2 {
            cel_to_fah();
        }   else if choice == 3 {
            break
        };
    }
    println!("Thinks for using");
}

fn fah_to_cel() {
    println!("Please enter a Fahrenheit temperature");
    let fah = read_input() as f32;

    println!("the Celsius of {fah} is {}", (fah-32.0)/1.8);
    println!("");
}

fn cel_to_fah() {
    println!("Please enter a Celsius temperature");
    let cel = read_input() as f32;

    println!("The Fahrenteit of {cel} is {}", cel*1.8+32.0);
    println!("");
}