Here is the program I have made:
#include <stdio.h>
int main(void)
{
//Put variables here
int space = 0;
int num_of_rows = 0;
int p = 1;
int t = 0;
char alphabet[100] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
get_and_validate_user_input(num_of_rows);
printf(" The number of rows are : %d", num_of_rows);
}
void get_and_validate_user_input(int num_of_rows)
{
//Input validation
while (1)
{
printf("Enter the number of rows : ");
//Getting the input from the user
scanf("%d", &num_of_rows);
if (num_of_rows <= 0)
{
printf("Negative numbers are not allowed here \n");
}
else
{
break;
}
}
}
Expected Input: Enter the number of rows: 5
Expected Ouput: The number of rows is: 5
But from my code output shows wrong. It shows: The number of rows is: 0
Now my question is: What's wrong I have been made in my program? How can I solve this problem?
The compiler does not see in the point of the call
how the function
get_and_validate_user_input
is declared. So the compiler issue a message. It expects that the function by default has the return typeint
but when it encountered the function definition it sees that the return type isvoid
.Place the function declaration before main.
The value of the passed parameter is not used in the function
The function parameter num_of_rows is a local variable of the function that is initialized by the value of the passed argument. So changing the local variable does not influence on the original argument.
You could declare and define the function like
And call it in main like