-2

I have written a program to accept and print but getting errors in accepting 2d arrays.

error:- thread 'main' panicked at 'called Result::unwrap() on an Err value: ParseIntError { kind: InvalidDigit }', test1.rs:15:39 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

use std::io;

fn main() {
    let width = 3;
    let height = 3;

    let mut array = vec![vec![0; width]; height];
    let mut input_text = String::new();
    for i in 0..width {
        for j in 0..height {
            io::stdin()
                .read_line(&mut input_text)
                .expect("failed to read from stdin");
            let trimmed = input_text.trim();
            let t:u32=trimmed.parse().unwrap(); //error
            array[i][j]=t;
            println!("{:?}", array);
        }
    }

    println!("{:?}", array);
}

OUTPUT :- 1

[[1, 0, 0], [0, 0, 0], [0, 0, 0]]

2

thread 'main' panicked at 'called Result::unwrap() on an Err value: ParseIntError { kind: InvalidDigit }', test1.rs:15:39 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

  • 1
    Please take your time to provide a proper [mre]. With a problem statement as thin as "getting some errors", it is not clear what exactly the problem is. If compilation failed, post the full error message _verbatim_ from the compiler into the question. If it was an error during execution, post the input provided and corresponding error output. – E_net4 Jan 06 '22 at 15:05
  • It's hard to answer without a proper MRE, but if I had to guess, I'd say you're missing a call to `input_text.clear()` in your inner loop. – Jmb Jan 07 '22 at 08:33
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jan 07 '22 at 08:33

2 Answers2

1

Your code is just one line away from working :).

You may notice that it is always the second entry where the program fails. It is because, at that time, the string input_text contains the previous input (number + newline).

If you check the documentation of std::io::stdin().read_line():

Locks this handle and reads a line of input, appending it to the specified buffer.

As a result of this behaviour, new input values you supply are appended to input_text, instead of replacing its contents. After you input 1 and then 2, input_text contains "1\n2", which obviously cannot be parsed correctly, hence an Err() is thrown.

Hence, the simplest solution to make your code work is to reset input_text to the empty string after every insertion.

-1
use std::io;

fn main(){

let width = 3;
let height = 3;

let mut array1 = vec![vec![0; width]; height];
println!("Enter values for 1st matrix");
for i in 0..width {
    for j in 0..height {
        let mut input_text = String::new();
        io::stdin()
            .read_line(&mut input_text)
            .expect("failed to read from stdin");
        let trimmed = input_text.trim();
        let t:u32=trimmed.parse().unwrap();
        array1[i][j]=t;
        println!("{:?}", array1);
    }
}

let mut _array2 = vec![vec![0; width]; height];
println!("Enter values for 2st matrix");
for a in 0..width {
    for b in 0..height {
        let mut input_text1 = String::new();
        io::stdin()
            .read_line(&mut input_text1)
            .expect("failed to read from stdin");
        let trimmed1 = input_text1.trim();
        let t1:u32=trimmed1.parse().unwrap();
        _array2[a][b]=t1;
        println!("{:?}",_array2);
    }
}

//mult
let mut multmat = vec![vec![0; width]; height];
//println!("Enter values for 2st matrix");
for a in 0..width {
    for b in 0..height {
        multmat[a][b]=0;
        for k in 0..height{
            multmat[a][b]=multmat[a][b]+array1[a][k]*_array2[k][b];
        }
    }
}

for a in 0..width {
    for b in 0..height {
        print!("{} ",multmat[a][b]);
    }
    println!("\n");
}


//println!("{:?}", array1);
//println!("{:?}", _array2);
//println!("{:?}", multmat)

}

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 09:42