problem in Loop

V

Vijaykumar Dave

Here is a program of calculator.

I have a proble in loop. When key 1-8 is pressed, it works ok but if
any character key is pressed, it goes to infinite loop. will any one
help me to correct my program?

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

Richard Heathfield

Vijaykumar Dave said:
Here is a program of calculator.

I have a proble in loop. When key 1-8 is pressed, it works ok but if
any character key is pressed, it goes to infinite loop. will any one
help me to correct my program?

#include<conio.h>

This header, and your dependency on it, stop me from conveniently
compiling your program, so I'll just have to rely on eyesight.
void main()

The best advice I can give you is to get better learning resources.
Whatever resource you used to learn C has given you poor service. I
recommend "The C Programming Language", 2nd edition, by Kernighan and
Ritchie.
 
C

CBFalconer

Vijaykumar said:
I have a proble in loop. When key 1-8 is pressed, it works ok but
if any character key is pressed, it goes to infinite loop. will
any one help me to correct my program?

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

No such file in standard C.
void menuprog();
void wait();

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

void main()

main always returns an int. say so.
{
clrscr();

No such routine in standard C.
menuprog();
gotoxy(30,12);

No such routine in standard C.
printf("Have a Nice Day");
gotoxy(30,14);

No such routine in standard C.
printf("- Vijaykumar Dave -");

Failure to return the required int.

It is also considered extremely disorganized to multi-post. You
can cross-post if you set follow-ups immediately.

--
<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
 
J

Jens Thoms Toerring

Vijaykumar Dave said:
Here is a program of calculator.
I have a proble in loop. When key 1-8 is pressed, it works ok but if
any character key is pressed, it goes to infinite loop. will any one
help me to correct my program?

Others already have told you about the various problems with
using non-standard headers etc.. But the reason for your problem
is probably right here:
scanf("%d",&choice);

What do you expect scanf() to do when it doesn't find an integer
as the input? You seem to assume that it throws away the input.
But it doesn't. Instead it simply returns (telling you that it
couldn't find an integer if you would care to check the return
value) and leaves the input unchanged. And since you don't deal
with this "wrong" input it gets checked again for an integer the
next time through the loop with, of course, the same result, i.e.
failure to find an int and thus leaving the input unchanged. Re-
peat ad nauseam...

You have two alternatives for dealing with the problem: you
could either read in the whole line the user entered and
then, at your leisure, check if it contains an integer (per-
haps also dealing gracefully with input like " 10 " etc.,
using e.g. strtol(), which also allows more error checking).
Or you could check the return value of scanf() and, if it's
0, indicating that the input wasn't an int, you could throw
away the whole line by repeatedly reading single chars from
the input until you find a '\n' (or EOF) - or gobble a single
char and try scanf() again until you either reach the end of
the line or scanf() succeeds.

Regards, Jens
 
E

Eric

Let me add a suggestion, I'm not solving your problem but
your use of switch is rather odd. Please review my changes
below and read up on switch and how it works
Eric
switch(choice)
{
case 1: you dont need this "if(choice==1)" /* Add */
or this: "{"
num1=get_number1();
num2=get_number2();
printf("Sum = %35.2f\n",num1+num2);
wait();
or this "}"
but you should have this break;
case 2: or this either "if(choice==2)" /* Subtract */
or this: "{"
num1=get_number1();
num2=get_number2();
printf("Subtraction = %35.2f\n",num1-num2);
wait();
or this "}"
but you should have this
break;
 

Ask a Question

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

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top