/* START REUSED CODE */ int getString(char* string, unsigned length, char* prompt) { int finished = FALSE; char tempString[ 200 + EXTRA_SPACES]; /*Continue to interact with the user until the input is validated */ do { /*customized prompt */ printf("%s", prompt); /*Accept input */ fgets(tempString, length + 2, stdin); /*A String without a '/n' is too long */ if (tempString[strlen(tempString) -1 ] != '\n') { printf("Input was too long.\n"); readRestOfLine(); } else { finished = TRUE; } } while (finished == FALSE); /* Overwrite the newline character with '\0' */ tempString[strlen(tempString) - 1] = '\0'; /*copy tempstring to string */ strcpy(string, tempString); return TRUE; }
/**************************************************************************** * Function guessLetter() prompts the user to enter one letter. The function * maintains an array of guessed letters. The function returns GOOD_GUESS * or BAD_GUESS depending on whether or not the letter is in the word. ****************************************************************************/ int guessLetter(char* word, int* guessedLetters) { char charGuess; int arrIndex; printf("\n%s", "Please enter one letter: "); charGuess = fgetc(stdin); readRestOfLine(); arrIndex = charGuess - BASE; if(guessedLetters[arrIndex] != 0) { printf("\n%s%c%s\n", "The letter ", charGuess, " has already been guessed."); return BAD_GUESS; } else { guessedLetters[arrIndex] = charGuess; if(inWord(word, charGuess)) { return BAD_GUESS; } else { return GOOD_GUESS; } } return EXIT_SUCCESS; }
void deleteParticipant(struct Ordered_container* rooms, struct Ordered_container* people) { int roomNum; int time; char lastnameBuffer[MAX_STRING_LENGTH]; struct Room* room; struct Meeting* meeting; struct Person* person; if ((roomNum = scanRoomNum()) == -1) { return; } if (!(room = findRoomByNum(rooms, &roomNum))) { printErrNoRoom(); return; } if ((time = scanMeetingTime()) == -1) { return; } if (!(meeting = findMeetingByTime(get_Room_Meetings(room), &time))) { printErrNoMeeting(); return; } SAFESCANF(lastnameBuffer); if (!(person = findPersonByLastname(people, lastnameBuffer))) { printErrNoPerson(); } if (remove_Meeting_participant(meeting, person)) { readRestOfLine(); printf("This person is not a participant in the meeting!\n"); } else { printf("Participant %s deleted\n", lastnameBuffer); } }
void getBrandName(char *input) { char myString[PRODUCT_BRAND_MAX + EXTRA_SPACES]; int finished = FALSE; int length; do { printf("\nEnter a brand name (1-20 characters):\n"); myString[0] = '\0'; fgets(myString, PRODUCT_BRAND_MAX + EXTRA_SPACES, stdin); length = (int)strlen(myString) - 1; if(myString[0] == '\n') { strcpy(input, myString); finished = TRUE; } else if (length < STRING_MIN_CHARS || length > PRODUCT_BRAND_MAX) { printf("Invalid Input.\n"); if (myString[length] != '\n') { readRestOfLine(); } } else { myString[length] = '\0'; strcpy(input, myString); finished = TRUE; } } while (!finished); }
int getCoins(VendingMachineType *vm){ char myString[PRODUCT_PRICE_MAX + EXTRA_SPACES]; int value; int finished = FALSE; char *ptr; int i; do { value = -1; fgets(myString, PRODUCT_PRICE_MAX + EXTRA_SPACES, stdin); if(myString[0] == '\n') { finished = TRUE; } else if (myString[strlen(myString) - 1] != '\n') { printf("Invalid Input.\n"); readRestOfLine(); } else { myString[strlen(myString) - 1] = '\0'; value = (int) strtol(myString, &ptr, 10); for(i=0; i<DISTINCT_COINS; i++){ if(value == vm->coins[i].value){ vm->coins[i].qty++; return value; } } printf("Invalid coin value. Please Enter a valid coin value: "); } } while (!finished); return value; }
/* * This is function allows the user to input what they want to do, * either play or exit the game. */ void enterChoice(){ char userChoice[3]; int input; /* * This is prompts for the user to input their choice, */ printf("\nPlease enter your choice: "); if (fgets(userChoice, 10, stdin) == NULL){ enterChoice(); } if(userChoice[strlen(userChoice)-1]!='\n') { printf("Input was too long, try again. \n\n"); readRestOfLine(); } /* * This is allows for the entered number to be passed into * the next function in order to see if the user wants to play or not. */ userChoice[strlen(userChoice)-1]=0; sscanf(userChoice, "%d", &input); playYesOrNo(input); }
void deleteGroup(struct Ordered_container* rooms, struct Ordered_container* people) { if (OC_apply_if(rooms, (OC_apply_if_fp_t)roomHasMeeting)) { readRestOfLine(); printf("Cannot clear people list unless there are no meetings!\n"); } else { OC_apply(people, (OC_apply_fp_t)destroy_Person); } }
int scanMeetingTime(void) { int time; if (scanf("%d", &time) != 1) { printErrInteger(); return -1; } else if (!((time >=9 && time <=12) || (time >=1 && time <= 5))) { readRestOfLine(); printf("Time is not in range!\n"); return -1; } return time; }
int scanRoomNum(void) { int roomNum; if (scanf("%d", &roomNum) != 1) { printErrInteger(); return -1; } else if (roomNum <= 0) { readRestOfLine(); printf("Room number is not in range!\n"); return -1; } return roomNum; }
char *MyGetString(char *str, int strlen) { if (fgets(str, strlen, stdin)!=NULL){ char *eot; char *newline = strchr(str,'\n'); if (newline!=NULL) *newline = '\0'; else readRestOfLine(); eot = strchr(str, 4); if (eot!=NULL) return NULL; return Trim(str); } return NULL; }
void deleteIndividual(struct Ordered_container* people, struct Ordered_container* rooms) { char lastnameBuffer[MAX_STRING_LENGTH]; struct Person* person; SAFESCANF(lastnameBuffer); if (!(person = findPersonByLastname(people, lastnameBuffer))) { printErrNoPerson(); return; } if (OC_apply_if_arg(rooms, (OC_apply_if_arg_fp_t)findPersonRoomMeeting, person)) { readRestOfLine(); printf("This person is a participant in a meeting!\n"); } else { OC_delete_item(people, findPersonByLastname(people, lastnameBuffer)); destroy_Person(person); printf("Person %s deleted\n", lastnameBuffer); } }
int getUserInt(char * message, int maxSize, int minRange, int maxRange) { char *myString = malloc(sizeof(maxSize)); int value; int finished = FALSE; char *ptr; do { printf("\n%s\n", message); value = -1; fgets(myString, (maxSize + EXTRA_SPACES), stdin); if(myString[0] == '\n') { finished = TRUE; } else if (myString[strlen(myString) - 1] != '\n') { printf("Invalid Input.\n"); readRestOfLine(); } else { myString[strlen(myString) - 1] = '\0'; value = (int) strtol(myString, &ptr, 10); if (value < minRange || value > maxRange || (int)*ptr != 0) { printf("Invalid Input.\n"); } else { finished = TRUE; } } } while (!finished); free(myString); return value; }
void printErrNoPerson() { readRestOfLine(); printf("No person with that name!\n"); }
void ParseFile_Recurse(FILE* fd, Settings& settings,int& lineCount){ std::string key,value; char lastCharParsed; do{ lastCharParsed = getKey(fd,key); removeTrailingSpaces(key); if(lastCharParsed == '=' && key.size()>0){ lastCharParsed = getBody(fd,value); removeTrailingSpaces(value); settings.values[key] = value; //Now lets add a new nested tree of if statements //sorry future me about this mess if(lastCharParsed=='#'){ readRestOfLine(fd); lineCount++; }else if(lastCharParsed=='}'){ return; //unwind the stack to be done with this group }else if(lastCharParsed=='\n'){ //nothing special lineCount++; }else if(lastCharParsed=='{'){ printf("ERROR: CONFIG:%d: Opening new group ( { ) while specifing value\n",lineCount); }else if(lastCharParsed=='='){ printf("ERROR: CONFIG:%d: Value specifing ( = ) while specifing value\n",lineCount); }else if( !feof(fd) ){ printf("ERROR: CONFIG:%d: Reached EOF Unexpectedly (value parsing)\n",lineCount); } }else if(lastCharParsed == '{' && key.size()>0){ //found a named group, so recurse down to get everything inside of it ParseFile_Recurse(fd,settings.groups[key],lineCount); }else if(lastCharParsed == '}' && key.size()==0){ //found that we are closing the group we are currently in return; // unwind the stack }else if(lastCharParsed == '\n' && key.size()==0){ //just ignore blank lines lineCount++; }else if(lastCharParsed == '#' && key.size()==0){ //ignore lines that only have comments readRestOfLine(fd); lineCount++; } // end of good states, following are bad states else if( lastCharParsed=='#' && key.size()>0 ){ readRestOfLine(fd); lineCount++; printf("ERROR: CONFIG:%d: found a comment token ( # ) while parsing label\n",lineCount); }else if( lastCharParsed=='=' && key.size()==0 ){ readRestOfLine(fd); lineCount++; printf("ERROR: CONFIG:%d: found a ( = ) token with an empty label\n",lineCount); }else if( lastCharParsed=='}' && key.size()>0 ){ printf("ERROR: CONFIG:%d: found a ( } ) token with a non-empty label\n",lineCount); }else if( lastCharParsed=='\n' && key.size()>0 ){ printf("ERROR: CONFIG:%d: reached end of line with a non-empty label\n",lineCount); lineCount++; }else if( lastCharParsed=='{' && key.size()>0 ){ printf("ERROR: CONFIG:%d: found a ( { ) token with an empty label\n",lineCount); }else if( feof(fd) ){ if(key.size()>0) printf("ERROR: CONFIG:%d: Reached EOF Unexpectedly (key parsing)\n",lineCount); //else no-one cares as we are not losing any information } }while(!feof(fd)); }
void printErrNoRoom(void) { readRestOfLine(); printf("No room with that number!\n"); }
void printErrInteger(void) { readRestOfLine(); printf("Could not read an integer value!\n"); }
void printErrNoMeeting(void) { readRestOfLine(); printf("No meeting at that time!\n"); }
/*Print out error when command is not recognized. * */ void printErrUnrecCmd() { readRestOfLine(); printf("Unrecognized command!\n"); }
void printErrParticipantExist(void) { readRestOfLine(); printf("This person is already a participant!\n"); }
void printErrMeetingExist(void) { readRestOfLine(); printf("There is already a meeting at that time!\n"); }
int main(int argc, char* argv[]) { GJCType gjc; int initFlag, dataFlag, exitFlag=FALSE; char c[MAX_OPTION_INPUT+EXTRA_SPACES]; /* Initialise the Gloria Jean's Coffee system to a safe empty state. */ initFlag = systemInit(&gjc); /* Populate the Gloria Jean's Coffee system with data from the data files. */ /* Uncomment this line when you are ready to use command line arguments:*/ if(argv[1]==NULL||argv[2]==NULL){ printf("No argument found\n"); exit(EXIT_SUCCESS); } dataFlag = loadData(&gjc, argv[1], argv[2]); /* Testing to see if both systemInit(.) and loadData(.) are ok */ if (initFlag == FAILURE || dataFlag == FAILURE){ exit(EXIT_FAILURE); } /* Interactive menu providing user with access to the 9 menu options */ while(exitFlag==FALSE){ printf("Main Menu:\n"); printf("(1) Hot Drinks Summary\n"); printf("(2) Cold Drinks Summary\n"); printf("(3) Detailed Menu Report\n"); printf("(4) Add Menu Category\n"); printf("(5) Delete Menu Category\n"); printf("(6) Add Menu Item\n"); printf("(7) Delete Menu Item\n"); printf("(8) Save & Exit\n"); printf("(9) Abort\n"); printf("Select your option (1-9):"); fgets(c,MAX_OPTION_INPUT+EXTRA_SPACES,stdin); if(c[0]=='\n'){ printf("Invalid input\n"); }else if(stringIsInRange(c,MAX_OPTION_INPUT)==FALSE){ readRestOfLine(); printf("Invalid input\n"); }else{ stripNewLine(c); if(strcmp(c,"1")==0){ displaySummary(&gjc,HOT); }else if(strcmp(c,"2")==0){ displaySummary(&gjc,COLD); }else if(strcmp(c,"3")==0){ }else if(strcmp(c,"4")==0){ }else if(strcmp(c,"5")==0){ }else if(strcmp(c,"6")==0){ }else if(strcmp(c,"7")==0){ }else if(strcmp(c,"8")==0){ }else if(strcmp(c,"9")==0){ exitFlag=TRUE; } } } /* Deallocate all dynamically allocated memory. */ systemFree(&gjc); exit(EXIT_SUCCESS); }
void printErrPersonExist(void) { readRestOfLine(); printf("There is already a person with this last name!\n"); }
void printErrRoomExist(void) { readRestOfLine(); printf("There is already a room with this number!\n"); }