簡體   English   中英

程序掛起,不輸出任何內容

[英]Program hangs and doesn't output anything

當我運行此代碼時,我得到一個警告:控制到達非void函數[Wreturn-type]的末尾。 我認為這可能是無限遞歸的情況,但我不知道如何解決。

該程序應該輸入數字,直到您輸入的不是數字。 poramnet()函數將任何9與7交換並返回它們。 我也有一個條件,我必須打印出最大的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;
}

注意事項:

  1. 始終將方括號與if塊一起使用,即使您認為不必要也是如此。
  2. getc替換了scanf
    1. 這里必須忽略“ ENTER”鍵,
    2. 然后轉換為int
    3. 然后用isdigit檢查。
  3. 您的一個嵌套for循環存在一個錯誤:需要j<n ,而不是i<n
  4. 保持函數的退出/返回到最小。 如果您仍然想使用一個函數的多個返回值,那么至少要放一個默認返回值,即使您編寫了錯誤的代碼,也知道它將始終被擊中。

有關更多詳細信息,請參見代碼中的注釋。

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;
}

輸出量

junglefox @ ubuntu:〜/ test_programs $。/ test

1個

2

3

4

5

一種

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM