Value is not initialized into the variable in c

384 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

The compiler does not see in the point of the call

get_and_validate_user_input(num_of_rows);

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 type int but when it encountered the function definition it sees that the return type is void.

Place the function declaration before main.

The value of the passed parameter is not used in the function

void get_and_validate_user_input(int num_of_rows){
    //...
    //Getting the input from the user
    scanf("%d",&num_of_rows);
    //...

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

int get_and_validate_user_input( void )
{
    int num_of_rows = 0;

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

    return num_of_rows;
}

And call it in main like

num_of_rows = get_and_validate_user_input();
0
On

your problem is get_and_validate_user_input modifies the local variable num_of_rows => the value stay 0 in main

if you want to have num_of_rows modified also out of get_and_validate_user_input give it by address rather than by value, example (unused variable commented, modify check in get_and_validate_user_input to be compatible with message) :

#include <stdio.h>
#include <stdlib.h>

void get_and_validate_user_input(int * num_of_rows);

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 is : %d\n",num_of_rows);
  return 0;
 }

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
    if (scanf("%d",num_of_rows) != 1) {
      int c;

      puts("Invalid input");
      // invalid input, bypass all the line
      while ((c = getchar()) != '\n') {
        if (c == EOF) {
          puts("EOF, abort");
          exit(-1);
        }
      }
    }
    else if(*num_of_rows < 0){ // 0 seems allowed, else update message
      printf("Negative numbers are not allowed here \n");
    }
    else {
      break;
    }
  }
}

Compilation and execution:

/tmp % gcc -Wall c.c
/tmp % ./a.out
Enter the number of rows : 12
 The number of rows is : 12
/tmp % ./a.out
Enter the number of rows : aze
Invalid input
Enter the number of rows : -2
Negative numbers are not allowed here 
Enter the number of rows : 12
 The number of rows is : 12
/tmp %