Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Is this an illegal stuff?

char describe[500];

puts("Describe yourself") ;

gets(describe) ;

if (describe > 500 ) ; /*are allowed to do this?*/

puts("Your text is too long!") ;

else

puts("Type in less text") ;

gets(describe) ;

puts("Thanks
") ;


What part is wrong and what part should I take out?

I have a bad feeling about the if statements in there,because describe is the name of the char array and the 200 its its value! no, again, the 'describe' part of the if statement is (whatever the user text is) and its comparing them true or false, this is the only way I have found to warn my visitor from flooding my program, need ideas? correct it. Thank you so much.


Comments

  • : char describe[500];

    : puts("Describe yourself") ;

    : gets(describe) ;

    : if (describe > 500 ) ; /*are allowed to do this?*/


    No, but your comments are now correct. =) You still have that semicolon after the if statement. Because of that, the following output statement will get executed anyway, and the else doesn't match the if.


    Also, it won't compare the user's input to the value 500. What precisely do you *want* to do here?


    I think what you need to do is try a project where you know precisely what you want to get as input and what you'll give as output. For example, how about writing a program that accepts two numbers and outputs their sum? Or, more elaborately, a program that accepts numbers until it gets a zero, and prints out the sum of all of them?


    Knowing precisely what you want to actually accomplish, and being able to specify that to yourself or to others, is halfway to having the program completed.



    : puts("Your text is too long!") ;


    Ah, you wanted to compare the length of the user input. Use strlen to do this. Give strlen the string, and it will give you back the length. You can then compare this return value to 500. However, you won't be able to prevent the user from entering in more data than the size of the array this way because by the time you're checking the length, the array will have been filled by user input, and therefore may have already overflowed.


    You can still check the length if you like, though, for other reasons.




    : else

    : puts("Type in less text") ;

    : gets(describe) ;

    : puts("Thanks
    ") ;


    : What part is wrong and what part should I take out?




    The semicolon after the if line but before the comment must be removed, and your comparison for length cannot be done that way. YOu must use the C standard function strlen to get the length of a string.




    : I have a bad feeling about the if statements in there,because describe is the name of the char array and the 200 its its value! no, again, the 'describe' part of the if statement is (whatever the user text is) and its comparing them true or false, this is the only way I have found to warn my visitor from flooding my program, need ideas? correct it. Thank you so much.




    A char array doesn't have 'value' in the numeric sense, really, and as I mentioned already, your check comes too late.


    If you really want to make sure the user doesn't enter in too much data, use:


    fgets(string,max_length,stdin);


    This will guarantee that no more than max_length characters (including the null byte) will be put in the array 'string'. the 'stdin' is the standard input stream.


    gets is a dangerous function, and in my opinion, shouldn't even be used.







Sign In or Register to comment.