I've got to write a program where it replaces a letter with a dash and then asks the user to enter the letter that should be their. I've written a program and It works the first time around and then prints out two lines.

Where have I gone wrong ??

#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

#include<string.h>

#include<time.h>



int main()

{

    char array[][10]= {"sam", "ben", "sally", "molly", "jake", "samuel", "table", "chair", "computer", "mouse"};

    char answer;

    char input;

    int  r1=0;

    int  r2=0;

    int  length=0;

    int  loop=0;

    int  loop2=0;

    int  score=0;

    

    srand (time(NULL));

    

    for (loop=0;loop!=10;loop++)

    {

        r1 = rand() %9;

        length = strlen(array[r1]);

        r2 = rand() %length;

        answer=array[r1][r2];

        printf("Type the missing letter of word %d\n",loop);

        

        for (loop2=0;loop2!=r2;loop2++)

        {

              printf("%c",array[r1][loop2]);

        }

        

        printf("-");

        

        while(loop2!=length)

        {

              printf("%c",array[r1][loop2+1]);

              loop2++;

        }

        

        if(loop2==length)

        {

              scanf("%c",&input);

              if (input==answer) score++;

              else score--;

        }

        

    }

    printf("your score is %d",score);

    system("pause");

}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>



int main()

{

    char array1[][10]= {"sam", "ben", "sally", "molly", "jake", "samuel", "table", "chair", "computer", "mouse"};

    char answer;
    char input;
    int  r1=0;
    int  r2=0;
    int  length=0;
    int  loop=0;
    int  loop2=0;
    int  score=0;
   
    srand (time(NULL));    

    for (loop=0;loop!=10;loop++) {
        r1 = rand() % 9;
        length = strlen(array1[r1]);
        r2 = rand() %length;
		answer = array1[r1][r2];

       
	for (loop2 = 0; loop2 < length; loop2++) {
		if (loop2 == r2) 
			printf("-");
		else  
			printf("%c",array1[r1][loop2]);
        }
               	
	printf("Type the missing letter of word %d\n",loop);	
      	scanf("%c", &input);

        if (input == answer) 
		score++;
	else 
		score--;
        
	getchar();
    }

    printf("your score is %d",score);
    getchar();	

    return 0;

}

Just put a gethchar() inside the loop.
Also, i modified the code a little. The two loops inside were not needed.

Edited 6 Years Ago by myk45: n/a

Comments
nice work :)
This question has already been answered. Start a new discussion instead.