-7

"Rewrite numbers from input to output. Stop processing input after reading in the number 42. All numbers at the input are integers of one or two digits."

#include <stdio.h>

int main() {
    int num ;

    int repeat()
    {  
        scanf("%d",&num) ;
        if(num!=42)
        {
             printf("\n%d",num) ;
             repeat() ;
        }
        else
        {
            return num ;
        }
        getch() ;
    }

    return 0;
}
Boann
  • 48,794
  • 16
  • 117
  • 146
Abhi N
  • 13
  • 4

3 Answers3

4

Why do Control don't enter repeat() function?

Because main() does not call it.

The repeat() function is defined inside main(). Which is non standard. Moving it out makes things clearer:

#include <stdio.h>

int num ;

int repeat()
{  
    scanf("%d", &num);
    if(num != 42)
    {
        printf("\n%d", num);

        repeat();
    }
    else
    {
        return num ;
    }

    getch();
}

int main() {
    return 0;
}

From the above it is obvious that main() in fact does nothing.

alk
  • 69,737
  • 10
  • 105
  • 255
0

You created the function repeat() but you never call it Also you should pass num as an argument to repeat() and expect it to return.

0

// Thanks for the help

#include <stdio.h>
int repeat(int num) 
    {  
        scanf("%d",&num) ;
        if(num!=42)
        {
               printf("%d",num) ;
               repeat(num) ;
        }
    else
    {
        return num ;
    }
    getch() ;
    }

int main() {
       int num ;
       scanf("%d",&num) ;
       repeat(num) ;
       return 0;
}
Abhi N
  • 13
  • 4