Facing Problem in Loop

Discussion in 'C Programming' started by Vijaykumar Dave, Apr 11, 2007.

  1. I have a program for base X power N as under. The problem is that when
    the range specified in loop is given it works well, but when any
    character is pressed, it goes to infinite loop.

    Second problem is it works fine for smaller value of X or N but not
    for higher say base 15 power 20. How can I get true value displayed
    with such higher base or power.?
    Can any one help me to correct this program putting proper loop and
    for high value power.

    #include<stdio.h>
    #include<conio.h>

    long double power(double x, double n);

    double base, pwr;
    long double result;
    //int pwr;

    void main()
    {
    clrscr();
    gotoxy(22,11);
    printf("Enter Base Number : ");
    scanf("%lf", &base);
    gotoxy(22, 13);
    printf("Enter Power Number : ");
    scanf("%lf", &pwr);
    printf("%lf %lf", base,pwr);

    result=power(base,pwr);
    gotoxy(22, 15);
    printf("Result of Base %f having power %d is\n %60.10Lf.", base,
    pwr, result);
    getch();
    }

    long double power(double x, double n)
    {
    long double res;
    if (n < 0)
    return 1/power(x, -n);
    else if (n == 0)
    return 1.0;
    else
    res=(x*power(x,n-1));
    printf("\n%60.10Lf", res);
    return(res);
    // return (x * power(x, n-1));
    }
    Vijaykumar Dave, Apr 11, 2007
    #1
    1. Advertising

  2. In article <>,
    Vijaykumar Dave <> wrote:
    >I have a program for base X power N as under. The problem is that when
    >the range specified in loop is given it works well, but when any
    >character is pressed, it goes to infinite loop.


    >#include<stdio.h>
    >#include<conio.h>


    Sorry, there is no conio.h in standard C, and there is no
    gotoxy() or getch() functions. As those appear to have something to
    do with your I/O and you are having problems with your I/O
    ("it goes to infinite loop") you will need to consult a newsgroup
    that deals with whatever operating system you are using. Or, better
    yet, rewrite the program without using those functions.
    --
    There are some ideas so wrong that only a very intelligent person
    could believe in them. -- George Orwell
    Walter Roberson, Apr 11, 2007
    #2
    1. Advertising

  3. In article <>,
    Vijaykumar Dave <> wrote:
    >I have a program for base X power N as under.


    >double base, pwr;


    > printf("Result of Base %f having power %d is\n %60.10Lf.", base,
    >pwr, result);


    You have pwr as a double, but you are attempting to print it with
    a %d format, which is only valid for int . It would be surprising
    if any of your outputs were correct, other than when pwr was 0.
    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
    Walter Roberson, Apr 11, 2007
    #3
  4. On Wed, 11 Apr 2007 14:03:42 -0700, Vijaykumar Dave wrote:

    > I have a program for base X power N as under. The problem is that when
    > the range specified in loop is given it works well, but when any
    > character is pressed, it goes to infinite loop.
    >
    > Second problem is it works fine for smaller value of X or N but not
    > for higher say base 15 power 20. How can I get true value displayed
    > with such higher base or power.?
    > Can any one help me to correct this program putting proper loop and
    > for high value power.
    >
    > #include<stdio.h>
    > #include<conio.h>


    conio.h isn't standard.

    > long double power(double x, double n);
    >
    > double base, pwr;
    > long double result;
    > //int pwr;
    >
    > void main()


    main returns int. Now and forever. Always has.

    > {
    > clrscr();


    No such function as clrscr() in C.

    > gotoxy(22,11);


    No such function as gotoxy in C.

    > printf("Enter Base Number : ");
    > scanf("%lf", &base);
    > gotoxy(22, 13);
    > printf("Enter Power Number : ");
    > scanf("%lf", &pwr);
    > printf("%lf %lf", base,pwr);
    >
    > result=power(base,pwr);
    > gotoxy(22, 15);
    > printf("Result of Base %f having power %d is\n %60.10Lf.", base,
    > pwr, result);
    > getch();


    No such function as getch() in C.
    > }




    > long double power(double x, double n)
    > {
    > long double res;
    > if (n < 0)
    > return 1/power(x, -n);
    > else if (n == 0)


    Note that n is a double. Somehow I suspect at least one of these tests is
    going to fail as the n you think should be, say, 0 is actually
    0.0000000014376 or some such.

    > return 1.0;
    > else
    > res=(x*power(x,n-1));
    > printf("\n%60.10Lf", res);
    > return(res);
    > // return (x * power(x, n-1));
    > }


    If I were you, I'd try changing the second parameter of power() to an
    integer of some flavour. Also, %lf? I believe that should simply be %f.

    According to my testing, the code - after changing n to an int - doesn't
    work right. Results:

    Your code: 332525673007965060202496.000000
    bc: 332525673007965087890625
    ^^^^^^^^

    I'm assuming this is the usual problem when using floating point values.
    Indeed, testing it with long doubles bought another four digits of correct
    answer.
    Kelsey Bjarnason, Apr 11, 2007
    #4
  5. Vijaykumar Dave wrote:
    > I have a program for base X power N as under. The problem is that when
    > the range specified in loop is given it works well, but when any
    > character is pressed, it goes to infinite loop.


    If you remove the non-standard implementation-specific conio junk,
    fix the illegal return type on main,
    fix the incorrect "%d" specifier,
    provide a '\n' to terminate the last line of output,
    and fflush(stdout) after your prompts, you may find your problems disappear.
    Of course, you still have the problem of detecting non-integral values
    for n, since you have no terminating condition for 0 < n < 1. This will
    obviously mean power never ends.

    (and don't use tabs or // comments in posted programs, unless you want
    them to be unreadable)

    > Second problem is it works fine for smaller value of X or N but not
    > for higher say base 15 power 20. How can I get true value displayed
    > with such higher base or power.?


    The equivalent
    return (n < 0) ? 1/power(x,-n) : (n == 0) ? 1 : x*power(x,n-1);
    has been tested and found to work fine for power(15,20).

    You might try a little easier to read output.
    #include <float.h> and instead of
    > printf("Result of Base %f having power %d is\n %60.10Lf.", base,
    > pwr, result);

    try
    printf("power(%g,%g) = %.*Lg\n", base, pwr, LDBL_DIG, result);

    How do you know the result was wrong?

    By the way, your variables result and res are typing practice and not
    needed.

    > Can any one help me to correct this program putting proper loop and
    > for high value power.
    >
    > #include<stdio.h>
    > #include<conio.h>
    >
    > long double power(double x, double n);
    >
    > double base, pwr;
    > long double result;
    > //int pwr;
    >
    > void main()
    > {
    > clrscr();
    > gotoxy(22,11);
    > printf("Enter Base Number : ");
    > scanf("%lf", &base);
    > gotoxy(22, 13);
    > printf("Enter Power Number : ");
    > scanf("%lf", &pwr);
    > printf("%lf %lf", base,pwr);
    >
    > result=power(base,pwr);
    > gotoxy(22, 15);
    > printf("Result of Base %f having power %d is\n %60.10Lf.", base,
    > pwr, result);
    > getch();
    > }
    >
    > long double power(double x, double n)
    > {
    > long double res;
    > if (n < 0)
    > return 1/power(x, -n);
    > else if (n == 0)
    > return 1.0;
    > else
    > res=(x*power(x,n-1));
    > printf("\n%60.10Lf", res);
    > return(res);
    > // return (x * power(x, n-1));
    > }
    >
    Martin Ambuhl, Apr 11, 2007
    #5
  6. Vijaykumar Dave

    Old Wolf Guest

    On Apr 12, 9:45 am, Kelsey Bjarnason <> wrote:
    > > printf("\n%60.10Lf", res);

    >
    > If I were you, I'd try changing the second parameter of power() to an
    > integer of some flavour. Also, %lf? I believe that should simply be %f.


    In C99, %Lf is used for printing long doubles and either of
    %f and %lf can be used for printing doubles.
    Old Wolf, Apr 12, 2007
    #6
  7. On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:

    > On Apr 12, 9:45 am, Kelsey Bjarnason <> wrote:
    >> > printf("\n%60.10Lf", res);

    >>
    >> If I were you, I'd try changing the second parameter of power() to an
    >> integer of some flavour. Also, %lf? I believe that should simply be %f.

    >
    > In C99, %Lf is used for printing long doubles and either of
    > %f and %lf can be used for printing doubles.


    My bad; somehow I completely overlooked the use of long double for result.

    Meanwhile the values passed around are doubles, rendering the use of long
    double here kinda pointless.
    Kelsey Bjarnason, Apr 13, 2007
    #7
  8. On Apr 13, 4:19 am, Kelsey Bjarnason <> wrote:
    > On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
    > > On Apr 12, 9:45 am, Kelsey Bjarnason <> wrote:
    > >> > printf("\n%60.10Lf", res);

    >
    > >> If I were you, I'd try changing the second parameter of power() to an
    > >> integer of some flavour. Also, %lf? I believe that should simply be %f.

    >
    > > In C99, %Lf is used for printing long doubles and either of
    > > %f and %lf can be used for printing doubles.

    >
    > My bad; somehow I completely overlooked the use of long double for result.
    >
    > Meanwhile the values passed around are doubles, rendering the use of long
    > double here kinda pointless.


    Dear All,

    My problem is regarding loop...

    It does not work when any key other than the option 1-8 is pressed
    keeping program in infinite loop
    Vijaykumar Dave, Apr 13, 2007
    #8
  9. Vijaykumar Dave

    Ian Collins Guest

    Vijaykumar Dave wrote:
    > On Apr 13, 4:19 am, Kelsey Bjarnason <> wrote:
    >
    >>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
    >>
    >>>On Apr 12, 9:45 am, Kelsey Bjarnason <> wrote:
    >>>
    >>>>> printf("\n%60.10Lf", res);

    >>
    >>>>If I were you, I'd try changing the second parameter of power() to an
    >>>>integer of some flavour. Also, %lf? I believe that should simply be %f.

    >>
    >>>In C99, %Lf is used for printing long doubles and either of
    >>>%f and %lf can be used for printing doubles.

    >>
    >>My bad; somehow I completely overlooked the use of long double for result.
    >>
    >>Meanwhile the values passed around are doubles, rendering the use of long
    >>double here kinda pointless.

    >
    >
    > Dear All,
    >
    > My problem is regarding loop...
    >
    > It does not work when any key other than the option 1-8 is pressed
    > keeping program in infinite loop
    >

    Have you stepped through in your debugger to see why?

    --
    Ian Collins.
    Ian Collins, Apr 14, 2007
    #9
  10. On Apr 14, 4:04 am, Ian Collins <> wrote:
    > Vijaykumar Dave wrote:
    > > On Apr 13, 4:19 am, Kelsey Bjarnason <> wrote:

    >
    > >>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:

    >
    > >>>On Apr 12, 9:45 am, Kelsey Bjarnason <> wrote:

    >
    > >>>>> printf("\n%60.10Lf", res);

    >
    > >>>>If I were you, I'd try changing the second parameter of power() to an
    > >>>>integer of some flavour. Also, %lf? I believe that should simply be %f.

    >
    > >>>In C99, %Lf is used for printing long doubles and either of
    > >>>%f and %lf can be used for printing doubles.

    >
    > >>My bad; somehow I completely overlooked the use of long double for result.

    >
    > >>Meanwhile the values passed around are doubles, rendering the use of long
    > >>double here kinda pointless.

    >
    > > Dear All,

    >
    > > Myproblemis regardingloop...

    >
    > > It does not work when any key other than the option 1-8 is pressed
    > > keeping program in infiniteloop

    >
    > Have you stepped through in your debugger to see why?
    >
    > --
    > Ian Collins.- Hide quoted text -
    >
    > - Show quoted text -


    ya... but it is not stopping at scanf if any alphabate is pressed...
    Vijaykumar Dave, Apr 14, 2007
    #10
  11. Vijaykumar Dave

    Ian Collins Guest

    Vijaykumar Dave wrote:
    > On Apr 14, 4:04 am, Ian Collins <> wrote:
    >
    >>Vijaykumar Dave wrote:
    >>
    >>>Dear All,

    >>
    >>>Myproblemis regardingloop...

    >>
    >>>It does not work when any key other than the option 1-8 is pressed
    >>>keeping program in infiniteloop

    >>
    >>Have you stepped through in your debugger to see why?
    >>


    *Please* don't quote signatures or that google quoted text crap.
    >
    > ya... but it is not stopping at scanf if any alphabate is pressed...
    >

    What do you expect if scanf fails? The second scanf just gets the end
    of line sequence form the first. Anyway, where is the loop?

    --
    Ian Collins.
    Ian Collins, Apr 14, 2007
    #11
  12. On Apr 15, 2:55 am, Ian Collins <> wrote:
    > Vijaykumar Dave wrote:
    > > On Apr 14, 4:04 am, Ian Collins <> wrote:

    >
    > >>Vijaykumar Dave wrote:

    >
    > >>>Dear All,

    >
    > >>>Myproblemis regardingloop...

    >
    > >>>It does not work when any key other than the option 1-8 is pressed
    > >>>keeping program in infiniteloop

    >
    > >>Have you stepped through in your debugger to see why?

    >
    > *Please* don't quote signatures or that google quoted text crap.
    >
    > > ya... but it is not stopping at scanf if any alphabate is pressed...

    >
    > What do you expect if scanf fails? The second scanf just gets the end
    > of line sequence form the first. Anyway, where is the loop?
    >
    > --
    > Ian Collins.


    sorry ian,

    I am appending the program in which I have the problem in loop which
    is for simple calculator. When I press the key 1-8, it runs fine, but
    any other key puts the program in infinite loop. Will you help me to
    correct my program ?

    Thanks for response...

    Vijaykumar Dave

    -------------------------

    #include<stdio.h>
    #include<conio.h>

    void menuprog();
    void wait();

    int i, choice, num3, num4;
    float num1, num2;

    void main()
    {
    clrscr();
    menuprog();
    gotoxy(30,12);
    printf("Have a Nice Day");
    gotoxy(30,14);
    printf("- Vijaykumar Dave -");
    }

    void menuprog()
    {
    gotoxy(14,1);
    printf("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
    gotoxy(14,2);
    printf("=======================================================");

    do
    {
    clrscr();
    gotoxy(20,4);
    printf("1.Adition.");
    gotoxy(20,6);
    printf("2.Subtraction.");
    gotoxy(20,8);
    printf("3.Multiplication.");
    gotoxy(20,10);
    printf("4.Division.");
    gotoxy(20,12);
    printf("5.Modular.");
    gotoxy(20,14);
    printf("6.1/x.");
    gotoxy(20,16);
    printf("7.Percentage.");
    gotoxy(20,18);
    printf("8.Exit.");
    gotoxy(20,20);
    printf("Enter your choice [1-8] : ");

    gotoxy(47,20);
    scanf("%d",&choice);

    if (choice<1 || choice >8)
    {
    textcolor(RED);
    gotoxy(20,23);
    cprintf(" Valid range : 1-8");
    textcolor(CYAN);
    // choice=0;
    }

    switch(choice)
    {
    case 1:if(choice==1) /* Add */
    {
    num1=get_number1();
    num2=get_number2();
    printf("Sum = %35.2f\n",num1+num2);
    wait();
    }
    case 2:if(choice==2) /* Subtract */
    {
    num1=get_number1();
    num2=get_number2();
    printf("Subtraction = %35.2f\n",num1-num2);
    wait();
    }
    case 3:if(choice==3) /* Multipy */
    {
    num1=get_number1();
    num2=get_number2();
    printf("Multiplication = %35.2f\n",num1*num2);
    wait();
    }
    case 4:if(choice==4) /* Divide */
    {
    num1=get_number1();
    num2=get_number2();
    printf("Division = %35.2f\n",num1/num2);
    wait();
    }
    case 5:if(choice==5) /* Reminder */
    {
    num2=get_number3();
    printf("Modular = %f\n",num3%num4);
    wait();
    }
    case 6:if(choice==6) /* 1/x */
    {
    num2=get_number2();
    printf("1/x = %35.2f\n",1/num2);
    // wait();
    }
    case 7:if(choice==7) /* Percent */
    {
    num1=get_number1();
    num2=get_number2();
    printf("Percentage = %35.2f\n",(num1/num2)*100);
    // wait();
    }
    default: /* Default */
    break;
    } /* end of switch */
    wait(); /* Hold the Program */
    } while (choice!=8); /* End of Do - While Loop */

    gotoxy(11,24);
    printf("Developed by : Vijaykumar Dave, IGNOU, BCA, IInd Semester");
    }

    int get_number1() /* Accept First Number */
    {
    float num1, num2;
    clrscr();
    gotoxy(30,12);
    printf("Enter First Number");
    scanf("%d",&num1);
    return (num1);
    }

    int get_number2() /* Accept Second Number */
    {
    float num1, num2;
    clrscr();
    if (choice==6)
    {
    gotoxy(30,10);
    printf("First Number is 1");
    }

    gotoxy(30,12);
    printf("Enter Second Number");
    scanf("%d",&num2);
    return (num2);
    }

    int get_number3() /* Where Numbers are not to be used as Float Type */
    {
    int num3,num4;
    clrscr();
    gotoxy(30,12);
    printf("Enter First Number");
    scanf("%d",&num3);
    gotoxy(30,14);
    printf("Enter Second Number");
    scanf("%d",&num3);
    return (num3,num4);
    }

    void wait()
    {
    gotoxy(10,24);
    printf("developed by Vijaykumar Dave... press any key to continue.
    ");
    getch();
    }
    Vijaykumar Dave, Apr 15, 2007
    #12
  13. Vijaykumar Dave

    osmium Guest

    "Vijaykumar Dave" wrote:


    >> >>>Myproblemis regardingloop...

    >>
    >> >>>It does not work when any key other than the option 1-8 is pressed
    >> >>>keeping program in infiniteloop

    >>
    >> >>Have you stepped through in your debugger to see why?

    >>
    >> *Please* don't quote signatures or that google quoted text crap.
    >>
    >> > ya... but it is not stopping at scanf if any alphabate is pressed...

    >>
    >> What do you expect if scanf fails? The second scanf just gets the end
    >> of line sequence form the first. Anyway, where is the loop?
    >>
    >> --
    >> Ian Collins.

    >
    > sorry ian,
    >
    > I am appending the program in which I have the problem in loop which
    > is for simple calculator. When I press the key 1-8, it runs fine, but
    > any other key puts the program in infinite loop. Will you help me to
    > correct my program ?
    >
    > Thanks for response...
    >
    > Vijaykumar Dave
    >
    > -------------------------
    >
    > #include<stdio.h>
    > #include<conio.h>
    >
    > void menuprog();
    > void wait();
    >
    > int i, choice, num3, num4;
    > float num1, num2;
    >
    > void main()
    > {
    > clrscr();
    > menuprog();
    > gotoxy(30,12);
    > printf("Have a Nice Day");
    > gotoxy(30,14);
    > printf("- Vijaykumar Dave -");
    > }
    >
    > void menuprog()
    > {
    > gotoxy(14,1);
    > printf("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
    > gotoxy(14,2);
    > printf("=======================================================");
    >
    > do
    > {
    > clrscr();
    > gotoxy(20,4);
    > printf("1.Adition.");
    > gotoxy(20,6);
    > printf("2.Subtraction.");
    > gotoxy(20,8);
    > printf("3.Multiplication.");
    > gotoxy(20,10);
    > printf("4.Division.");
    > gotoxy(20,12);
    > printf("5.Modular.");
    > gotoxy(20,14);
    > printf("6.1/x.");
    > gotoxy(20,16);
    > printf("7.Percentage.");
    > gotoxy(20,18);
    > printf("8.Exit.");
    > gotoxy(20,20);
    > printf("Enter your choice [1-8] : ");
    >
    > gotoxy(47,20);
    > scanf("%d",&choice);


    scanf returns a value, look at it. The usual advice is to not use scanf
    because of problems such as this. Read a line, examine it with the
    fundamental tools you have and do your own conversion. There are functions
    to do the *actual* conversion.

    This may be a FAQ, I don't know. In any event you will get tons of hits
    doing a Google search on this Usenet group.

    <snip>
    osmium, Apr 15, 2007
    #13
  14. Vijaykumar Dave

    CBFalconer Guest

    Vijaykumar Dave wrote:
    >
    > On Apr 15, 2:55 am, Ian Collins <> wrote:
    > > Vijaykumar Dave wrote:
    > > > On Apr 14, 4:04 am, Ian Collins <> wrote:

    > >
    > > >>Vijaykumar Dave wrote:

    > >
    > > >>>Dear All,

    > >
    > > >>>Myproblemis regardingloop...

    > >
    > > >>>It does not work when any key other than the option 1-8 is pressed
    > > >>>keeping program in infiniteloop

    > >
    > > >>Have you stepped through in your debugger to see why?

    > >
    > > *Please* don't quote signatures or that google quoted text crap.
    > >
    > > > ya... but it is not stopping at scanf if any alphabate is pressed...

    > >
    > > What do you expect if scanf fails? The second scanf just gets the end
    > > of line sequence form the first. Anyway, where is the loop?
    > >
    > > --
    > > Ian Collins.

    >
    > sorry ian,
    >
    > I am appending the program in which I have the problem in loop which
    > is for simple calculator. When I press the key 1-8, it runs fine, but
    > any other key puts the program in infinite loop. Will you help me to
    > correct my program ?
    >
    > Thanks for response...
    >
    > Vijaykumar Dave
    >
    > -------------------------
    >
    > #include<stdio.h>
    > #include<conio.h>


    No such include file in standard C

    >
    > void menuprog();
    > void wait();
    >
    > int i, choice, num3, num4;
    > float num1, num2;
    >
    > void main()


    illegal call. Main always returns int.

    > {
    > clrscr();


    No such function in standard C


    > menuprog();
    > gotoxy(30,12);


    No such function in standard C

    > printf("Have a Nice Day");
    > gotoxy(30,14);


    No such function in standard C

    > printf("- Vijaykumar Dave -");
    > }


    Main returns an int. Do so.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Apr 15, 2007
    #14
  15. Vijaykumar Dave

    Flash Gordon Guest

    Vijaykumar Dave wrote, On 15/04/07 19:52:
    > On Apr 15, 2:55 am, Ian Collins <> wrote:
    >> Vijaykumar Dave wrote:
    >>> On Apr 14, 4:04 am, Ian Collins <> wrote:
    >>>> Vijaykumar Dave wrote:
    >>>>> Dear All,
    >>>>> Myproblemis regardingloop...
    >>>>> It does not work when any key other than the option 1-8 is pressed
    >>>>> keeping program in infiniteloop
    >>>> Have you stepped through in your debugger to see why?

    >> *Please* don't quote signatures or that google quoted text crap.
    >>
    >>> ya... but it is not stopping at scanf if any alphabate is pressed...

    >> What do you expect if scanf fails? The second scanf just gets the end
    >> of line sequence form the first. Anyway, where is the loop?
    >>
    >> --
    >> Ian Collins.


    Please do not quote people signatures, the bit typically after the "-- "
    unless you are quoting on them.

    > sorry ian,
    >
    > I am appending the program in which I have the problem in loop which
    > is for simple calculator. When I press the key 1-8, it runs fine, but
    > any other key puts the program in infinite loop. Will you help me to
    > correct my program ?
    >
    > Thanks for response...
    >
    > Vijaykumar Dave
    >
    > -------------------------
    >
    > #include<stdio.h>
    > #include<conio.h>


    conie.h is not a standard header, so it and anything to do with it is
    not topical here.

    > void menuprog();
    > void wait();
    >
    > int i, choice, num3, num4;
    > float num1, num2;


    Unless you have a very good reason for using float you should use double.

    You should not be using file scope variables (globals) as a general
    rule, and there is certainly no good reason for them in this program.

    > void main()


    main returns an in, throw away any books/tutorials which claim otherwise
    as they are wrong. If they can't get something that basic correct there
    is no reason to suppose they will get other things correct.

    <snip>

    > scanf("%d",&choice);


    <snip>

    As Ian said, you need to check the return value of scanf. You also need
    to read the documentation for what happens if it fails. This is all in
    the comp.lang.c FAQ which can be found at http://c-faq.com/ which you
    should always check before asking a question here, specifically question
    12.19.

    In fact, with all newsgroups you should check out the groups FAQ (if one
    exists) and charter (if one exists) before posting.
    --
    Flash Gordon
    Flash Gordon, Apr 15, 2007
    #15
  16. CBFalconer said:

    > Vijaykumar Dave wrote:
    >>

    <snip>

    >> void main()

    >
    > illegal call.


    It's not a call.

    > Main always returns int.


    I think you mean "main", not "Main".

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Apr 15, 2007
    #16
  17. Vijaykumar Dave

    pete Guest

    Vijaykumar Dave wrote:
    >
    > On Apr 15, 2:55 am, Ian Collins <> wrote:
    > > Vijaykumar Dave wrote:
    > > > On Apr 14, 4:04 am, Ian Collins <> wrote:

    > >
    > > >>Vijaykumar Dave wrote:

    > >
    > > >>>Dear All,

    > >
    > > >>>Myproblemis regardingloop...

    > >
    > > >>>It does not work when any key other than the option 1-8 is pressed
    > > >>>keeping program in infiniteloop

    > >
    > > >>Have you stepped through in your debugger to see why?

    > >
    > > *Please* don't quote signatures or that google quoted text crap.
    > >
    > > > ya... but it is not stopping at scanf if any alphabate is pressed...

    > >
    > > What do you expect if scanf fails? The second scanf just gets the end
    > > of line sequence form the first. Anyway, where is the loop?
    > >
    > > --
    > > Ian Collins.

    >
    > sorry ian,
    >
    > I am appending the program in which I have the problem in loop which
    > is for simple calculator. When I press the key 1-8, it runs fine, but
    > any other key puts the program in infinite loop. Will you help me to
    > correct my program ?
    >
    > Thanks for response...
    >
    > Vijaykumar Dave
    >
    > -------------------------
    >
    > #include<stdio.h>
    > #include<conio.h>
    >
    > void menuprog();
    > void wait();
    >
    > int i, choice, num3, num4;
    > float num1, num2;
    >
    > void main()
    > {
    > clrscr();
    > menuprog();
    > gotoxy(30,12);
    > printf("Have a Nice Day");
    > gotoxy(30,14);
    > printf("- Vijaykumar Dave -");
    > }
    >
    > void menuprog()
    > {
    > gotoxy(14,1);
    > printf("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
    > gotoxy(14,2);
    > printf("=======================================================");
    >
    > do
    > {
    > clrscr();
    > gotoxy(20,4);
    > printf("1.Adition.");
    > gotoxy(20,6);
    > printf("2.Subtraction.");
    > gotoxy(20,8);
    > printf("3.Multiplication.");
    > gotoxy(20,10);
    > printf("4.Division.");
    > gotoxy(20,12);
    > printf("5.Modular.");
    > gotoxy(20,14);
    > printf("6.1/x.");
    > gotoxy(20,16);
    > printf("7.Percentage.");
    > gotoxy(20,18);
    > printf("8.Exit.");
    > gotoxy(20,20);
    > printf("Enter your choice [1-8] : ");
    >
    > gotoxy(47,20);
    > scanf("%d",&choice);
    >
    > if (choice<1 || choice >8)
    > {
    > textcolor(RED);
    > gotoxy(20,23);
    > cprintf(" Valid range : 1-8");
    > textcolor(CYAN);
    > // choice=0;
    > }
    >
    > switch(choice)
    > {
    > case 1:if(choice==1) /* Add */
    > {
    > num1=get_number1();
    > num2=get_number2();
    > printf("Sum = %35.2f\n",num1+num2);
    > wait();
    > }
    > case 2:if(choice==2) /* Subtract */
    > {
    > num1=get_number1();
    > num2=get_number2();
    > printf("Subtraction = %35.2f\n",num1-num2);
    > wait();
    > }
    > case 3:if(choice==3) /* Multipy */
    > {
    > num1=get_number1();
    > num2=get_number2();
    > printf("Multiplication = %35.2f\n",num1*num2);
    > wait();
    > }
    > case 4:if(choice==4) /* Divide */
    > {
    > num1=get_number1();
    > num2=get_number2();
    > printf("Division = %35.2f\n",num1/num2);
    > wait();
    > }
    > case 5:if(choice==5) /* Reminder */
    > {
    > num2=get_number3();
    > printf("Modular = %f\n",num3%num4);
    > wait();
    > }
    > case 6:if(choice==6) /* 1/x */
    > {
    > num2=get_number2();
    > printf("1/x = %35.2f\n",1/num2);
    > // wait();
    > }
    > case 7:if(choice==7) /* Percent */
    > {
    > num1=get_number1();
    > num2=get_number2();
    > printf("Percentage = %35.2f\n",(num1/num2)*100);
    > // wait();
    > }
    > default: /* Default */
    > break;
    > } /* end of switch */
    > wait(); /* Hold the Program */
    > } while (choice!=8); /* End of Do - While Loop */
    >
    > gotoxy(11,24);
    > printf("Developed by : Vijaykumar Dave, IGNOU, BCA, IInd Semester");
    > }
    >
    > int get_number1() /* Accept First Number */
    > {
    > float num1, num2;
    > clrscr();
    > gotoxy(30,12);
    > printf("Enter First Number");
    > scanf("%d",&num1);
    > return (num1);
    > }
    >
    > int get_number2() /* Accept Second Number */
    > {
    > float num1, num2;
    > clrscr();
    > if (choice==6)
    > {
    > gotoxy(30,10);
    > printf("First Number is 1");
    > }
    >
    > gotoxy(30,12);
    > printf("Enter Second Number");
    > scanf("%d",&num2);
    > return (num2);
    > }
    >
    > int get_number3() /* Where Numbers are not to be used as Float Type */
    > {
    > int num3,num4;
    > clrscr();
    > gotoxy(30,12);
    > printf("Enter First Number");
    > scanf("%d",&num3);
    > gotoxy(30,14);
    > printf("Enter Second Number");
    > scanf("%d",&num3);
    > return (num3,num4);
    > }
    >
    > void wait()
    > {
    > gotoxy(10,24);
    > printf("developed by Vijaykumar Dave... press any key to continue.
    > ");
    > getch();
    > }


    /* BEGIN new.c */

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

    void menuprog(void);
    void wait(void);
    void get_number1(double *num_1);
    void get_number2(double *num_2, int choice);
    void get_number3(int *num_3, int *num_4);

    int main(void)
    {
    menuprog();
    puts("Have a Nice Day - Vijaykumar Dave -");
    return 0;
    }

    void menuprog(void)
    {
    double num1, num2;
    int choice, num3, num4;

    puts("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
    puts("=======================================================");
    do {
    puts("1.Adition.");
    puts("2.Subtraction.");
    puts("3.Multiplication.");
    puts("4.Division.");
    puts("5.Modular.");
    puts("6.1/x.");
    puts("7.Percentage.");
    puts("8.Exit.");
    puts("Enter your choice [1-8] : ");
    scanf("%d", &choice);
    switch (choice) {
    case 1:
    get_number1(&num1);
    get_number2(&num2, choice);
    printf("Sum = %35.2f\n", num1 + num2);
    break;
    case 2:
    get_number1(&num1);
    get_number2(&num2, choice);
    printf("Subtraction = %35.2f\n",num1-num2);
    break;
    case 3:
    get_number1(&num1);
    get_number2(&num2, choice);
    printf("Multiplication = %35.2f\n",num1*num2);
    break;
    case 4:
    get_number1(&num1);
    get_number2(&num2, choice);
    printf("Division = %35.2f\n",num1/num2);
    break;
    case 5:
    get_number3(&num3, &num4);
    printf("Modular = %d\n", num3 % num4);
    break;
    case 6:
    get_number2(&num2, choice);
    printf("1/x = %35.2f\n",1 / num2);
    break;
    case 7:
    get_number1(&num1);
    get_number2(&num2, choice);
    printf("Percentage = %35.2f\n",(num1/num2)*100);
    break;
    case 8:
    break;
    default:
    puts(" Valid range : 1-8");
    break;
    }
    } while (choice != 8);
    puts("Developed by : Vijaykumar Dave, IGNOU, BCA, IInd Semester");
    }

    void get_number1(double *num_1)
    {
    puts("Enter First Number");
    if (scanf("%lf", num_1) != 1) {
    puts("forget about it.");
    exit(EXIT_SUCCESS);
    }
    }

    void get_number2(double *num_2, int choice)
    {
    if (choice == 6) {
    puts("First Number is 1");
    }
    puts("Enter Second Number");
    if (scanf("%lf", num_2) != 1) {
    puts("forget about it.");
    exit(EXIT_SUCCESS);
    }
    }

    void get_number3(int *num_3, int *num_4)
    {
    puts("Enter First Number");
    if (scanf("%d", num_3) != 1) {
    puts("forget about it.");
    exit(EXIT_SUCCESS);
    }
    puts("Enter Second Number");
    if (scanf("%d", num_4) != 1) {
    puts("forget about it.");
    exit(EXIT_SUCCESS);
    }
    }

    /* END new.c */

    --
    pete
    pete, Apr 15, 2007
    #17
  18. On Apr 16, 2:12 am, pete <> wrote:
    > Vijaykumar Dave wrote:
    >
    > > On Apr 15, 2:55 am, Ian Collins <> wrote:
    > > > Vijaykumar Dave wrote:
    > > > > On Apr 14, 4:04 am, Ian Collins <> wrote:

    >
    > > > >>Vijaykumar Dave wrote:

    >
    > > > >>>Dear All,

    >
    > > > >>>Myproblemis regardingloop...

    >
    > > > >>>It does not work when any key other than the option 1-8 is pressed
    > > > >>>keeping program in infiniteloop

    >
    > > > >>Have you stepped through in your debugger to see why?

    >
    > > > *Please* don't quote signatures or that google quoted text crap.

    >
    > > > > ya... but it is not stopping at scanf if any alphabate is pressed...

    >
    > > > What do you expect if scanf fails? The second scanf just gets the end
    > > > of line sequence form the first. Anyway, where is theloop?

    >
    > > > --
    > > > Ian Collins.

    >
    > > sorry ian,

    >
    > > I am appending the program in which I have theprobleminloopwhich
    > > is for simple calculator. When I press the key 1-8, it runs fine, but
    > > any other key puts the program in infiniteloop. Will you help me to
    > > correct my program ?

    >
    > > Thanks for response...

    >
    > > Vijaykumar Dave

    >
    > > -------------------------

    >
    > > #include<stdio.h>
    > > #include<conio.h>

    >
    > > void menuprog();
    > > void wait();

    >
    > > int i, choice, num3, num4;
    > > float num1, num2;

    >
    > > void main()
    > > {
    > > clrscr();
    > > menuprog();
    > > gotoxy(30,12);
    > > printf("Have a Nice Day");
    > > gotoxy(30,14);
    > > printf("- Vijaykumar Dave -");
    > > }

    >
    > > void menuprog()
    > > {
    > > gotoxy(14,1);
    > > printf("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
    > > gotoxy(14,2);
    > > printf("=======================================================");

    >
    > > do
    > > {
    > > clrscr();
    > > gotoxy(20,4);
    > > printf("1.Adition.");
    > > gotoxy(20,6);
    > > printf("2.Subtraction.");
    > > gotoxy(20,8);
    > > printf("3.Multiplication.");
    > > gotoxy(20,10);
    > > printf("4.Division.");
    > > gotoxy(20,12);
    > > printf("5.Modular.");
    > > gotoxy(20,14);
    > > printf("6.1/x.");
    > > gotoxy(20,16);
    > > printf("7.Percentage.");
    > > gotoxy(20,18);
    > > printf("8.Exit.");
    > > gotoxy(20,20);
    > > printf("Enter your choice [1-8] : ");

    >
    > > gotoxy(47,20);
    > > scanf("%d",&choice);

    >
    > > if (choice<1 || choice >8)
    > > {
    > > textcolor(RED);
    > > gotoxy(20,23);
    > > cprintf(" Valid range : 1-8");
    > > textcolor(CYAN);
    > > // choice=0;
    > > }

    >
    > > switch(choice)
    > > {
    > > case 1:if(choice==1) /* Add */
    > > {
    > > num1=get_number1();
    > > num2=get_number2();
    > > printf("Sum = %35.2f\n",num1+num2);
    > > wait();
    > > }
    > > case 2:if(choice==2) /* Subtract */
    > > {
    > > num1=get_number1();
    > > num2=get_number2();
    > > printf("Subtraction = %35.2f\n",num1-num2);
    > > wait();
    > > }
    > > case 3:if(choice==3) /* Multipy */
    > > {
    > > num1=get_number1();
    > > num2=get_number2();
    > > printf("Multiplication = %35.2f\n",num1*num2);
    > > wait();
    > > }
    > > case 4:if(choice==4) /* Divide */
    > > {
    > > num1=get_number1();
    > > num2=get_number2();
    > > printf("Division = %35.2f\n",num1/num2);
    > > wait();
    > > }
    > > case 5:if(choice==5) /* Reminder */
    > > {
    > > num2=get_number3();
    > > printf("Modular = %f\n",num3%num4);
    > > wait();
    > > }
    > > case 6:if(choice==6) /* 1/x */
    > > {
    > > num2=get_number2();
    > > printf("1/x = %35.2f\n",1/num2);
    > > // wait();
    > > }
    > > case 7:if(choice==7) /* Percent */
    > > {
    > > num1=get_number1();
    > > num2=get_number2();
    > > printf("Percentage = %35.2f\n",(num1/num2)*100);
    > > // wait();
    > > }
    > > default: /* Default */
    > > break;
    > > } /* end of switch */
    > > wait(); /* Hold the Program */
    > > } while (choice!=8); /* End of Do - WhileLoop*/

    >
    > > gotoxy(11,24);
    > > printf("Developed by : Vijaykumar Dave, IGNOU, BCA, IInd Semester");
    > > }

    >
    > > int get_number1() /* Accept First Number */
    > > {
    > > float num1, num2;
    > > clrscr();
    > > gotoxy(30,12);
    > > printf("Enter First Number");
    > > scanf("%d",&num1);
    > > return (num1);
    > > }

    >
    > > int get_number2() /* Accept Second Number */
    > > {
    > > float num1, num2;
    > > clrscr();
    > > if (choice==6)
    > > {
    > > gotoxy(30,10);
    > > printf("First Number is 1");
    > > }

    >
    > > gotoxy(30,12);
    > > printf("Enter Second Number");
    > > scanf("%d",&num2);
    > > return (num2);
    > > }

    >
    > > int get_number3() /* Where Numbers are not to be used as Float Type */
    > > {
    > > int num3,num4;
    > > clrscr();
    > > gotoxy(30,12);
    > > printf("Enter First Number");
    > > scanf("%d",&num3);
    > > gotoxy(30,14);
    > > printf("Enter Second Number");
    > > scanf("%d",&num3);
    > > return (num3,num4);
    > > }

    >
    > > void wait()
    > > {
    > > gotoxy(10,24);
    > > printf("developed by Vijaykumar Dave... press any key to continue.
    > > ");
    > > getch();
    > > }

    >
    > /* BEGIN new.c */
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    >
    > void menuprog(void);
    > void wait(void);
    > void get_number1(double *num_1);
    > void get_number2(double *num_2, int choice);
    > void get_number3(int *num_3, int *num_4);
    >
    > int main(void)
    > {
    > menuprog();
    > puts("Have a Nice Day - Vijaykumar Dave -");
    > return 0;
    >
    > }
    >
    > void menuprog(void)
    > {
    > double num1, num2;
    > int choice, num3, num4;
    >
    > puts("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
    > puts("=======================================================");
    > do {
    > puts("1.Adition.");
    > puts("2.Subtraction.");
    > puts("3.Multiplication.");
    > puts("4.Division.");
    > puts("5.Modular.");
    > puts("6.1/x.");
    > puts("7.Percentage.");
    > puts("8.Exit.");
    > puts("Enter your choice [1-8] : ");
    > scanf("%d", &choice);
    > switch (choice) {
    > case 1:
    > get_number1(&num1);
    > get_number2(&num2, choice);
    > printf("Sum = %35.2f\n", num1 + num2);
    > break;
    > case 2:
    > get_number1(&num1);
    > get_number2(&num2, choice);
    > printf("Subtraction = %35.2f\n",num1-num2);
    > break;
    > case 3:
    > get_number1(&num1);
    > get_number2(&num2, choice);
    > printf("Multiplication = %35.2f\n",num1*num2);
    > break;
    > case 4:
    > get_number1(&num1);
    > get_number2(&num2, choice);
    > printf("Division = %35.2f\n",num1/num2);
    > break;
    > case 5:
    > get_number3(&num3, &num4);
    > printf("Modular = %d\n", num3 % num4);
    > break;
    > case 6:
    > get_number2(&num2, choice);
    > printf("1/x = %35.2f\n",1 / num2);
    > break;
    > case 7:
    > get_number1(&num1);
    > get_number2(&num2, choice);
    > printf("Percentage = %35.2f\n",(num1/num2)*100);
    > break;
    > case 8:
    > break;
    > default:
    > puts(" Valid range : 1-8");
    > break;
    > }
    > } while (choice != 8);
    > puts("Developed by : Vijaykumar Dave, IGNOU, BCA, IInd Semester");
    >
    > }
    >
    > void get_number1(double *num_1)
    > {
    > puts("Enter First Number");
    > if (scanf("%lf", num_1) != 1) {
    > puts("forget about it.");
    > exit(EXIT_SUCCESS);
    > }
    >
    > }
    >
    > void get_number2(double *num_2, int choice)
    > {
    > if (choice == 6) {
    > puts("First Number is 1");
    > }
    > puts("Enter Second Number");
    > if (scanf("%lf", num_2) != 1) {
    > puts("forget about it.");
    > exit(EXIT_SUCCESS);
    > }
    >
    > }
    >
    > void get_number3(int *num_3, int *num_4)
    > {
    > puts("Enter First Number");
    > if (scanf("%d", num_3) != 1) {
    > puts("forget about it.");
    > exit(EXIT_SUCCESS);
    > }
    > puts("Enter Second Number");
    > if (scanf("%d", num_4) != 1) {
    > puts("forget about it.");
    > exit(EXIT_SUCCESS);
    > }
    >
    > }
    >
    > /* END new.c */
    >
    > --
    > pete- Hide quoted text -
    >
    > - Show quoted text -


    This also have same problem... when any key other than 1-8 is pressed,
    program goes to infinite loop.
    Vijaykumar Dave, Apr 17, 2007
    #18
  19. Vijaykumar Dave

    pete Guest

    Vijaykumar Dave wrote:
    >
    > On Apr 16, 2:12 am, pete <> wrote:


    > > /* BEGIN new.c */


    > > puts("Enter your choice [1-8] : ");
    > > scanf("%d", &choice);
    > > switch (choice) {


    > This also have same problem... when any key other than 1-8 is pressed,
    > program goes to infinite loop.


    Sorry about that.
    Change the above quoted lines of code to:

    puts("Enter your choice [1-8] : ");
    if (scanf("%d", &choice) != 1) {
    choice = 9;
    if (!feof(stdin)) {
    getchar();
    }
    }
    switch (choice) {

    --
    pete
    pete, Apr 18, 2007
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Konark Khera
    Replies:
    0
    Views:
    485
    Konark Khera
    Mar 22, 2005
  2. Anubhav Jain
    Replies:
    0
    Views:
    428
    Anubhav Jain
    Mar 20, 2006
  3. facing a problem

    , Dec 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    393
    =?Utf-8?B?U2h5YW0gUmVtZWxsYQ==?=
    Dec 27, 2003
  4. Amratash
    Replies:
    0
    Views:
    497
    Amratash
    Apr 13, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    348
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page