scanf with char 2 dimensional array

Hello all,

I am trying to print on the screen 3 char strings after entering numbers 1 to 3, each number stands for on of the expressions in the 2 dim array. Entering 1 results the first expression 4 times or so... and almost nothing happens after entering 2 and 3. Can anybody help to solve it?

[code]

#include

int main(void)
{
int nRet=0;

char str[3][22] = {"My name is attis.",
"My name is Matsuyama.",
"This is London."};
int i;
int j;
int k;

printf("Enter a number from 1 to 3!");
scanf(" %d", &k);

for(i=0; i<=3; i++) {
for(j=0; j<22; j++) {
if( k == 1 )
printf( "%c", str[0][j]);
}
if( k == 2 ) {
printf( "%c", str[1][j]);
}
if( k == 1 ) {
printf( "%c", str[2][j]);
}
}
return nRet;

}

[/code]

Comments

  • :
    [code]:
    for(i=0; i<=3; i++) {
    [/code]:

    Now count on your fingers (you do have them don't you) from 0 to and including 3. How many is that? Your array has 3 rows but that loop above is attempting to access a 4th row, which if you can count correctly, does not exist.

    And why are you doing this the hard way? You can print the entire string with one line of code instead of printing each character one at a time.
    [code]
    printf("%s
    ", str[i]);
    [/code]

    Conversation should be pleasant without scurrility, witty without affectation, free without indecency, learned without conceitedness, novel without falsehood.
    William Shakespeare
  • : :
    : [code]: :
    : for(i=0; i<=3; i++) {
    : [/code]: :
    :
    Now count on your fingers (you do have them don't you) from 0 to and
    including 3. How many is that? Your array has 3 rows but that loop
    above is attempting to access a 4th row, which if you can count
    correctly, does not exist.

    And why are you doing this the hard way? You can print the entire
    string with one line of code instead of printing each character one
    at a time.
    [code]:
    printf("%s
    ", str[i]);
    [/code]:

    and looking even closer at your program you won't need any of those loops
    [code]
    #include <stdio.h>

    int main(void)
    {
    int nRet=0;

    char str[3][22] = {"My name is attis.",
    "My name is Matsuyama.",
    "This is London."};
    int k;

    printf("Enter a number from 1 to 3!");
    scanf("%d", &k);
    if( k > 0 && k < 4)
    printf("%s
    ", str[k-1]);
    return 0;
    }

    [/code]


  • : [code]:
    : #include
    :
    : int main(void)
    : {
    : int nRet=0;
    :
    : char str[3][22] = {"My name is attis.",
    : "My name is Matsuyama.",
    : "This is London."};
    : int k;
    :
    : printf("Enter a number from 1 to 3!");
    : scanf("%d", &k);
    : if( k > 0 && k < 4)
    : printf("%s
    ", [b]str[k-1][/b]);
    : return 0;
    : }
    :
    : [/code]:
    :

    [color=Blue]Is it str[k-1]? or should we write str[0][k-1]?[/color]
    :

    [code][blue]============================[/blue]
    [red]THANK YOU AND GODBLESS !!!
    from playagain[/red]
    [blue]============================[/blue][/code]

  • [color=Blue]Is it str[k-1]? or should we write str[0][k-1]?[/color]

    It should be exactly like I posted it. [b]str[0][k-1][/b] only references the [b]k-1[/b]th character in the 0th row of the array. That is not what you want.
    Conversation should be pleasant without scurrility, witty without affectation, free without indecency, learned without conceitedness, novel without falsehood.
    William Shakespeare

  • Dear experts,

    Thank you very much for the sophisticated solutions for my problem. It really looks magic for me as I am in my fifth lesson in c programming. This may be the reason why I would need the step by step methond which is probably longer but makes me understand the "what and why".

    By the way, did my post included the code tag? I inserted [code] [/code] but it did not become gray...

    thanks again for any further help

    attis
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories