scanf problem
Results 1 to 6 of 6

Thread: scanf problem

  1. #1
    Junior Member mostafaxx's Avatar
    Join Date
    Jul 2010
    Location
    Egypt-damanhour
    Posts
    15

    scanf problem

    peace upon you all every one :

    i'm having a littel problem with the scanf() function

    the following code asks three users about their age using
    scanf("%i",&user_age)

    and then it matches if user_age >= 18 and user_age <= 100 and scanf() returned something except zero.

    if the user entered a character or any except integer scanf() fails and return zero then the code loop back and try to reread the user age .

    the problem is that scanf() won’t re prompt the user to enter any thing !!

    but just keep returning 0 and the program keep trying to reread the age

    here is the code :

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i = 0;
    	int user_age = 0;
    	int scanf_result = 0;
    
    	for (i=1 ; i<=5 ; i++)
    	{
    		printf("user number %i Enter your Age : ",i);
    		scanf_result = scanf("%i",&user_age);
    
    		if (user_age >= 18 && user_age <= 100 && scanf_result > 0)
    		{
    			//if the user input is >= 18 and <= 100 and scanf() returned non zero (succeeded) then
    			//do some work with the user input
    		}
    		else
    		{
    			printf("your age should be an integer from 18 to 100 try again\n");
    			i = i -1;
    		}
    	}
    
    	printf ("\n");
    	return 0;
    }
    Decode the following to 8-Bit ASCII : 01001001 01110011 01101100 01100001 01101101 00100000 01101001 01110011 00100000 01110100 01101000 01100101 00100000 01110011 01101111 01101100 01110101 01110100 01101001 01101111 01101110

    <a rel=nofollow href=http://www.opensuse.org/en/ target=_blank>http://www.opensuse.org/en/</a>

  2. #2
    Junior Member
    Join Date
    Apr 2004
    Location
    United States
    Posts
    24
    A quick google search should help... here are some links that should help you understand what is going on:

    http://www.velocityreviews.com/forum...ith-scanf.html
    http://c-faq.com/stdio/scanfjam.html

    For the reasons mentioned above I prefer to use gets(), or just read() from stdin and check the input, and convert to an integer (if needed) with atoi(), instead of relying on scanf(). It is more work but gives some nicer control over what you can decide to do with the input.

    Good luck.
    Last edited by ABS; January 17th, 2011 at 07:08 PM. Reason: Fixed bad link tags

  3. #3
    Member
    Join Date
    Apr 2004
    Posts
    49
    dear mostafaxx, to take three user's age you should use an array, please take a look on arrays in C/C++ language. then

    at for (i=1 ; i<=5 ; i++) //statement says that you are taking input 5 times.

    and last thing you should mention here that a user should enter a value like 20 or 35 etc --->printf("user number &#37;i Enter your Age in values like 20 or 35 : ",i);

    continue your struggle.



    int age[3],i;
    clrscr();
    for(i=0;i<3;i++)
    { cout<<"please enter age of "<<i+1<<" person";
    cin>>age[i];
    }
    for(i=0;i<3;i++)
    cout<<age[i];

  4. #4
    Junior Member
    Join Date
    Apr 2004
    Location
    United States
    Posts
    24
    As poppy_123 mentioned the C++ syntax is nicer when using cin, unfortunately if you can only use C, then you won't be able to use it.

    I also would mention that IMO using the age array like this makes more sense (replace cin, with your preference of reading user input as needed and cout with your printf command if using strictly C):

    Code:
    int age_count=0;
    int age_needed=3;
    int age[age_needed];
    int age;
    while (age_count < age_needed) {
      cout << "Enter name for user #" << age_count+1 << ": ";
      cin>>age;
      if (age < 18) {
        cout << "Age must be 18+\n";
      }
      else {
        age[age_count]=age;
        age_count++;
      }
    }
    [NOTE] I didn't test the above code, so don't complain if it I made a syntax error
    Last edited by ABS; January 19th, 2011 at 12:16 AM.

  5. #5
    Junior Member mostafaxx's Avatar
    Join Date
    Jul 2010
    Location
    Egypt-damanhour
    Posts
    15
    well thanks poppy_123

    but i have only C under practice for now

    ABS c-faq.com is indeed a very valuable resource for C developers and i really didn't know before now about it ,,and i would recommend any one to have the book published by Addison-Wesley

    but

    i have tried gets() before but it just will keep reading until user hits Enter and that can be used to over-run my buffer like mentioned here

    Since the user cannot specify the length of the buffer passed to gets(), use of this function is discouraged. The length of the string read is unlimited. It is possible to overflow this buffer in such a way as to cause applications to fail, or possible system security violations.

    It is recommended that the fgets() function should be used to read input lines.
    i really would like to stick with scanf() until i find a way to clear the buffer i will try fgets()
    Decode the following to 8-Bit ASCII : 01001001 01110011 01101100 01100001 01101101 00100000 01101001 01110011 00100000 01110100 01101000 01100101 00100000 01110011 01101111 01101100 01110101 01110100 01101001 01101111 01101110

    <a rel=http://www.opensuse.org/en/" border="0" />

  6. #6
    THE Bastard Sys***** dinowuff's Avatar
    Join Date
    Jun 2003
    Location
    Third planet from the Sun
    Posts
    1,247
    Scanf expects data of the correct type. If you enter in the wrong data then it fails to work.

    Have you tried fgets() (nevermind I just re read the post)

    You can try to catch what scanf is returning so if it returns 0 you at least know it failed something along the lines of
    Code:
    int check = 1;int var = 0;while(check){    check = !scanf("&#37;d", &var);}

    fgets(buffer,size,stdin) is your friend - scanf() is not
    09:F9:11:02:9D:74:E3:5B8:41:56:C5:63:56:88:C0

Similar Threads

  1. A Headache of an Email Problem
    By AngelicKnight in forum General Computer Discussions
    Replies: 14
    Last Post: June 15th, 2006, 04:04 AM
  2. Serious Problem
    By IcSilk in forum Operating Systems
    Replies: 8
    Last Post: October 30th, 2005, 10:01 PM
  3. 500 mile email problem
    By Tedob1 in forum Tech Humor
    Replies: 0
    Last Post: December 23rd, 2002, 03:58 PM
  4. C problem...
    By Rna in forum General Programming Questions
    Replies: 4
    Last Post: May 22nd, 2002, 07:03 AM
  5. Help! I've got a nasty IDE problem
    By thesecretfire in forum Hardware
    Replies: 16
    Last Post: May 17th, 2002, 12:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides