1

When I run the program , It has no error, but when I run it, it gives result that I don't want

The Program(Rust):

use std::io;


fn main() {
    println!("What temperature do you want to convert to?");
    println!("Celcius to Fahrenheit?,Please Type cf");
    println!("Fahrenheit to Celcius?,Please Type fc");
    let mut the_answer = String::new();

    io::stdin().read_line(&mut the_answer).expect("The Answer");
    let x = String::from(the_answer);

    println!("You want to convert to : {}", x);
    println!("What temperature would you like to convert?");

    let mut temperature = String::new();

    io::stdin().read_line(&mut temperature).expect("The Number");
    let temperature : i32 = match temperature.trim().parse(){
        Ok(temperature) => temperature,
        Err(_e) => {
            -1
        }
    };

    match x.as_str(){
        "cf\n" => println!("{}", cf(temperature)),
        "fc\n" => println!("{}", fc(temperature)),
        _ => println!("temperature = {:?}", x),
    }

}

fn cf(c: i32) -> i32 {
    (c * (9/5)) + 32
}

fn fc(f: i32) -> i32 {
    (f-32)*(5/9)
}

The Result:

   Compiling Converter v0.1.0 (C:\Users\NicMar\Documents\Rust Projects\Converter)
warning: crate `Converter` should have a snake case name
  |
  = note: `#[warn(non_snake_case)]` on by default
  = help: convert the identifier to snake case: `converter`

warning: `Converter` (bin "Converter") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.32s
     Running `target\debug\Converter.exe`
What temperature do you want to convert to?
Celcius to Fahrenheit?,Please Type cf
Fahrenheit to Celcius?,Please Type fc
cf
You want to convert to : cf

What temperature would you like to convert?
12
temperature = "cf\r\n"

the result is supposed to be temperature =53.6,

but it shows temperature = "cf\r\n"

Is there any mistake about my code? or I should change the version of the rust?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Does this answer your question? [Why does my string not match when reading user input from stdin?](https://stackoverflow.com/questions/27773313/why-does-my-string-not-match-when-reading-user-input-from-stdin) – Jmb May 31 '22 at 07:05

1 Answers1

2

Like you can see, the problem is that the Enter at the end is not trimmed and so x is "cf\r\n" which is neither "cf" nor "fc", so we fall back to the default arm println!("temperature = {:?}", x).

You just need to .trim() it (or even .trim_end()):

let x = String::from(the_answer.trim());
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • I'm sorry but when I changed it become ```let x = String::from(the_answer.trim());``` The result will be: ```What temperature do you want to convert to? Celcius to Fahrenheit?,Please Type cf Fahrenheit to Celcius?,Please Type fc fc You want to convert to : fc What temperature would you like to convert? 12 temperature = "fc"``` – Nicholas Martin May 31 '22 at 08:53
  • @NicholasMartin Use `"cf"` and `"fc"` in the `match` arms. – Jmb May 31 '22 at 09:10