-4

i have a question, my loop is stopping at sequence 2, i want to infinite loop the function ambil_nilai() and ulang() until scanf receiving the word "tidak" and then the program stop, and it seems i can't quite right to do it, please please help me, and please tell me if there's any not quite right in my ode, thank you very much for your 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;
}
mas bro
  • 312
  • 1
  • 12
  • 1
    `while((1) {}` will create an infinite loop for you. Use an `if` and a `break` statement to get out of the loop. –  Jun 10 '16 at 09:23
  • If you want to loop **until** *scanf receiving the word "tidak"* then you don't want an infinite loop. You just told yourself your stop condition. – Jordi Nebot Jun 10 '16 at 09:28
  • `for ( ; ; ) { }` will also create an infinite loop. – codeflag Jun 10 '16 at 09:29
  • thanks all for the support help, i really appreciate it, you guys are sophisticated coders. – mas bro Jun 10 '16 at 12:43

2 Answers2

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

char *string = (char*) malloc(60, sizeof(char));

while (scanf("%s", string) > 0) {
   if (strcmp(string, "tidak") == 0) break;
}

let me know, if this was what you were looking for

matt93
  • 368
  • 4
  • 15
0

I just want to correct matt93 code with an explanation :

First, you malloc a fixed size, so that's not really useful. Plus, you didn't check if malloc doesn't return NULL. Moreover, your scanf is flawed, since we can easily do a buffer overflow. In addition, you doesn't flush stdin, so when the loop continu, you will have a behavior in your program that you don't really want.

Here the code that correct all of this :

#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;

    do {
        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 for STR_HELPER and STR macro. Don't forget to read scanf man if you have another question.

Community
  • 1
  • 1
Tom's
  • 2,448
  • 10
  • 22
  • omg, you are amazing sophisticated coders.. i own you big time.. i even did not think about buffer overflow, but sure now i will think about it. this is works ! – mas bro Jun 10 '16 at 12:41