简体   繁体   English

K&R C练习1-18没有输出/调试问题

[英]K&R C Exercise 1-18 no output/debugging issues

I wrote the code below in Code::Blocks as a response to K&R C exercise 1-18: 我将以下代码写在Code :: Blocks中,作为对K&R C练习1-18的回应:

Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. 编写程序以从输入的每一行中删除结尾的空白和制表符,并删除整个空白行。

I meant it to remove the blanks and tabs (I haven't tackled the blank line part yet). 我的意思是删除空白和制表符(我还没有解决空白行部分)。 The while loop correctly saves input to the character array ip , however, the rest of the code doesn't seem to be working as EOF doesn't illicit any output at all. while循环正确保存输入字符数组ip ,但是,其余代码似乎并没有被工作作为EOF不违禁任何输出。

#include <stdio.h>
#define MAXLINE 1000

main(){
    int c, z;
    char ip[MAXLINE];

    z = 0;

    while ((c = getchar()) != EOF) {
        ip[z] = c;
        ++z;
    }

    for (z = 0; ip[z] == ' ' || ip[z] == '\t'; ip[z] = ip[z + 1]);
    printf("%s", ip);

}

I was trying to use this issue as a way to learn the debugger, but after I add a breakpoint at line 14, open the watches window, and press the start arrow, nothing happens. 我试图使用此问题作为学习调试器的方法,但是在第14行添加断点后,打开监视窗口,然后按开始箭头,什么都没有发生。 No yellow arrow appears at my break point, my step options are greyed out, and neither my variable names nor their values show up in the watch window. 我的断点处没有黄色箭头,我的步骤选项显示为灰色,并且监视窗口中都没有显示我的变量名或其值。

Advice on fixing my code is appreciated, but what I really want to know is what I'm doing or not doing that is preventing the debugger from helping me. 修复代码的建议值得赞赏,但是我真正想知道的是我在做什么或不做什么,这阻止了调试器对我的帮助。

If you aren't seeing any output, then this is probably because your program is stuck in the for loop: for (z = 0; ip[z] ... , which happens if the string ip starts with two consecutive spaces and/or tabs. 如果您没有看到任何输出,则可能是因为您的程序陷入了for循环中: for (z = 0; ip[z] ... ,如果字符串ip以两个连续的空格开头和/或标签。

The algorithm for removing a certain character is as such: 删除某个字符的算法如下:

Have two variables that index a position in the string, destination and source. 有两个在字符串中索引位置的变量,目标和源。 The code will be inside an outer loop. 该代码将在外部循环内。 Source index will iterate in an inner loop until it finds a character that isn't one of the characters that have to be removed or the null character. 源索引将在内部循环中进行迭代,直到找到不是必须删除的字符之一或空字符为止的字符。 Then the character from source index is assigned to destination index. 然后将源索引中的字符分配给目标索引。 The code then checks if source index reached a null character and breaks the outer loop if it did. 然后,代码检查源索引是否到达空字符,并在中断时中断外部循环。 Both indexes get incremented, the outer loop is then repeated. 两个索引都增加,然后重复外部循环。

In pseudo code: 用伪代码:

remove = '\t'
string
source = 0 
destin = 0

while
    while string at source equals remove and not-equals null character
        source++
    string at destin = string at source
    if string at source equals null character
        break
    source++
    destin++

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM