-1

I am trying make this nested loop work.. for some reason the code does not go into the second loop..it keeps asking for a letter to enter so it does not go into the code with the k counter... and ask me 10 times to enter a letter

thanks.

 char letter;
 int proposition;
 int i,k;
 char dashes[50];
 proposition=10;
 for (i=0;i<proposition;i++){
    printf("enter letter to guess %s\n");
    scanf("%s", &letter);
     for(k=0;k<ret;k++){
        if (tab[k] == letter){
            dashes[k]= letter;
            printf("%s\n", dashes);
        }
    }
  }
danidar
  • 179
  • 2
  • 10

4 Answers4

1

scanf("%s", &letter);

The problem is that, you are asking for a string, passing it to an adress. You either want to do

scanf("%c", &letter);or change it to a int getchar( void );.

Edit #1:

I'd recommend against using int scanf (const char * format, ...);. For more information you can read here Disadvantages of scanf.

Edit #2:

scanf("%s", letter); is technically possible, however, as you are only asking for one letter it is wrong to do so. Also remember that strings ends with \0 so make sure you reserve room for that as well.

Community
  • 1
  • 1
Emz
  • 1,280
  • 1
  • 14
  • 29
0

try this

char letter[2];
int proposition;
int i,k;
char dashes[50]={0};
memset(dashes, '-', strlen(tab));
proposition=10;
for (i=0;i<proposition;i++){
   printf("enter letter to guess %%s\n");
   scanf("%1s", letter);
    for(k=0;k<ret;k++){
       if (tab[k] == *letter){
           dashes[k]= *letter;
           printf("%s\n", dashes);
       }
   }
 }

may be sample

#include <stdio.h>
#include <string.h>

int main(void){
    char *tab = "foobar";
    int ret = strlen(tab);
    char letter[2];
    int proposition;
    int i,k, match=0;
    char dashes[50]={0};
    memset(dashes, '-', ret);
    proposition=10;
    for (i=0;i<proposition;i++){
        if(match == ret)
            break;
        printf("enter letter to guess\n");
        scanf("%s", letter);
        for(k=0;k<ret;k++){
            if (tab[k] == *letter){
                dashes[k]= *letter;
                ++match;
            }
        }
        printf("%s\n", dashes);
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • i need the dashes to be intialized to display only "-" so I enter a string that goes to the tab variable. eg i enter the world foobar the variable tab will be foobar. the variable dashes would display "-----" then i enter a letter if the letter matches one of the characters in variable tab i replace it on the variable dashes. eg i enter o then the variable dashes becomes -oo--. – danidar Nov 30 '14 at 20:19
  • @danidar initialize `dashes` by `memset`. – BLUEPIXY Nov 30 '14 at 20:24
  • I did this to initialize the string to - : `for (k=0;k – danidar Nov 30 '14 at 20:51
0

The problem seems to be related to your ret variable.
If you haven't initialized it, the value will be undefined and you won't know if the loop condition will be true.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
0

Use %c instead of %s.

%s is get the string up to the white space character. So this is the reason it asking the ten character from the user.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31