0
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include <windows.h> 

int TotalCI = 0;
int count = 0;
struct depend{
    char dependency[200];
};

struct intdepend{
    char interdependency[200];
};

struct infradep{    
    char name[200];
    int depcount;
    struct depend Dep[20];
    int intdepcount;
    struct intdepend IntDep[20];
};

struct infradep ci[50];

void menuModel(){
    
    int menu4;
    system("cls");
    printf("=========================================\n");
    printf("\tDependency Model\n");
    printf("=========================================\n");
    count = 0;
    for (int j=0; j < TotalCI; j++)
    {
        count++;
//      printf("%d)",count);
//      printf("%s\n",ci[j].name);
        
        for(int k=0; k <= ci[j].depcount; k++)
        {
            if(ci[j].Dep[k].dependency != 0)
            {
                printf(" %s ---> %s\n", ci[j].name, ci[j].Dep[k].dependency);
            }
        }   
        
        for(int m=0; m <= ci[j].intdepcount; m++)
        {
            if(ci[j].IntDep[m].interdependency != 0)
            {
                printf(" %s <---> %s\n", ci[j].name, ci[j].IntDep[m].interdependency);
            }
        }   
    }
}

Im trying to print out the non null value from my struc Dep[20] and struct IntDep[20]. The IF statement works but, it displays like this:

=========================================
        Dependency Model
=========================================
 Drinking Water ---> Food
 Drinking Water ---> Transport
 Drinking Water ---> 
 Drinking Water <---> Food
 Drinking Water <---> 
 Waste Water ---> Chemical
 Waste Water ---> Manufacture
 Waste Water ---> 
 Waste Water <---> 

The code print extra line with null struct value, making it look weird. Can anyone teach me or help me how to fix this?

4
  • 1
    for(int k=0; k <= ci[j].depcount; k++) You may want < instead of <= here. Commented Jan 12, 2022 at 20:52
  • dependency is a string, not a pointer. It can never be null. Commented Jan 12, 2022 at 20:53
  • How do you know that your if statement works? Commented Jan 12, 2022 at 20:54
  • @JohnnyMopp Thanks man, that works. Its displaying properly now. Commented Jan 12, 2022 at 21:13

1 Answer 1

1

An array can never be equal to 0. If you want to test if the string is empty, check the first character.

            if(ci[j].Dep[k].dependency[0] != '\0')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.