Home > Enterprise >  How can I resolve Code will be never executed error
How can I resolve Code will be never executed error

Time:04-04

I am making a program that reads input from a user then check the validity, but for the while statement in the validateUser function I am getting an warning

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int ValidateUser(char *);
int ValidateInput(int);
FILE *database;

struct{
  int year;
  int unit;
  float gpa;
  char semester;
  char grade;
  char name[40];
}student;

int ValidateInput(int x)
{
    while (x != 1 || x != 2 || x != 3) // code that causing a warning
    return x;
}

int ValidateUser(char *input)
{
    int result;
    result = strcmp(input, student.name);
    if (result == 0)
    {
        return 1;
    }
    else
    {
        while (result != 0)
        {
            printf("The Username you entered does not exit. Please enter correct name.");
        }
    }
    return 1;
}

int main() 
{

  printf("\t----------------------------------------------------------\n");
  printf("\t|\t\t\t\t Santa Monica College\t\t\t\t\t |\n");
  printf("\t----------------------------------------------------------\n\n");
  printf("\t\tWelcome to Santa Monica Student Record System.\n\n");
  printf("Please Enter Your Option\n");
  printf("\t1.View GPA/GRADE\n");
  printf("\t2.Add New GPA\n");
  printf("\t3.Modify Information\n");
    
    int choice;
    scanf("%d", &choice);
    char UserName[40];
    
    if(choice == 1)
    {
        printf("Please enter your name\n");
        scanf("%s", UserName);
        ValidateUser(UserName);
        printf("The GPA of %s is %f , %c ", student.name, student.gpa, student.grade);
    }
    else if (choice == 2)
    {
        printf("Please enter your name\n");
        scanf("%s", UserName);
        ValidateUser(UserName);
    }
    else if (choice == 3)
    {
        printf("Please enter your name\n");
        scanf("%s", UserName);
        ValidateUser(UserName);
    }
    else
    {
        ValidateInput(choice);
    }
 
  
  return 0;
}

The ide was suggesting to add parentheses around x-values in the argument of while statement in the ValidateUser to make it silence

I've done some research on this warning and I found that the condition I made is not true so that's why I am getting it, but I am not quite sure what the problem is.

Can someone help me out with this???

CodePudding user response:

In validateUser(), your program will reach the

        while (result != 0)

only in the event that the while condition is initially true, so if it reaches the loop, it will enter it. The body of the loop does not modify result, so if it enters the loop, it will loop indefinitely.

Ultimately, if the first branch of the if / else is taken, then the function returns from within that branch, and if the second branch is taken then control never exits that branch. Either way, the

    return 1;

at the end of that function cannot be reached. Of course, that's just a symptom. The infinite loop is the main problem.

It's unclear what behavior you actually want here, but what would be most in keeping with the name of the function would be for it to only evaluate whether the specified user name is valid, returning a result that conveys either "yes" or "no". There is no particular reason why such a function would need to loop at all.

CodePudding user response:

You put while (result != 0) inside function ValidateUser(char *input) and never put a way to get out of the loop, resulting in the return value below it never running.

To fix this, you would need to add a break; somewhere in the loop to indicate that you are ready to leave the current loop, or make result = 0;

The difference between the two is that break; will indicate that the rest of the loop doesn't need to run and will jump out of the loop, or use result=0; to run the rest of the code in the loop and then jump out of it

  •  Tags:  
  • c
  • Related