2

I have taken the input from user but when i am trying to compare, it does not enter the if or else if block . Instead, it goes to else block

use std::io;

fn main() {
    let small_coffee = 2;
    let medium_coffee = 4;
    let large_coffee = 6;

    let mut total_price = 0;

    let mut user_choice = String::new();
    io::stdin().read_line(&mut user_choice).ok().expect("Eror");

    if user_choice == "1" {
        total_price += small_coffee;
    } else if user_choice == "2" {
        total_price += medium_coffee;
    } else if user_choice == "3" {
        total_price += large_coffee;
    } else {
        println!("Sorry! Invalid Choice");
    }

    println!("Your total bill amounted to : {}$", total_price);
}
ruohola
  • 21,987
  • 6
  • 62
  • 97
  • Thank you brother for taking out the time to answer the Question . . . but i am getting the problem using VSCode on Ubuntu 18.04 – M SUbhan Raza Aug 16 '19 at 10:32

1 Answers1

1

This function will read bytes from the underlying stream until the newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes up to, and including, the delimiter (if found) will be appended to buf.

Use trim(), let input = user_choice.trim();

Stargateur
  • 24,473
  • 8
  • 65
  • 91