Is there any clear function?

This is a discussion on Is there any clear function? within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <string.h> main() { char s[2][3]; int c; for( c = 0 ; c < 2 ; c++ ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question Is there any clear function?

    #include <stdio.h>
    #include <string.h>

    main()
    {
    char s[2][3];
    int c;

    for( c = 0 ; c < 2 ; c++ )
    {
    printf("Your %d string: ", c);
    scanf("%s", s[c]);

    if( strlen(s[c]) > 3 )
    {
    c--;
    printf("More than 3 characteres!\n");
    continue;
    }
    }

    return 0;
    }

    Question:
    Whenever the user enter more than 3 characters which exceeds
    the allocated space, this program will prompt the user to reenter their desire string. But the problem is when I purposely enter more than 3 characters in second attempt, the compiler prints
    out as usual but with error windows pop up. I know this error was due to over-stored characters. So, is there any function that can clear that particular array and treat is as new one or maybe there is other way of doing so?

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    well....im not sure but u can try using pointers...correct me if im wrong

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Checking the amount of characters after you have read them is to late, if the user enters to many they will have overwritten memory not reserved for it. I suggest you look at the function fgets() which allows you to set a maximum number characters to be read.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Talking Purposely causing an error.

    >>I suggest you look at the function fgets() which allows you to set a maximum number characters to be read.

    Thanks for your suggestion.

    >>well....im not sure but u can try using pointers...correct me if im wrong.

    Thanks for your suggestion too.



    New question (purposely causing an error):
    #include <stdio.h>

    main()
    {
    int n;

    while(1)
    {
    printf("Please enter your number in the range of 0 - 30 ");
    scanf("%d", &n);

    if( n < 1 || n > 30 )
    {
    printf("Wrong!\n");
    continue;
    }

    }
    return 0;
    }

    From the above code it is already obvious that the program requires the user to enter an integer number. If let say the user accidentally or purposely entered a character which opposes the declared integer variable, how do I clear the entered characters? I had tried to use this isalpha() function to detect the existence of character but the problem again is too late as what stated by C_Coder (Checking the amount of characters after you have read them is to late). What should I do?

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    11

    Clear function numbers 0 to 30

    #include <stdio.h>



    int main()
    {
    int n;

    printf("Please enter your number in the range of 0 - 30\n ");
    scanf("%i", &n);
    fflush(stdin);
    while(( n < 0) || (n > 30 ))
    {
    printf("Wrong! number must be in the range 0 - 30\n");
    scanf("%i", &n);
    fflush(stdin);
    }
    printf( "you got it!!!\n\n");
    system("PAUSE");

    return 0;
    }
    If its not 5.00p.m. on Friday its crap!!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    fred_scotland, don't post again until you can tell me why

    >fflush(stdin);
    is wrong, and

    >system("PAUSE");
    is generally a bad idea.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question Curious and have no idea!

    >>fflush(stdin);
    Why can we use this function as it clears whatever entered?

    >>system("PAUSE")
    What does this function, function as?

    >>scanf("%i", &n);
    Haven't seen this '%i' before. What does it takes?

    Thanks for reading guys!

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > Why can we use this function as it clears whatever entered?

    Because it's nonstandard. It could clear what's entered, or it could do something else.

  9. #9
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42

    Re: Clear function numbers 0 to 30

    Code:
    Coded by fred_scotland 
    
    [B]#include <stdio.h> 
    while(( n < 0) || (n > 30 ))
    { 
        printf("Wrong! number must be in the range 0 - 30\n");
        scanf("%i", &n);
        fflush(stdin);
    }
    If you want the range of the number that is entered, change your conditional statement in your while loop.

    Code:
    while (n >= 0 && n <= 30) {
        do.blah();
    }
    http://www.KBeutler.com

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    11

    re clear function etc

    Hi there ,

    Use my code without any changes and use all the system calls you need to. They are not illegal, there are thousands of programs running using this type of code, and they work just fine, which is more than can be said for most of the stuff posted on here.

    Don't worry about the arguments over this function and that function, the most important thing is that the code runs and that the program does its job. Most code posted on here does not compile, and when it does, it is useless because it has no built in functionality.


    Indeed some code i read on here just recently proposed reading and writing to file on disk without specifying the drive the file was on, the guy who replied thanked the first guy for the code and said that it compiled and ran just fine on his machine and he was able to write to disk with it ho hum!!!!!

    Anyway the code i posted will run just fine and won't loop or hang don't let the others on here worry you just keep on posting.
    If its not 5.00p.m. on Friday its crap!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21