void startpos(ROBOT *bot) { char dir; printf("Please input the desired start co-ordinates\n"); printf("X:"); scanf("%i", &bot->xpos); clear_stdin(); printf("Y:"); scanf("%i", &bot->ypos); clear_stdin(); printf("0 = North, 1 = East, 2 = South, 3 = West:\n"); scanf("%c", &dir); bot->dir = dir - '0'; }
int ask_question_int(char *q) { printf("%s", q); int num = 0; int tmp = scanf("%d", &num); while(tmp == 0) { puts("Använd siffror!"); clear_stdin(); tmp = scanf("%d", &num); } clear_stdin(); return num; }
int operations_menu(Variable *varArry, int arrySize){ char choice[3]; //Print the menu do{ //User interaction printf("%s", "\n\nChoose an operation\n1. Add\n2. Subtract\n3. Divide\n4. Multiply\n5. Equal\n6. Assing\n7. Back to Main Menu\nChoose:"); fgets(choice, sizeof choice, stdin); choice[strcspn(choice, "\n")] = 0; //Check if the user choice was valid if (strcmp(choice, "1") && strcmp(choice, "2") && strcmp(choice, "3") && strcmp(choice, "4") && strcmp(choice, "5") && strcmp(choice, "6") && strcmp(choice, "7")) if (strlen(choice) > 1) clear_stdin(); //Clear stdin printf("%s", "\n\nInvalid option!\n"); } while (strcmp(choice, "1") && strcmp(choice, "2") && strcmp(choice, "3") && strcmp(choice, "4") && strcmp(choice, "5") && strcmp(choice, "6") && strcmp(choice, "7")); //Run the coresponding function if (!strcmp(choice, "1")) add(varArry, arrySize); else if (!strcmp(choice, "2")) subtract(varArry, arrySize); else if (!strcmp(choice, "3")) divide(varArry, arrySize); else if (!strcmp(choice, "4")) multiply(varArry, arrySize); else if (!strcmp(choice, "5")) compare(varArry, arrySize); else if (!strcmp(choice, "6")) assign(varArry, arrySize); return false; }
void set_var_bool(Variable *varArry, int arrySpot){ String value = (String)malloc(ARRAYLENGHT * sizeof(char)); int flag; //Check if memory allocated properly. mem_check(value); do{ printf("%s", "\n\nGive the variables value (true/false):"); fgets(value, ARRAYLENGHT, stdin); //Check for valid lenght flag = false; if (value[strlen(value) - 1] == '\n' && strlen(value) > 1){ value[strcspn(value, "\n")] = 0; //Drop the \n from the input if (!isbool(value)) printf("%s", "\n\nThe value you gave is not a bool!\n\n"); } else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); if (strlen(value) > 1) clear_stdin(); //Clear stdin from extra input } } while (!isbool(value)); //Copy the value to the array and free memory strcpy(varArry[arrySpot].value, value); free(value); }
//Set a decimal variable void set_var_decimal(Variable *varArry, int arrySpot){ String value = (String)malloc(ARRAYLENGHT * sizeof(char)); int flag; //Check if memory allocated properly. mem_check(value); do{ //User interaction printf("%s", "\n\nGive the variables value (Decimal):"); fgets(value, ARRAYLENGHT, stdin); //Check for valid lenght flag = false; if (value[strlen(value) - 1] == '\n' && strlen(value) > 1) value[strcspn(value, "\n")] = 0; //Drop the \n from the input else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); if (strlen(value) > 1) clear_stdin(); //Clear stdin from extra input } //Check if user entered a decimal if (!isdecimal(value) || flag) printf("%s", "\n\nThe value you gave is not a decimal!\n\n"); } while (!isdecimal(value)); //Copy the decimal to our array and free the buffer strcpy(varArry[arrySpot].value, value); free(value); }
int human_choice(int pile) { clear_stdin(); printf("Enter 1-3\n"); scanf("%d", &pile); return pile; }
char ask_question_char(char *q, char *alt) //fråga { printf("%s [%s]\n", q, alt); char alf = 0; alf = getchar(); while (strchr(alt, alf) == NULL) { printf("Felaktig input '%c', försök igen om du vågar! [%s]\n", alf, alt); clear_stdin(); alf = getchar(); } clear_stdin(); return alf; }
int ask_question_int(void){ int num = 0; int temp = scanf("%d", &num); while(temp == 0){ puts("Use numbers!"); clear_stdin(); temp = scanf("%d", &num); } return num; }
int user_continue (void) { char input[MAX_LINE_LENGTH]; fgets (input, MAX_LINE_LENGTH, stdin); if ( strchr (input, '\n') == NULL ) { clear_stdin (); } return input[0] != 'q'; }
void read_instructions(ROBOT *bot) { char temp; clear_stdin(); while((temp = getchar()) != '\n'){ if(temp == 't'){ turn(bot); } else if(temp == 'm') { move(bot); }else { ; } } }
bool ask_yes_no(){ char *confirm = calloc(1, sizeof(char)); scanf("%s", confirm); if (*confirm == 'y' || *confirm == 'Y'){ free(confirm); return true; } if (*confirm == 'n' || *confirm == 'N'){ free(confirm); return false; } else{ puts("Not a valid answer, try again"); free(confirm); clear_stdin(); ask_yes_no(); } return true; }
//Create new variable menu. int create_var_menu(Variable *varArry, int *arrySize, int arrySpot){ String type = (String)malloc(ARRAYLENGHT * sizeof (char)); String name = (String)malloc(ARRAYLENGHT * sizeof (char)); int flag; //Check if memory allocated properly. mem_check(type); mem_check(name); //Get the variable's type do{ //User interaction. printf("%s", "\n\nGive the variable's type. Valid types are Integer, Decimal, String and Bool. To go back type <:"); fgets(type, ARRAYLENGHT, stdin); flag = false; if (type[strlen(type) - 1] == '\n' && strlen(type) > 1){ type[strcspn(type, "\n")] = 0; //Drop the \n from fgets input strtolower(type); //Convert the string to lowercase for ease of use. //Check for back input if (!strcmp(type, "<")){ return false; } //Check for valid input. if (strcmp(type, "integer") && strcmp(type, "decimal") && strcmp(type, "string") && strcmp(type, "bool")){ printf("%s", "\n\nInvalid type!\n\n"); } } else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); if (strlen(type) > 1) clear_stdin(); //Clear stdin from extra input } } while (strcmp(type, "integer") && strcmp(type, "decimal") && strcmp(type, "string") && strcmp(type, "bool") || flag); //Loop until user decides to cooperate. //Get the variable's name. do{ //User interaction. printf("%s", "\n\nGive a name for the variable. The max lenght is 255 characters. It can't contain whitespaces or symbols, except underscores (_):"); fgets(name, ARRAYLENGHT, stdin); flag = false; //Check for valid lenght if (name[strlen(name) - 1] == '\n' && strlen(name) > 2){ name[strcspn(name, "\n")] = 0; //Drop the \n from fgets input. //Check for valid input if (strpbrk(name, " !@#$%^&*()-+=\\|[]{};\':\",./<>?") != NULL){ printf("%s", "\n\nInvalid name!\n\n"); } else if (var_exist(varArry, name, *arrySize)){ printf("%s", "\n\nA variable with that name already exists!\n\n"); } } else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); if (strlen(name) > 1) clear_stdin(); //Clear stdin from extra input } } while (flag || strpbrk(name, " `~!@#$%^&*()-+=\\|[]{};\':\",./<>?") != NULL || var_exist(varArry, name, *arrySize)); //Loop until valid. //Copy the name and the type in the variables struct array. strcpy(varArry[arrySpot].type, type); strcpy(varArry[arrySpot].name, name); //Free the memory we don't need anymore. free(name); free(type); //Run the function corresponding to the entered type. if (!strcmp(varArry[arrySpot].type, "integer")){ set_var_int(varArry, arrySpot); } else if (!strcmp(varArry[arrySpot].type, "decimal")){ set_var_decimal(varArry, arrySpot); } else if (!strcmp(varArry[arrySpot].type, "string")){ //Loop until the lenght is correct. do{ //User interaction. For strings we don't need any valid input checks. printf("%s", "\n\nGive the variables value (String, 255 characters max):"); fgets(varArry[arrySpot].value, ARRAYLENGHT, stdin); flag = false; if (varArry[arrySpot].value[strlen(varArry[arrySpot].value) - 1] == '\n') varArry[arrySpot].value[strcspn(varArry[arrySpot].value, "\n")] = 0; //Drop the \n from fgets input. else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); clear_stdin(); //Clear stdin from extra input } } while (flag); } else{ set_var_bool(varArry, arrySpot); } return true; }