4 Replies - 233 Views - Last Post: 08 August 2012 - 06:05 AM Rate Topic: -----

#1 Kartikcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 12

alpha_check in integer type

Posted 07 August 2012 - 02:25 PM

view this simple program...
This prgm is about finding prime nos in a certain range...and i made it..the problem which i faced was
when the user is entered an alphabet wrong instead of an integer..i have to ignore that entry and i have to continue the execution at the same place...
Eg;suppose if "ending value" goes wrong the execution has to continue again from ending value...
For alpha check i used the statement
"if(scanf("%ld", &n))" but this is executing only once..next time if this statement is encountered it is not asking the value from user, instead it itself assigning the previous value and resulting in infinite loop......
How to overcome this problem ???



#include<stdio.h>
#include<ctype.h>
long int x,y;
int h=0;
int main()
{
    long int a[150000],i,j;
    int c,g,k=1,t=0;
   // clrscr();
    while(t<1)
    {
      printf("\n enter the starting limit:");
      t=check();
    }
    t=0;
    while(t<1)
    {
	printf("\n enter the ending limit:");
        t=check();

    }

    for(i=x;i<=y;i++)
    {
        c=0;
       for(j=1;j<=i;j++)
       {
            if(i%j==0)
                c++;
	/*if(c==3)
	   j=j+i;*/
       }
       if(c==2)
       {
	    a[k]=i;
	    printf("\n prime no %d is:%ld",k,a[k]);
	    k++;
	}
    }
    t=0;
      printf("\n Enter the n th prime no within the range:");
      scanf("%d",&g);
	if(g<=k)
    printf("the %d th prime no within the range:%ld",g,a[g]);
    else
    printf("\n the entered no is out of range!");
    printf("\n");
   // getch();
    return 0;
}
int check()
{   long int n;
    if(scanf("%ld", &n))
    {
        if(n==1)
        {
            printf("\n invalid value");
            return 0;
        }
        else if(h==0)
        {
            x=n;
            h++;
        }
        else
        {
            y=n;
        }
        return 1;
    }
    else
    {
        printf("\n invalid value");
        return 0;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: alpha_check in integer type

#2 GWatt  Icon User is offline

  • member icon

Reputation: 257
  • View blog
  • Posts: 3,035
  • Joined: 01-December 05

Re: alpha_check in integer type

Posted 07 August 2012 - 02:39 PM

You could wrap the check in a while loop. Something like
while (scanf("%ld", ...)==0) {}


I think the problem is that scanf doesn't remove the characters from the buffer if the conversion is invalid. What I would do is read the input into a character buffer and then use atol pr sscanf on that buffer.

This post has been edited by GWatt: 07 August 2012 - 02:43 PM

Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

Re: alpha_check in integer type

Posted 07 August 2012 - 02:53 PM

Getting valid input from a user is always difficult. In this case GWatt is correct, when scanf() fails it doesn't remove the value from the input buffer. To proceed you must first empty the input buffer then ask for the input again.

I also recommend using fgets() and then sscanf() when dealing with user input, then use sscanf() to properly parse the string.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Kartikcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-August 12

Re: alpha_check in integer type

Posted 08 August 2012 - 05:39 AM

View PostGWatt, on 07 August 2012 - 02:39 PM, said:

You could wrap the check in a while loop. Something like
while (scanf("%ld", ...)==0) {}


I think the problem is that scanf doesn't remove the characters from the buffer if the conversion is invalid. What I would do is read the input into a character buffer and then use atol pr sscanf on that buffer.


Thank you for your guidance Sir...

I try to implement buffer concept sir..
But i dont have sound knowledge about buffer concepts..
Can you Make use of that coding in my program?
So that it will be very useful for me to understand the buffer concept very quickly..
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

Re: alpha_check in integer type

Posted 08 August 2012 - 06:05 AM

I suggest that you look at the documentation for the functions fgets() and sscanf(). Then try to implement getting user input with the fgets() and process the buffer, the array of characters, with sscanf(). If you have problems post the code causing the problems, any compiler errors, and ask specific questions about what you are having problems with.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1