Chào các bạn daynhauhoc.com
- Khi mình input lựa chọn vào thì chương trình tự động lặp lại input, debug thì không có lỗi nào cả.
* File: main.c
* Author: luuhoangvu
*
* Created on June 7, 2019, 9:35 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double earnings;
FILE *f;
/*
*
*/
void showMenu() {
printf("----------------\n");
printf("1) Play Game.\n");
printf("2) Save Game.\n");
printf("3) Cash Out.\n");
printf("----------------\n");
}
int checkChoice(int *pcheckChoice) {
int checking;
int letter;
do {
checking = scanf("%d%c", pcheckChoice, &letter);
if (checking == 2 && letter == '\n') {
return *pcheckChoice;
} else
printf("Input again: ");
fpurge(stdin);
} while (checking != 2 || letter != '\n');
}
void playGame() {
int digits1, digits2, digits3;
digits1 = rand() % 10;
digits2 = rand() % 10;
digits3 = rand() % 10;
printf("=> The slot machine: %d%d%d", digits1, digits2, digits3);
int sameCount;
sameCount = (digits1 == digits2) + (digits2 == digits3) + (digits1 == digits3);
earnings -= 0.25;
if (sameCount == 0) {
printf("You don't win anything!\n");
}
if (sameCount == 1) {
printf("You win 50 cent!\n");
earnings += 0.5;
}
if (sameCount == 3) {
printf("You win the big prize!!!!\n");
earnings += 10.0;
}
}
void saveGame() {
f = fopen("savedMoney.txt", "w");
fprintf(f, "%.2lf", earnings);
fclose(f);
printf("Your money had saved!\n ");
}
void cashOut() {
printf("Thank you for playing! Your money $%.2lf", earnings);
}
void restoreMoney() {
f = fopen("savedMoney.txt", "r");
if (f == NULL) {
earnings = 10.0;
} else
fscanf(f, "%lf", &earnings);
fclose(f);
}
void imPlement() {
int choice;
do {
if (earnings == 0) {
printf("You run out of money, please deposit more money!!!\n");
break;
}
printf("You have $%.2lf\n", earnings);
showMenu();
printf("Choose one of the following options menu\n");
checkChoice(&choice);
switch (choice) {
case 1: playGame();
break;
case 2: saveGame();
break;
case 3: cashOut();
break;
}
} while (choice != 3);
getchar();
getchar();
}
int main(int argc, char** argv) {
srand(time(0));
restoreMoney();
imPlement();
restoreMoney();
return 0;
}


83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?