c - 如何验证c中的数值?

标签 c

<分区>

当我在数组中插入字符值时,我的程序退出了。如果我插入字符值,我想在不退出程序的情况下继续我的循环并获取下一个整数值。 我的代码有什么问题。

我的代码:

#include<stdio.h>
#include<ctype.h>

int main()
{
    int row1, col1, row2, col2, i, j, k, sum = 0;
    int first[10][10], second[10][10], multiply[10][10];
    clrscr();
    printf("Enter the no of rows and columns of first matrix\n");
    scanf("%d%d",&row1,&col1);
    printf("Enter the Element of first matrix");
    for(i=0;i<row1;i++){
        for(j=0;j<col1;j++){
            printf("\nEnter %d element %d row",j+1,i+1);
            scanf("%d",&first[i][j]);
            check = first[i][j];
            printf("%d",check);
            if(check>0 || check<0)
            {
                printf("this is digit \n");
            }
            else
            {
                printf("this is not digit\n");
                printf("\nEnter %d element %d row",j+1,i+1);
                scanf("%d",&first[i][j]);
            }
        }
    }
    printf("Enter the no of rows and columns of second matrix\n");
    scanf("%d%d",&row2,&col2);

    if(col1 == row2){
        printf("Enter the Element of second matrix");
        for(i=0;i<row2;i++){
            for(j=0;j<col2;j++){
                printf("\nEnter %d element %d row",j+1,i+1);
                scanf("%d",&second[i][j]);
            }
        }

        for(i=0;i<row1;i++){
            for(j=0;j<col2;j++){
                for(k=0;k<row2;k++){
                    multiply[i][j]=sum;
                    sum=0;
                }
            }
        }
        printf("Multiplication of matrices=\n");
        for(i=0;i<row1;i++){
            for(j=0;j<col2;j++){
                multiply[i][j]=0;
                for(k=0;k<row1;k++){
                    multiply[i][j] += first[i][k] * second[k][j];
                }
                printf("%d\t",multiply[i][j]);
            }
            printf("\n");
        }
    }
    else{
        printf("You are incorrect. According to the requirement of matrix multiplication, the number of column of first matrix should equal to the number of rows of second matrix.");
    }
    getch();
    return 0;
}

谢谢你

最佳答案

当你执行

scanf("%d",&first[i][j])

这意味着您的scanf 只接受整数。如果您输入字母字符,则 scanf 将不读取任何内容,然后返回 0 作为 first[i][j]

的值

下一个 scanf("%d"); 将无法获取您的输入(即使它们是数字),因为标准输入尚未清除字母字符。

替换这个

for(i=0;i<row1;i++){
        for(j=0;j<col1;j++){
            printf("\nEnter %d element %d row",j+1,i+1);
            scanf("%d",&first[i][j]);

通过

for(i=0;i<row1;i++){
        for(j=0;j<col1;j++){
            printf("\nEnter %d element %d row",j+1,i+1);
            while (scanf("%d",&first[i][j]))<=0) {
                printf("this is not digit\n");
                printf("\nEnter %d element %d row",j+1,i+1);
                scanf("%*[^\n]"); // this clean your input buffer in case yhe input is not integer
            }

并删除此 block :

            check = first[i][j];
            printf("%d",check);
            if(check>0 || check<0)
            {
                printf("this is digit \n");
            }
            else
            {
                printf("this is not digit\n");
                printf("\nEnter %d element %d row",j+1,i+1);
                scanf("%d",&first[i][j]);
            }

关于c - 如何验证c中的数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624234/

相关文章:

c - 可能知道变量所在的内存部分?

c++ - 基于使用 make 命令或 makefile 运行预处理器

c - 从 c 函数返回一个二维数组

java - 如何使用 JNI 将 Java ByteBuffer 发送到 C?

c - 指向整数并再次返回的指针

c - 当用户在C中输入时如何获取时间戳

c - 如何使用 C 将一个 long 值(32 位)拆分为四个 char 变量(8 位)?

c++ - 关于使用#pragma region 在 Visual Studio 中折叠代码块

c - 为什么要使用函数原型(prototype)?

c - C 中字符串数组的动态分配