Thread: whats wrong here?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    whats wrong here?

    Code:
    #include <stdio.h>
    
    int main()
    {
    
      int i; /*Declaring variable*/
      char text[100]; /*Declaring variable*/
      
      printf("Enter something:"); /*Printing Enter something: to screen*/
      gets(text); /*Getting text typed in and setting the value to text*/
      if(text==0)
      {
      printf("You must enter text");
      }
      else
      {
      for(i=1;i<=5;i++) /* setting the value of i to 1 then setting i to be lesser than or equal to 5 , then adding 1 to to i until it reaches 5*/
      printf("You typed %s and it's been spammed %d times\n" ,text ,i); /*Printing The text followed by the value of text and i %s displays string of text value, and %d displays int value*/
      return(0);
      }
    }
    how come when i dont enter something is still prints the end message?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    i have tried text==NULL doesnt do anything

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > gets(text);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    > if(text==0)
    text is an array, and arrays always exist, so an array can never be equal to NULL
    If you're trying to test the first character, then it would be
    Code:
    if ( text[0] == '\0' )
    But read the FAQ first. fgets() is not quite the same as gets(), but it is far safer to use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    great works fine now.. btw what do you mean fget is safer than gets .. i dont understand why its not safe?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    ah never mind read the page, thanks again

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    also a better practice would be to have the return(0); outside of the else{}

    Code:
      else
      {
        for(i=1;i<=5;i++) /* setting the value of i to 1 then setting i to be lesser than or equal to 5 , then adding 1 to to i until it reaches 5*/
            printf("You typed %s and it's been spammed %d times\n" ,text ,i); /*Printing The text followed by the value of text and i %s displays string of text value, and %d displays int value*/
    
      }
        return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM