简体   繁体   English

为什么我的C程序要求的输入多于应该的? 额外的输入甚至没有做任何事情

[英]Why is my C program asking for one more input than it should? And the extra input doesn't even do anything

The the code for that portion, if you want to compile it is below. 如果你想编译它的那部分的代码如下。

Basically I have to take the following data and input it into the program: 基本上我必须将以下数据输入到程序中:

4 5
12 5 7 0 -3
9 11 2 5 4
0 -5 9 6 1
2 12 93 -15 0
5 3
7 1 31
0 0 5
-5 -3 2
9 41 11
0 13 31

The first 4 5 & 5 3 represent the dimensions of the first and second matrix, while the data after it is the data for the matrix. 第一个4 5和5 3表示第一个和第二个矩阵的维度,而后面的数据是矩阵的数据。

The problem is, when I copy and paste that in, it asks for one more input afterward, and when I input anything (say 84) it works flawlessly (output wise), and the 84 appears to do nothing. 问题是,当我复制并粘贴它时,它要求再输入一次,当我输入任何内容(比如说84)时,它可以完美地工作(输出方式),84似乎什么也不做。 Why's it asking for this extra one? 为什么要求这个额外的?

#include <stdio.h>

int main(int argc, char *argv[]) {
    int rows1 = 1, columns1 = 1, rows2 = 1, columns2 = 1;    // variables for number of rows and columns in each matrix 
        int i, j, k;                                             // loop variables 

        // These will affect the loop's length
        scanf("%d %d", &rows1, &columns1);
        int matrix1[rows1][columns1];
        for (i = 0; i < rows1; i++) {
            for (j = 0; j < columns1; j++) {
                scanf("%d ", &matrix1[i][j]);
            }
        }


        scanf("%d %d", &rows2, &columns2);
        int matrix2[rows2][columns2];
        for (i = 0; i < rows2; i++) {
            for (j = 0; j < columns2; j++) {
                scanf("%d ", &matrix2[i][j]);
            }
        }
}

Your last scanf requires 2 things 你的上一次扫描需要2件事

                scanf("%d ", &matrix[i][j]);
                //     112

Thing 1 is an integer 东西1是整数
Thing 2 is zero or more of whitespace 事物2是空白的零或更多

If you provide "31" (or "31 " or "31\\n") to the scanf it will be expecting more whitespace. 如果向scanf提供“31”(或“31”或“31 \\ n”),则需要更多的空格。 Once you provide "84", it know the whitespace has ended and carries on with the program. 一旦你提供“84”,就知道空白已经结束并继续执行程序。


Suggestion: remove all whitespace from the scanf conversion specifiers: scanf("%d%d") is good! 建议:从scanf转换说明符中删除所有空格: scanf("%d%d")是好的! The "%d" conversion specifier already removes whitespace before the number. "%d"转换说明符已删除数字前的空格。

Better yet, read data with fgets() and parse with sscanf() 更好的是,使用fgets()读取数据并使用sscanf()解析

The behaviour is due to the spaces in: 行为是由于以下空格:

scanf("%d ", &matrix1[i][j]);
scanf("%d ", &matrix2[i][j]);

Change the format specifiers to: 将格式说明符更改为:

scanf("%d", &matrix1[i][j]);
scanf("%d", &matrix2[i][j]);

(Well, it's only the second one that matters, but you might as well keep them identical.) (嗯,这只是第二个重要的,但你也可以让它们保持一致。)

A space in the format specifier matches one or more whitespaces in the input. 格式说明符中的空格匹配输入中的一个或多个空格。 When your code is run, the final scanf() sits there reading from stdin until it encounters a non-whitespace character (the 8 in 84 ), at which point it returns and your program ends. 当你的代码运行时,最后的scanf()坐在那里从stdin读取,直到它遇到一个非空白字符( 848 ),此时它返回并且你的程序结束。

I think that what is happening is that you specified white-space after integer when you used scanf("%d ", &matrix2[i][j]); 我认为当你使用scanf("%d ", &matrix2[i][j]);时,你在整数之后指定了空格scanf("%d ", &matrix2[i][j]); . scanf ignores white-space by default when using %d . 使用%d时, scanf默认忽略空格。 However you specified a space so when it gets to the end of the file and sees an integer with no white-space following it doesn't see anything that matches your formatted input. 但是,您指定了一个空格,因此当它到达文件的末尾并看到一个没有空格的整数后,它看不到任何与您的格式化输入匹配的内容。 Once you enter the new integer you also put a space after the last number satisfying the input formatting. 输入新整数后,您还会在满足输入格式的最后一个数字后面添加一个空格。 So it wasn't the number you needed but the whitespace. 所以这不是你需要的数字,而是空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 C 程序不等待扫描输入? - Why doesn't my C program wait to scan input? C程序数组需要的输入过多 - C program array takes more input than it should 如何在此程序中添加一个 do-while 循环,以便如果输入小于 4,它不会打印任何内容 - How do I add a do-while loop in this program so that if the input is less than 4 it doesn't print anything 为什么输入不会将我的输入保存在 c 中的 txt 文件中? - why the input doesn't save my input in txt file in c? 为什么在退出 do-while 循环后程序会崩溃,要求在 C 中输入? - Why does program crash after exiting do-while loop asking for input in C? 即使输入非常大,我的 InsertionSort 程序运行速度也比 MergeSort 程序快。 为什么? - My InsertionSort Program runs faster than MergeSort Program even with very large input. Why? 为什么我的程序不允许我输入 b? - Why my program doesn't allow me to input b? 为什么我的程序不等待另一个输入? - Why doesn't my program wait for another input? 在 c 中只应输入两次时要求输入三次 - Asking for input thrice when it should only do twice in c C:即使输入文件作为参数,程序也会要求用户输入 - C: Program asking for user input even when input file is given as an argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM