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