Thread: why?

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    Post why?

    hi
    im pretty new to c programming.

    so would please anyone of you tell me whats wrong with this code?

    #include <stdio.h>
    #include <stdlib.h>

    main ()
    {

    printf ("type 'hello'.\n");
    scanf ("%ld");
    }
    {
    if scanf == 'hello';
    printf ("correct\n");
    else
    printf ("wrong!\n");
    getch();
    }


    the purpose of this code is very simple.

    the user types in the word 'hello', if he does so the output 'correct' should appear.
    in any other case there should pop a 'wrong'.

    thanks for the help
    cya threadhead

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    just about everything is wrong with it.

    here it is fixed for you. Compare my version to yours and see your errors...
    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
    char buffer[256];
    printf("Type hello\n");
    scanf("%s",buffer);
    if (strcmp(buffer,"hello")==0) printf("Correct\n");
    else printf("wrong\n")
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
    ** main returns an int, this should be
    ** int main ( void )
    */
    main ()
    {
    
    printf ("type 'hello'.\n");
    /*
    ** scanf must have a variable to write to, this is undefined.
    ** Also, the ld flag is for long integers, not strings.
    */
    scanf ("%ld");
    /*
    ** This closes main. Not what you want.
    */
    }
    /*
    ** Illegal block.
    */
    {
    /*
    ** You cannot test scanf like this and strings are surrounded
    ** by double quotations, not single.
    */
    if scanf == 'hello';
    printf ("correct\n");
    else
    printf ("wrong!\n");
    /*
    ** getch is declared in conio.h, yet you did not include it.
    */
    getch();
    }
    This code fixed (somewhat) would look something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    int main ( void )
    {
      char word[10];
    
      printf ( "Type hello: " );
      scanf ( "%s", word );
    
      if ( strcmp ( word, "hello" ) == 0 )
        printf ("correct\n");
      else
        printf ("wrong!\n");
    
      getch();
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Frstly use int main() and return 0 at the end of main.

    >> scanf ("%ld");
    You'll need a variable to store the user-input.

    eg.
    Code:
    char var[20];
    scanf("%s", var);
    //Note the "%s". The ",var" is there to tell the 
    //compiler where you want the input stored.
    >if scanf == 'hello';
    You have to compare strings using a string comparison function of some sort. You can include string.h and use strcmp().
    eg.
    Code:
    #include <string.h>
    ..
    ..
    if( strcmp(var,"hello") == 0) //They'll be equal if strcmp() returns 0
        printf ("correct\n");
    else
        printf ("wrong!\n");
    Also, you're using getch() without including conio.h.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    Here's an ugly way of doing it:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
    char typed [10];
    char compare [10] = "hello";
    printf ("type 'hello':\n");
    scanf ("%s",&typed);
    if (strcmp (typed,compare) == 0){
    	printf("correct");
       }
    else {
    	printf("wrong");
       }
       return 0;
    }
    Hope this helps
    http://uk.geocities.com/ca_chorltonkids

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks for the maybe fastest help on earth

    cya threadhead

Popular pages Recent additions subscribe to a feed