/* * 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); }
int main() { cout << "Welcome to the TransAsia Airways Flight Reservation System\n"; ReservationDatabase reservationDatabase; FlightSchedule flightSchedule; MakingReservation makingReservation( reservationDatabase, flightSchedule ); ViewingCanceling viewingCanceling( reservationDatabase, flightSchedule ); int choice; while ( ( choice = enterChoice() ) != 4 ) { switch ( choice ) { case 1: makingReservation.execute(); break; case 2: viewingCanceling.viewingReservation(); break; case 3: viewingCanceling.cancelingReservation(); break; default: cerr << "Incorrect choice" << endl; break; } } cout << endl; system( "pause" ); }
int main() { FILE *cfPtr; /* credit.dat file pointer */ int choice; /* user's choice */ /* fopen opens the file; exits if file cannot be opened */ if ( ( cfPtr = fopen( "credit.dat", "rb+" ) ) == NULL ) { printf( "File could not be opened.\n" ); } /* end if */ else { /* enable user to specify action */ while ( ( choice = enterChoice() ) != 6 ) { switch ( choice ) { /* create text file from record file */ case 1: textFile( cfPtr ); break; /* update record */ case 2: updateRecord( cfPtr ); break; /* create record */ case 3: newRecord( cfPtr ); break; /* delete existing record */ case 4: deleteRecord( cfPtr ); break; /* sum up balances of valid records and record sum in text file */ case 5: sumRecords( cfPtr ); break; /* display message if user does not select valid choice */ default: printf( "Incorrect choice\n" ); break; } /* end switch */ } /* end while */ fclose( cfPtr ); /* fclose closes the file */ } /* end else */ return 0; /* indicates successful termination */ } /* end main */
/* * This displays the main menu text. */ int main() { printf("Welcome to Reversi!\n"); printf("===================\n"); printf("Select an option:\n"); printf("1. Play a game\n"); printf("2. Quit the program"); enterChoice(); return EXIT_SUCCESS; }
int main() { FILE *fp; int choice; int i=0; if ((fp = fopen("books.dat","rb+"))==NULL) //create binary file with blank records if file pointer returns NULL { printf("File does not exist\nCreating Binary File with blank records..."); struct book blankBook = { "","",0,0,0.0,0.0}; fp = fopen("books.dat","wb+"); for ( i = 1; i <= 100; i++ ) //write 100 blank book structures { fwrite( &blankBook, sizeof( struct book ), 1, fp ); } fclose ( fp ); main(); } else //menu { while ( ( choice = enterChoice() ) != 5 ) { switch ( choice ) { case 1: writeToTextFile( fp ); break; case 2: updatePrice( fp ); break; case 3: newBook( fp ); break; case 4: deleteBook( fp ); break; default: printf( "Incorrect choice\n" ); break; } } fclose( fp ); } return 0; }
int main( void ) { FILE *cfPtr; // credit.dat file pointer unsigned int choice; // user's choice // fopen opens the file; exits if file cannot be opened if ( ( cfPtr = fopen( "/home/abubakr/test.txt", "rb+" ) ) == NULL ) { puts( "File could not be opened." ); } // end if else { // enable user to specify action while ( ( choice = enterChoice() ) != 5 ) { switch ( choice ) { // create text file from record file case 1: textFile( cfPtr ); break; // update record case 2: updateRecord( cfPtr ); break; // create record case 3: newRecord( cfPtr ); break; // delete existing record case 4: deleteRecord( cfPtr ); break; // display message if user does not select valid choice default: puts( "Incorrect choice" ); break; } // end switch } // end while fclose( cfPtr ); // fclose closes the file } // end else } // end main
/* * This function verifies if the user wants to play, quit and the outcome * of the game when it is finished. */ int playYesOrNo(int i) { if (i == 1){ Player first,second, * winner; /* * This triggers the game to start and when finished winner will be returned a * value, either a single winner or if it was a draw. */ winner = playGame(&first, &second); /* * If it was a draw winner would be null otherwise the details are passed through * and a winner is announced. */ if (winner == NULL){ printf("\nThe game was a draw\n\n"); main(); }else{ printf("\n%s won with a score of %d\n\n", winner->name, winner->score); main(); } } else if (i == 2){ printf("\nGoodbye.\n\n"); } else { enterChoice(); } return EXIT_SUCCESS; }