Why is my code faulty? Ive tried everything in my reach

So, i am new to rust, and i thought it would be a fun project to make a minutes to seconds converter inputted by the user. But, i soon found out that you CANNOT multiply a string by an integer, so please i need help, im also trash at programming in general so, im sorry if the answer is too obvious

fn main() {

   let mut input = String::new();

  const SECONDS_IN_MINUTE: i32 = 60;
  println!("{}",SECONDS_IN_MINUTE);


  let MINUTE_IN_SECONDS: i32 = SECONDS_IN_MINUTE * input;
  println!("how many minutes do you want to convert?");

  io::stdin()
    .read_line(&mut input)
    .expect("FAIL! FAIL! FAIL!");

   // chat gpt im gonna throw a sandal at you if this doesnt work
     let input: i32 = input.trim().parse().expect("Please enter a valid number");

   let MINUTE_IN_SECONDS: i32 = SECONDS_IN_MINUTE * input;

    println!("Your result is: {}!", input);
}

The problem is that you're doing the multiplication before converting the input to an integer.

This is the fixed code:

use std::io;

fn main() {
    const SECONDS_IN_MINUTE: i32 = 60;

    println!("how many minutes do you want to convert ");

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read line");

    // Trim the newline and parse the input
    let num_input: i32 = input.trim().parse().expect("Not a valid number");

    let minute_in_seconds: i32 = SECONDS_IN_MINUTE * num_input;

    println!("Your result is: {} seconds!", minute_in_seconds);
}

Example:
Run the program, then type 10 as number of minutes. It should output 600 seconds.

1 Like

thank you so much!!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.