mercredi 8 novembre 2017

if statement with stringcompare in c

Hello im doing my homework and I'm supposed to write a short c code where I get a random number between 1 - 6 if i type "random". If I type in "exit" or "quit" the program must end. "quit" and "exit" works but nothing happens when I enter "random".

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

int main() {
printf("enter your command");
char input[100];
fgets (input, 100, stdin);

if (strcmp(input, "quit") == 0){
   exit(0); 
} else if (strcmp(input, "exit") == 0) {
   exit(0);
} else if (strcmp(input, "random") == 0) {
   srand(time(NULL));
   int random_number = rand() %7;
   printf("%d\n",random_number);     
}
return 0;
}

nter code here

Aucun commentaire:

Enregistrer un commentaire