简体   繁体   English

程序将无法执行功能?

[英]Program won't execute function?

I have no idea what i'm doing wrong here, but I'm trying to make a vertical histogram program based on the length of strings input through the commad line using getchar() (one of The C Programming Language excercises), but something seems to be going wrong when I run it. 我不知道我在这里做错了什么,但是我正在尝试使用getchar() (C编程语言的一种练习),基于通过逗号行输入的字符串的长度来制作一个垂直直方图程序。我运行它时似乎出错了。 The function printgraph() is supposed to print the histogram using the for loops shown by printing the graph, graph[][] line by line, where j increments the y axis and i increments the x axis. 假定函数printgraph()使用for循环打印直方图,该循环通过逐行打印图形graph[][]来显示,其中j递增y轴, i递增x轴。 However, when I run this, the graph doesn't print when it reaches this line of code. 但是,当我运行此代码时,到达此代码行时图形将不会打印。 I've revised the code and gone over it many times, and still haven't a clue. 我已经修改了代码并遍历了很多次,但仍然没有头绪。 I know that this may also be a trivial question to some, and I apologize that I lack much experience, but all help is appreciated. 我知道这对某些人来说可能也是个琐碎的问题,对不起,我缺乏很多经验,但我很感谢所有帮助。

#include <stdio.h>

char graph[11][11];

void printgraph(){
    int i, j;
    char graph[11][11];
    for(j = 0; j<=10; j++){
        for(i = 0; i<=10; i++){
            putchar(graph[i][j]);
        }
        printf("\n");
    }
}
int main(){
    char c, graph[11][11];
    int i, j, onoroff, numchar[10];
    for(i = 10; i>=0; i--)
        graph[0][i] = i;
    for(j=10;j>=0; j--)
        graph[j][0] = j;
    for(j=0;j<=9;i++)
        numchar[j] = 0;
    onoroff = 1;
    i = 0;
    while(graph[1][10] != 'O' || graph[2][10] != 'O' || graph[3][10] != 'O' || graph[4][10] != 'O' || graph[5][10] != 'O' || graph[6][10] != 'O' || graph[7][10] != 'O' || graph[8][10] != 'O' || graph[9][10] != 'O' ||graph[10][10] != 'O'){
        while((c = getchar()) != EOF){
            printgraph();
            if(c == ' '|| c == '\n' || c == '\t'){
                if(onoroff == 1){
                    numchar[i]++;
                    graph[i][numchar[i]+1] = 'O';
                }   
                onoroff = 0;
                i = 0;
            }else if(onoroff == 1){
                i++;
            }else if(onoroff == 0){
                onoroff = 1;
                i++;
            }
        }
    }
    return 0;
}

It never reaches the printgraph function since you are getting stuck in the third for loop. 它永远不会到达printgraph函数,因为您陷入了第三个for循环中。

for(j=0;j<=9;i++)
    numchar[j] = 0;

You are incrementing i but testing j 您正在递增i但正在测试j

Also see the answer from NPToita 另请参阅NPToita的答案

it's because you have 3 variables named graph; 这是因为您有3个名为graph的变量; the global graph variable is never used because the main function has its own local variable graph which it writes to and printgraph has its own version of graph variable which it reads from. 永远不会使用全局图变量,因为main函数有自己的局部变量图,它要写入,而printgraph有它自己的版本的图变量,它是从中读取的。

can you try deleting the graph variables declarations from the main and printgraph functions and see what happens? 您可以尝试从main和printgraph函数中删除图形变量声明,看看会发生什么吗?

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

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