Beginner help with strings 
Author Message
 Beginner help with strings

Hi all. Im a beginner trying to work on a code using strings and it
doesnt seem to work, the problem is somewhere in the if statement. Any
help to show me what I am doing wrong will be greatly appreciated.  Here
is part of the code.

main()
{
    char direction[15];

    printf("What direction do you want to go?-->");
    scanf(" %s", direction);

    if (direction == 'n')
       {
         printf("You went North.");
       }

    return 0;

Quote:
}



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings

Quote:

>main()

It's better to make that declaration explicit: int main(void)

Quote:
>    if (direction == 'n')

Look up strcmp() in your manual.

Gergo

--
I think that all good, right thinking people in this country are sick
and tired of being told that all good, right thinking people in this
country are fed up with being told that all good, right thinking people
in this country are fed up with being sick and tired.  I'm certainly
not, and I'm sick and tired of being told that I am.
                -- Monty python

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings
When you compare "direction to 'n' you are comparing a pointer to an int.
You either want "n" or use getchar() to take input.

Greg Martin

Quote:

>Hi all. Im a beginner trying to work on a code using strings and it
>doesnt seem to work, the problem is somewhere in the if statement. Any
>help to show me what I am doing wrong will be greatly appreciated.  Here
>is part of the code.

>main()
>{
>    char direction[15];

>    printf("What direction do you want to go?-->");
>    scanf(" %s", direction);

>    if (direction == 'n')
>       {
>         printf("You went North.");
>       }

>    return 0;
>}



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings
 : When you compare "direction to 'n' you are comparing a pointer to an int.

Yes.

 : You either want "n" or use getchar() to take input.

No. You can't do
  if (direction == "n")
which seems to be what you are suggesting (use strcmp() in this case).

Perhaps OP should try
  if (direction[0] == 'n' || direction[0] == 'N')
So if I enter "North", it will still work as intended.

 :>    char direction[15];
 :>    if (direction == 'n')

--
John Rixon



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings

Quote:


>No. You can't do
>  if (direction == "n")

Although I don't think it would make much sense to do it I do see why you
say you can't do it and obviously what you suggest below makes a lot more
sense. Doesn't (direction == "n") compare char arrays?
Quote:
>which seems to be what you are suggesting (use strcmp() in this case).

>Perhaps OP should try
>  if (direction[0] == 'n' || direction[0] == 'N')
>So if I enter "North", it will still work as intended.



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings

wrote in comp.lang.c:

Quote:



> >No. You can't do
> >  if (direction == "n")
> Although I don't think it would make much sense to do it I do see why you
> say you can't do it and obviously what you suggest below makes a lot more
> sense. Doesn't (direction == "n") compare char arrays?

> >which seems to be what you are suggesting (use strcmp() in this case).

> >Perhaps OP should try
> >  if (direction[0] == 'n' || direction[0] == 'N')
> >So if I enter "North", it will still work as intended.

<Jack>

No, arrays are not first class objects in C.  Given the definition
from the original post:

char direction[15];

the conditional test:

if (direction == "n")

will always be false because the array references decay into pointers,
so the actual comparison is:

if (&direction[0] == &"n"[0])

that is, compare the address of the first character in the array
direction with the address of the first character in the unnamed array
containing the string literal.

These two addresses will never be the same.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings

James a crit:

Quote:
> Hi all. Im a beginner trying to work on a code using strings and it
> doesnt seem to work, the problem is somewhere in the if statement. Any
> help to show me what I am doing wrong will be greatly appreciated.  Here
> is part of the code.

> main()
> {
>     char direction[15];

>     printf("What direction do you want to go?-->");
>     scanf(" %s", direction);

>     if (direction == 'n')
>        {
>          printf("You went North.");
>        }

>     return 0;
> }

  ------------------------------------------------------------------------

Here is another (maybe better) way to do this...

int main()
{
    char direction;

    printf("What direction do you want to go? (N/S/W/E) ");
    direction = getchar ();

    switch (direction)
    {
        case 'n':
        case 'N':
            printf ("You went North");
            break;
        case 's':
        case 'S':
            printf ("You went South\n");
            break;
        case 'w':
        case 'W':
            printf ("You went West\n");
            break;
        case 'e':
        case 'E':
            printf ("You went East\n");
            break;
        default:
            printf ("You went no where\n");
    }

    return 0;

Quote:
}

printf and getchar are both from conio.h I think

Tom



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings
Right ... I get it - the same as you can't copy arrays by array1 =  array2.
Thanks,
Greg.
Quote:


>wrote in comp.lang.c:



>> >No. You can't do
>> >  if (direction == "n")
>> Although I don't think it would make much sense to do it I do see why you
>> say you can't do it and obviously what you suggest below makes a lot more
>> sense. Doesn't (direction == "n") compare char arrays?

>> >which seems to be what you are suggesting (use strcmp() in this case).

