Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

arrays - How to validation struct value is exist or not using for loop in C language

i want to ask you, here in my code i used case condition for input, but i got a trouble, in my code i want to check is my "NIM" value was inputted to struct or not, if it was inputted, it can't be inputted to struct. Here are my code

#include <stdio.h>
#include <string.h>

struct data {
    char nim[10];
};

struct data batas[100];
int a, b, c, d;
int i, j;
char x;

void inputdata()
{
  printf("
Input Data
");
  printf("=======================
");
  printf("NIM : "); scanf("%s", batas[a].nim);
  for(i=0; i<a; i++){
    if (strcmp(batas[a].nim, batas[i].nim) == 0) {
      x = "FLAG";
    } else {
      x = "FLAGX";
    }
  }
  printf("%s", x);
  if (strcmp(x, "FLAGX") == 0) {
      a++; // This will input to struct
  }
}

void showdata()
{
  j=0;
  for(i=0; i<a; i++){
    j = j + 1;
    printf("
Data-%i", j);
    printf("
NIM : %s", batas[i].nim);
  }
}

int main() {
  int menu;
  do {
    printf("
Choose input = "); scanf("%d", &menu);
    switch(menu)
    {
      case 1 : inputdata(); break;
      case 2 : showdata(); break;
    }
  }while (menu != 3);

  return 0;
}

From this i always get an error Segmentation Fault. I appreciate you guys, thank you.

question from:https://stackoverflow.com/questions/65857438/how-to-validation-struct-value-is-exist-or-not-using-for-loop-in-c-language

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have already got many suggestion to check your program for warnings and errors.

Check warnings and errors of your program : https://onlinegdb.com/By1VeOK1d

main.c:20:9: warning: assignment makes integer from pointer without a cast [-Wint-conversion]   
                                                 
main.c:22:9: warning: assignment makes integer from pointer without a cast [-Wint-conversion]                                                    

main.c:25:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]    
                              
main.c:26:14: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]  
                             
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘char’                                                       
main.c:44:12: warning: unknown escape sequence: 'C'

All of the above warnings will be cleared just by making two changes to your code

line 11 change char x; as char* x;
line 44 change printf(" Choose input = "); as printf(" Choose input = ");

Moving on to your code correctness, your function inputdata() is depending on global variable a and the loop for(i=0; i<a; i++){ is never true because i < a will never be true, since a is global (default value 0).

Which leads to execution of other statements in an undefined way.

you are directly reading your input into structure scanf("%s", batas[a].nim);, instead read into a local variable like char temp[30] , then add it your batas and continue checking for the existence of your string from next input.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...