-
Fastest Typer Game
I tried the program and this appeared many times (warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat])
My goal is to make a game where a player is required to type a set of words from a file but the words should appear only one by one and the user should type every time the word is displayed. I'm also trying to make a stopwatch timer that starts as soon as the player chooses the option start game but I think it's hard so my main goal for now is to make the game without the timer. The function in the bottom should make a file which contains highscores.
Code:
#include<stdio.h>
#include<time.h>
int main(){
FILE *fp;
char go, player[30];
char word[15],typed[15];
clock_t start_t, end_t, total_t;
int choice, i=0;
printf("Welcome to the fastest typer game!\n");
printf("Input name of player:");
scanf("%s",player);
printf("Are you ready?\n\t1. Play Game\n\t2. High Scorers\n\t3. Exit");
scanf("%i",&choice);
switch (choice){
case 1: start_t = clock();
for(i =0;i<10000000; i++)
{
}
//File Read
fp=fopen("words.txt","r");
do{
fscanf(fp,"%s \n", word[i]);
do{
printf("Type the word:\n %s", word[i]);
scanf("%s",typed[i]);
}while(typed[i]!=word[i]);
i++;
}while(!feof(fp));
fclose(fp);
end_t = clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time: %f\n", total_t );
highScore(player);
case 2: fp=fopen("highscore.text","r");
fscanf(fp,"%s \n", player);
printf("%s", player);
case 3: break;
default: printf("Not in the choices, try again: ");
scanf("%i",choice);
}
}
void highScore(char player){
//File Write
FILE *fp;
fp=fopen("highscore.text","a");
fprintf(fp,"%s\n", player);
fclose(fp);
}
-
You could look at the return from scanf() to see if the user had entered any letters (or check the length of the 'typed' array) before moving on.
If the user has not typed the same number of characters as in the word then do you need to test if they are the same?
Beware of the bugs this could introduce...
You cant compare 2 char arrays with 'if(words == typed)' you can write a function that does the comparison use something like strcmp()
Only read the whole word from the file once.
Always initialize your variables.
etc....
>>format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
%s expects a 'char*' not a 'char'
If you declare 'char words[15]' then 'words' is a 'char*' (as it means the whole char array) while 'words[1]' is the ASCII character in the second element of the words char array. (every ASCII character has a number).
Also note that char words[15] can only hold 14 letters as the last element (words[14]) will be a null terminator (ASCII code 0 or '/0')
If you only want to print out or read in a single character then use '%c'
Code:
//note code written from memory and will contains bugs...
char typed[15] = {0};//set the array to empty
char words[15] = "ABCDEFGH";
printf("This is a character [%c] while this is an array [%s].", words[0], words);
//note using single quotes
printf("This is a character [%c] and this is its ASCII code [%d].", 'A', 'A');