Facing Problem in Loop

V

Vijaykumar Dave

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));
}

W

Walter Roberson

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.

W

Walter Roberson

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.

K

Kelsey Bjarnason

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:

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

M

Martin Ambuhl

Vijaykumar said:
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,
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
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 said:
> 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.

O

Old Wolf

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.

K

Kelsey Bjarnason

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.

V

Vijaykumar Dave

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

I

Ian Collins

Vijaykumar said:
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?

V

Vijaykumar Dave

Have you stepped through in your debugger to see why?

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

I

Ian Collins

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?

V

Vijaykumar Dave

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?

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 wait();

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

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

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

do
{
clrscr();
gotoxy(20,4);
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)
{
{
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();
}

O

osmium

:

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?

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 wait();

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

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

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

do
{
clrscr();
gotoxy(20,4);
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>

C

CBFalconer

Vijaykumar said:
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

No such include file in standard C
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

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

F

Flash Gordon

Vijaykumar Dave wrote, On 15/04/07 19:52:
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

conie.h is not a standard header, so it and anything to do with it is
not topical here.
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.

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.

R

P

pete

Vijaykumar said:

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?

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 wait();

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

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

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

do
{
clrscr();
gotoxy(20,4);
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)
{
{
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 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)
{
puts("Have a Nice Day - Vijaykumar Dave -");
return 0;
}

{
double num1, num2;
int choice, num3, num4;

puts("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
puts("=======================================================");
do {
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) {
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) {
exit(EXIT_SUCCESS);
}
}

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

/* END new.c */

V

Vijaykumar Dave

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

void wait();
int i, choice, num3, num4;
float num1, num2;
void main()
{
clrscr();
gotoxy(30,12);
printf("Have a Nice Day");
gotoxy(30,14);
printf("- Vijaykumar Dave -");
}
{
gotoxy(14,1);
printf("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
gotoxy(14,2);
printf("=======================================================");
do
{
clrscr();
gotoxy(20,4);
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)
{
{
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 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)
{
puts("Have a Nice Day - Vijaykumar Dave -");
return 0;

}

{
double num1, num2;
int choice, num3, num4;

puts("Program to Calculate +, -, *, /, Reminder, 1/x, Percent");
puts("=======================================================");
do {
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) {
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) {
exit(EXIT_SUCCESS);
}

}

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

}

/* END new.c */

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

P

pete

Vijaykumar said:
/* 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.

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) {

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.