Hi,

 

Better use fgets followed by sscanf.

 

Ex: A sample program to use fgets

 

main()

{

char input[10];

int num;

 

printf(“Enter the number : \t”);

If (fgets(input, 10, stdin) == NULL)           {

                        fprintf(stderr, “Enter the proper input\n”);

                        exit(1);

}

sscanf(input, “%d”, &num);        (or) num  = atoi(input);

printf(“The Entered num is : %d”, num);

}

 

 

                        (If you want to check the leading spaces, then we have to skip skip the spaces).

 

main()

{

char input[10];

char *p;

int num;

 

printf(“Enter the number : \t”);

If (fgets(input, 10, stdin) == NULL)           {

                        fprintf(stderr, “Enter the proper input\n”);

                        exit(1);

}

p = input;

while (*p == ‘ ‘ || *p == ‘\t’)

            p++;

sscanf(p, “%d”, &num);        (or) num  = atoi(p);

printf(“The Entered num is : %d”, num);

}

 

 

 

 

 


From: [email protected] [mailto:[email protected]] On Behalf Of Kishan Tikandar
Sent: Friday, August 18, 2006 5:55 PM
To: [email protected]
Subject: [c-prog] Re: Cannot loop this thing.

 

Thanks for replying..

I don't think I got you correctly... I'm not familiar with fgets() and
strtol(). I tried replacing the "scanf("%c",&another);" with "another
= getchar();". The program still behaves in the same way. :(

--- In [EMAIL PROTECTED]com, andrew clarke <[EMAIL PROTECTED]> wrote:
>
> On Fri, Aug 18, 2006 at 07:49:49AM -0000, Kishan Tikandar wrote:
>
> > I'm trying to create a program for calculate grace marks for a student
> > based on certain rules. The logic works fine but it is not looping as
> > I want it to.
> >
> > I want this program to prompt me whether i wish to "check for another
> > student" but it just executes for one time and end.
>
> > printf("\nEnter the class obtained by the student:");
> > scanf("%d",&clas);
>
> scanf() can be unpredictable and difficult to work with. I would use
> fgets() then strtol() instead.
>
> > scanf("%c",&another);
>
> Here I would use
>
> another = getchar();
>

__._,_.___

To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.




SPONSORED LINKS
C and c++ Computer programming languages Java programming language
Basic programming language Programming languages


YAHOO! GROUPS LINKS




__,_._,___

Reply via email to