>> >Perhaps OP should try
>> >  if (direction[0] == 'n' || direction[0] == 'N')
>> >So if I enter "North", it will still work as intended.

><Jack>

>No, arrays are not first class objects in C.  Given the definition
>from the original post:

>char direction[15];

>the conditional test:

>if (direction == "n")

>will always be false because the array references decay into pointers,
>so the actual comparison is:

>if (&direction[0] == &"n"[0])

>that is, compare the address of the first character in the array
>direction with the address of the first character in the unnamed array
>containing the string literal.

>These two addresses will never be the same.

></Jack>
>--
>Do not email me with questions about programming.
>Post them to the appropriate newsgroup.
>Followups to my posts are welcome.



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings

Quote:

> Hi all. Im a beginner trying to work on a code using strings and it
> doesnt seem to work, the problem is somewhere in the if statement. Any
> help to show me what I am doing wrong will be greatly appreciated.  Here
> is part of the code.
> main()
> {
>     char direction[15];
>     printf("What direction do you want to go?-->");
>     scanf(" %s", direction);

     if (direction == 'n')
        {
          printf("You went North.");
        }

     return 0;
 }

Direction is a pointer to character,if ypu want to know what it shows,you
must use dereferencing operator *
The correct if statement is:
  in C
    array[i] = *(array + i)
  so array[0] =*array

   if(*direction == 'n')

if user inputs a string contains more than one character,for example
  "North"
*direction='N'
*(direction + 1) ='o'
*(direction + 2) ='r'
  .
  .
  .
  .
i suggest you take a look array pointer relationship..



Wed, 24 Oct 2001 03:00:00 GMT  
 Beginner help with strings

...

Quote:
>Here is another (maybe better) way to do this...

>int main()
>{
>    char direction;

Make that int direction since getchar() can return values outside the range
of a char.

Quote:
>    printf("What direction do you want to go? (N/S/W/E) ");

You should have

     fflush(stdout);

here otherwise the prompt may not be displayed before the program pauses for
input.

Quote:
>    direction =3D getchar ();

You have some sort of encoding turned on in your newsreader/posting software
that turns = into =3D. I recommend that you turn it off.

Quote:
>    switch (direction)
>    {
>        case 'n':
>        case 'N':
>            printf ("You went North");
>            break;
>        case 's':
>        case 'S':
>            printf ("You went South\n");
>            break;
>        case 'w':
>        case 'W':
>            printf ("You went West\n");
>            break;
>        case 'e':
>        case 'E':
>            printf ("You went East\n");
>            break;
>        default:
>            printf ("You went no where\n");
>    }

>    return 0;
>}

>printf and getchar are both from conio.h I think

There is no conio.h in C. These functions are declared in <stdio.h>.

...

Quote:
><BR>{
><BR>&nbsp;&nbsp;&nbsp; char direction[15];

><P>&nbsp;&nbsp;&nbsp; printf("What direction do you want to go?-->");
><BR>&nbsp;&nbsp;&nbsp; scanf(" %s", direction);

><P>&nbsp;&nbsp;&nbsp; if (direction == 'n')
><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("You went North.");
><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

><P>&nbsp;&nbsp;&nbsp; return 0;
><BR>}</BLOCKQUOTE>

...

In fact you have HTML encoding turned on which causes all of this rubbish
to be included. Please turn it off ASAP.

--
-----------------------------------------


-----------------------------------------



Thu, 25 Oct 2001 03:00:00 GMT  
 Beginner help with strings
You cannot use == to compare strings, you have to use a function like
strcmp(). But since you are really only looking at the first character of
the string, you _can_ use == if you do it like this:
(first your code printed then new code)

Quote:
>     printf("What direction do you want to go?-->");
>     scanf(" %s", direction);

>     if (direction == 'n')
>        {
>          printf("You went North.");
>        }

printf("What direction bla bla?-->");
scanf("%s", direction);
if (direction[0] == 'n')
    printf ("bla bla North");

p.s. putting that space you have in scanf doesn't do what I think you think
it does.

from documentation on scanf format strings:
The format string contains regular characters which must match the input
exactly as well as a conversion specifiers, which begin with a percent
symbol.  Any whitespace in the format string matches zero or more of any
whitespace characters in the input.  Thus, a single space may match a
newline and two tabs in the input.  All conversions except `c' and `[' also
skip leading whitespace automatically.

...In other words, if you want to print a space, you have to do it in
printf(). If you want scanf to skip leading whitespace, don't worry it will
already do it.



Thu, 25 Oct 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Beginner needs help with strings, pointers, arrays

2. help : beginners simple string question

3. String assignments, please help (beginner)

4. Need help with char* to String conversion (beginner)

5. Beginner string question, help!

6. newbie needs help to strings, strings, strings

7. C-beginner needs advise for string function

8. Beginners Question about strings

9. Beginner String Question

10. string memory allocation - beginner

11. string conversion beginner question

12. Another Beginner string question....

 

 
Powered by phpBB® Forum Software