简体   繁体   English

程序挂起,不输出任何内容

[英]Program hangs and doesn't output anything

When i run this code, i get a warning: Control reaches end of non void function [Wreturn-type]. 当我运行此代码时,我得到一个警告:控制到达非void函数[Wreturn-type]的末尾。 I think this might be an infinite recursion case but i don't know how to fix it. 我认为这可能是无限递归的情况,但我不知道如何解决。

This program is supposed to intake numbers until you enter something that's not a number. 该程序应该输入数字,直到您输入的不是数字。 The poramnet() function swaps any 9's, with 7's and returns them. poramnet()函数将任何9与7交换并返回它们。 I also have a condition where i have to print out the biggest 5 numbers, and if i don't have 5 numbers in the array i have to print them all. 我也有一个条件,我必须打印出最大的5个数字,如果我在数组中没有5个数字,则必须全部打印出来。

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

int poramnet(int n, int m, int i){ //i should start with 1 
    if(n==0)
        return m;
    if(n%10==9){
        m+=i*7;
    }
    else{
        m+=i*(n%10);
        return poramnet(n/10, m, i*10);
    }
}

int main()
{
    int array1[100], i=0, output[100], br=1, j, temp, m=0, n;
    while(1){
    scanf("%d", &array1[i]);
    if(!isdigit(array1[i]))
        break;
        i++;
    }
    n=i;
    for(i=0;i<n;i++){
        output[i]=poramnet(array1[i], m, br);
    }
    for(i=0;i<n-1;i++){
        for(j=1;i<n;j++){
            if(output[i]>output[j]){
                temp=output[i];
                output[i]=output[j];
                output[j]=temp;
            }
        }
    }
    if(n<5){
        for(i=0;i<n;i++)
            printf("%d ", output[i]);
    }
    else{
        for(i=0;i<5;i++){
        printf("%d ", output[i]);
        }
    }
    return 0;
}

Points to keep in mind: 注意事项:

  1. Always use brackets with if block, even if you think it unnecessary. 始终将方括号与if块一起使用,即使您认为不必要也是如此。
  2. Replaced the scanf with getc . getc替换了scanf
    1. Here one has to ignore the 'ENTER' key, 这里必须忽略“ ENTER”键,
    2. and then convert to int , 然后转换为int
    3. and then check with isdigit . 然后用isdigit检查。
  3. One of your nested for loops had a bug: j<n is needed, not i<n . 您的一个嵌套for循环存在一个错误:需要j<n ,而不是i<n
  4. Keep exits/returns from a function to a minimum. 保持函数的退出/返回到最小。 IF you still want to use multiple returns from a function, then at least put a default return, which you know will always be hit, even if you program something wrong. 如果您仍然想使用一个函数的多个返回值,那么至少要放一个默认返回值,即使您编写了错误的代码,也知道它将始终被击中。

See the comments in the code for more detail. 有关更多详细信息,请参见代码中的注释。

main.cpp main.cpp

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

int poramnet(int n, int m, int i){ //i should start with 1
    // Always put brackets with if !!! It avoid confusion
    if(n==0) {
        return m;
    }
    if(n%10==9){
        m+=i*7;
        return m;
    }
    else{
        m+=i*(n%10);
        return poramnet(n/10, m, i*10);
    }
    // put a default return always, so you know if the function returns
    // in an expected way, that you can track it back down.
    return -1;
}

int main()
{
    int array1[100], i=0, output[100], br=1, j, temp, m=0, n;

    // Instead of scanf, you could use getc, ignore the "enter" key-press,
    // and convert to an int from char and then check with isdigit().
    while(1){
        char tempChar;
        tempChar = getc(stdin);
        if (tempChar != '\n') {
            // printf("what you entered is, %c\n", tempChar);
            // printf("is it a digit:  %d\n", !isdigit(tempChar));
            tempChar = tempChar - '0';
            // printf("is it a digit:  %d\n", !isdigit(array1[i]));
            if(isdigit(tempChar)) {
                break;
            }
            array1[i] = tempChar;
            i++;
        }
    }

    n=i;
    for(i=0;i<n;i++){
        output[i]=poramnet(array1[i], m, br);
    }



    for(i=0;i<n-1;i++){
        for(j=1;j<n;j++){ // you had a bug here also! you need j<n and not i<n !! copy-paste error!
            if(output[i]>output[j]){
                temp=output[i];
                output[i]=output[j];
                output[j]=temp;
            }
        }
    }

    if(n<5){
        for(i=0;i<n;i++)
            printf("%d ", output[i]);
    }
    else{
        for(i=0;i<5;i++){
            printf("%d ", output[i]);
        }
    }
    return 0;
}

Output 输出量

junglefox@ubuntu:~/test_programs$./test junglefox @ ubuntu:〜/ test_programs $。/ test

1 1个

2 2

3 3

4 4

5 5

a 一种

1 4 3 2 5 junglefox@ubuntu:~/test_programs$ 1 4 3 2 5 junglefox @ ubuntu:〜/ test_programs $

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

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