Thread: Fastest Typer Game

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    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);
         }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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');
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on fastest typer game
    By Jokkerz in forum C Programming
    Replies: 3
    Last Post: 03-24-2014, 03:24 PM
  2. Fastest DataBase
    By C_programmer.C in forum C Programming
    Replies: 21
    Last Post: 07-12-2013, 01:52 PM
  3. I'm looking for the fastest!
    By Yarin in forum Windows Programming
    Replies: 4
    Last Post: 11-07-2007, 03:30 PM
  4. Fastest way to do an SDL_BlitSurface?
    By manugarciac in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2007, 05:44 AM
  5. Fastest Display?
    By QuestionC in forum Windows Programming
    Replies: 1
    Last Post: 07-05-2002, 01:15 PM