how do i do infinite loop in this function in C -


i have question, loop stopping @ sequence 2, want infinite loop function ambil_nilai() , ulang() until scanf receiving word "tidak" , program stop, , seems can't quite right it, please please me, , please tell me if there's not quite right in ode, thank help.

#include <stdio.h>  int ambil_nilai(){     int nilai, nmk;      printf("masukkan mata kuliah yang ingin dicari analisa nya:\n");     scanf("%d",&nmk);     printf("masukkan nilai mata kuliahnya:\n");     scanf("%d",&nilai);     if(nilai<=50){       printf("kamu harus belajar lagi karena nilai kamu kurang\n\n");      }     else if(nilai>=51){       printf("nilai kamu sudah cukup untuk lulus mata kuliah\n\n");     }        return 0; }  char ulang(){     char lagi='y';     char tidak='n';     printf("ingin coba mata kuliah lain? tekan y untuk yes, n untuk no\n");     scanf("%c %c", &lagi,&tidak);      if(lagi){       system("clear");       return ambil_nilai();     }else if(tidak){       printf("terima kasih sudah menggunakan program ini\n");          }     return 1; }  int main() {        printf("\n\n");     printf("ini adalah mata kuliah kamu:\n");     printf("1. a\n");     printf("2. b\n");     printf("3. c\n\n");      ambil_nilai();     ulang();      printf("\n\n");     return 0; } 

i want correct matt93 code explanation :

first, malloc fixed size, that's not useful. plus, didn't check if malloc doesn't return null. moreover, scanf flawed, since can buffer overflow. in addition, doesn't flush stdin, when loop continu, have behavior in program don't want.

here code correct of :

#include <stdio.h> #include <stdlib.h> #include <string.h>  #define str_helper(str) #str #define str(str)        str_helper(str)  #define array_len 5  int main(void) {     char string[array_len + 1];     int  c;      {         printf("enter 'tidak' please\n");         scanf("%"str(array_len)"[^\n]",  string);         while ((c = getchar()) != '\n' && c != eof);         printf("got : '%s'\n", string);     } while (strcmp(string, "tidak") != 0);      return (0); } 

you can read this str_helper , str macro. don't forget read scanf man if have question.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -