When I enter a char the program goes infinitiley

I tried putting the cin.fail() incase so that I can stop the loop and let the user enter a value again but then its infinite loop
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
60
 void strassenMultiplication() {
    int first[2][2], second[2][2], third[2][2], i, j;
    int p1, p2, p3, p4, p5, p6, p7;

    //input 4 elements in first matrix
    printf("Enter the 4 elements of the first matrix: ");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &first[i][j]);
            if (cin.fail()){
                printf("you have entered a non-numeric data. Please Try Again.");
                cin>>first[i][j];
            }

    //input 4 elements in second matrix
    printf("Enter the 4 elements of the second matrix: ");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &second[i][j]);

    //displays first matrix
    printf("\nThe first matrix is\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            printf("%d\t", first[i][j]);
        printf("\n");
    }

    //displays second matrix
    printf("\nThe second matrix is\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            printf("%d\t", second[i][j]);
        printf("\n");
    }

    //formulas for strassen multiplication
    p1 = first[0][0] * (second[0][1] - second[1][1]);
    p2 = (first[0][0] + first[0][1]) * second[1][1];
    p3 = (first[1][0] + first[1][1]) * second[0][0];
    p4 = first[1][1] * (second[1][0] - second[0][0]);
    p5 = (first[0][0] + first[1][1]) * (second[0][0] + second[1][1]);
    p6 = (first[0][1] - first[1][1]) * (second[1][0] + second[1][1]);
    p7 = (first[0][0] - first[1][0]) * (second[0][0] + second[0][1]);


    third[0][0] = p5 + p4 - p2 + p6;
    third[0][1] = p1 + p2;
    third[1][0] = p3 + p4;
    third[1][1] = p1 + p5 - p3 - p7;

    //displays result after multiplication
    printf("\nThe result after the multiplication is: \n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            printf("%d\t", third[i][j]);
        printf("\n");
    }
    printf("\n");
}
you have to clear the buffer by reading off what is still in it.
another way is to read as strings, and check those, then convert valid ones into numbers.
Thanks for the help!
anytime. most would use cin.ignore to read off the offending data.
Why are you mixing c-style input/output with C++? If you're coding C++ then use C++

scanf() returns the number of fields successfully assigned. So if L9 doesn't do an assign, then the return value from scanf() will be 0.

As C, then perhaps something like:

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
60
61
62
63
64
65
66
67
68
69
void strassenMultiplication() {
    int first[2][2], second[2][2], third[2][2];

    //input 4 elements in first matrix
    printf("Enter the 4 elements of the first matrix: ");

    char ch;    // Temp for bad input

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; )
            if (scanf("%d", &first[i][j]) != 1) {
                printf("You have entered a non-numeric data. Please Try Again.");
                scanf("%[^\n]c", &ch);
            } else
                ++j;

    //input 4 elements in second matrix
    printf("Enter the 4 elements of the second matrix: ");
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2;)
            if (scanf("%d", &second[i][j]) != 1) {
                printf("You have entered a non-numeric data. Please Try Again.");
                scanf("%[^\n]c", &ch);
            } else
                ++j;

    //displays first matrix
    printf("\nThe first matrix is\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            printf("%d\t", first[i][j]);

        putchar('\n');
    }

    //displays second matrix
    printf("\nThe second matrix is\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            printf("%d\t", second[i][j]);

        putchar('\n');
    }

    //formulas for strassen multiplication
    int p1 = first[0][0] * (second[0][1] - second[1][1]);
    int p2 = (first[0][0] + first[0][1]) * second[1][1];
    int p3 = (first[1][0] + first[1][1]) * second[0][0];
    int p4 = first[1][1] * (second[1][0] - second[0][0]);
    int p5 = (first[0][0] + first[1][1]) * (second[0][0] + second[1][1]);
    int p6 = (first[0][1] - first[1][1]) * (second[1][0] + second[1][1]);
    int p7 = (first[0][0] - first[1][0]) * (second[0][0] + second[0][1]);


    third[0][0] = p5 + p4 - p2 + p6;
    third[0][1] = p1 + p2;
    third[1][0] = p3 + p4;
    third[1][1] = p1 + p5 - p3 - p7;

    //displays result after multiplication
    printf("\nThe result after the multiplication is: \n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            printf("%d\t", third[i][j]);

        putchar('\n');
    }
    putchar('\n');
}

@freshman236784,
I hope you know that in genuine (and worthwhile) Strassen multiplication your elements are block matrices, not individual ints.

I can't comprehend multiplying 2x2 matrices like this.
Topic archived. No new replies allowed.