473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Facing Problem in Loop

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

Apr 11 '07 #1
18 2067
In article <11**********************@e65g2000hsc.googlegroups .com>,
Vijaykumar Dave <vi************@gmail.comwrote:
>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
Apr 11 '07 #2
In article <11**********************@e65g2000hsc.googlegroups .com>,
Vijaykumar Dave <vi************@gmail.comwrote:
>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
Apr 11 '07 #3
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.
Apr 11 '07 #4
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.hand 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));
}
Apr 11 '07 #5
On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:
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.

Apr 12 '07 #6
On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:
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.
Apr 12 '07 #7
On Apr 13, 4:19 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:
On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:
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

Apr 13 '07 #8
Vijaykumar Dave wrote:
On Apr 13, 4:19 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:
>>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
>>>On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:

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.
Apr 13 '07 #9
On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.comwrote:
Vijaykumar Dave wrote:
On Apr 13, 4:19 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:
>On Thu, 12 Apr 2007 15:32:33 -0700, Old Wolf wrote:
>>On Apr 12, 9:45 am, Kelsey Bjarnason <kbjarna...@ncoldns.comwrote:
>>> 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...

Apr 14 '07 #10
Vijaykumar Dave wrote:
On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.comwrote:
>>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.
Apr 14 '07 #11
On Apr 15, 2:55 am, Ian Collins <ian-n...@hotmail.comwrote:
Vijaykumar Dave wrote:
On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.comwrote:
>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();
}

Apr 15 '07 #12
"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>
Apr 15 '07 #13
Vijaykumar Dave wrote:
>
On Apr 15, 2:55 am, Ian Collins <ian-n...@hotmail.comwrote:
Vijaykumar Dave wrote:
On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.comwrote:
>>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

Apr 15 '07 #14
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.
Apr 15 '07 #15
Vijaykumar Dave wrote, On 15/04/07 19:52:
On Apr 15, 2:55 am, Ian Collins <ian-n...@hotmail.comwrote:
>Vijaykumar Dave wrote:
>>On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.comwrote:
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
Apr 15 '07 #16
Vijaykumar Dave wrote:
>
On Apr 15, 2:55 am, Ian Collins <ian-n...@hotmail.comwrote:
Vijaykumar Dave wrote:
On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.comwrote:
>>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
Apr 15 '07 #17
On Apr 16, 2:12 am, pete <pfil...@mindspring.comwrote:
Vijaykumar Dave wrote:
On Apr 15, 2:55 am, Ian Collins <ian-n...@hotmail.comwrote:
Vijaykumar Dave wrote:
On Apr 14, 4:04 am, Ian Collins <ian-n...@hotmail.comwrote:
>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.
Apr 17 '07 #18
Vijaykumar Dave wrote:
>
On Apr 16, 2:12 am, pete <pfil...@mindspring.comwrote:
/* 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
Apr 18 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jitendra Shinde | last post by:
I am facing a problem on Session. Firstly i am getting log in the application and woring with my some windows. now keeping that other window open and now from that main window i am logging...
0
by: John | last post by:
Internet Facing SQL Server, Login Security? How? I would think that our Project is fairly common. We're stuck on a security question. We have a typical SQL Server 2000, running under Windows...
2
by: Uma Abhyankar | last post by:
Hello, We are facing precision issues with addition, multiplication or division of "double" Issue1: ##### The output of 0.1 + 0.2 is not 0.3 It is 0.30000000000000004 How do we tackle this??
22
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
5
by: psbasha | last post by:
Hi, Currently I am using Active Python 2.4.2 IDLE.I am facing the following issue. - When I modify the code in different modules and execute the main module ,and if any error is coming in some...
3
by: yassinsearch | last post by:
Hi , I am facing problem text direction.(for HEBREW lanaguage) By default the direction of text field is Left to Rignt. Any SRW methods is available to set the reading direction from Right to...
0
by: Niks | last post by:
I am facing a very strange problem in my websevice which is working on socket communication. While i am publishing my webservice to the IIS and invoking a method, a data loss problem in occuring....
1
by: manojnkumar | last post by:
#This is to add, sub, mul and div using functions def menu(): print "" print "Welcome to the calculator" print "===== These are some of the options =====" print "1-->...
3
by: coolestavi007 | last post by:
Hi All, I am facing a problem in Ziping any file and then sending it to another server through FTP. What I have to actually do is to retrive the property of files of any directory and then find...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.