problem with fscan

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • orrgal

    #1

    problem with fscan

    in trying to run the code below i get an infinite loop. i thought the
    code was supposed to stop at the scanf command each time it gets there
    but apparently it just keeps going. what is the solution to this?
    thanks.


    #include <stdio.h>
    #include <stdlib.h>


    /
    *============== =============== =============== =============== ==============
    Constants and definitions:
    =============== =============== =============== =============== ==============*/
    #define PERIOD 6;
    #define MAX_PCT 14;
    #define DISCOUNT 15;

    /
    *============== =============== =============== =============== ==============
    Functions:
    =============== =============== =============== =============== ==============*/
    //Returns smaller of two doubles
    double mindbl(double a,double b)
    {
    return a<b?a:b;
    }

    /
    *-------------------------------------------------------------------------
    The main program. Calculates share investment and profit.

    -------------------------------------------------------------------------
    */
    int main()
    {



    int pct,salary,scan res,dscnt,prd,m axpct;
    double beginprc,endprc ,invest,dscntpr c,cost,income,s tocks,profit;
    dscnt=DISCOUNT;
    prd=PERIOD;
    maxpct=MAX_PCT;

    //read salary
    scanres=0;
    while(scanres!= 1)
    {
    printf("\nEnter salary:");
    scanres=scanf(" %d",&salary);
    if(scanres==1)
    {
    scanres= salary>0?1:0;
    }
    if(scanres==0) printf("\nInval id input");
    }

    }
  • Morris Dovey

    #2
    Re: problem with fscan

    orrgal wrote:
    >
    in trying to run the code below i get an infinite loop. i thought the
    code was supposed to stop at the scanf command each time it gets there
    but apparently it just keeps going. what is the solution to this?
    You might care to notice that your input stream contains a
    newline.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA

    Comment

    • Mark Bluemel

      #3
      Re: problem with fscan

      orrgal wrote:


      [(S)He's having trouble with scanf for interactive input]

      have you looked at the FAQ at http://www.c-faq.com ? Question 12.20 is
      probably relevant :-) Question 12.19 addresses your current issue.

      Comment

      Working...