473,385 Members | 1,798 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,385 software developers and data experts.

problem in Loop

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

Apr 15 '07 #1
4 1876
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.

--
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 #2
Vijaykumar Dave wrote:
>
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

--
Posted via a free Usenet account from http://www.teranews.com

Apr 15 '07 #3
Vijaykumar Dave <vi************@gmail.comwrote:
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Apr 15 '07 #4
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;

Apr 17 '07 #5

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
4
by: JP SIngh | last post by:
Thanks to Manohar for writing the basic code for displaying the managers and the employees in a tree like structure. I have adapted the code below but it gives me an error "exception occcured"...
2
by: Xiaozhu | last post by:
I trid to use icc -axW to optimize the code, but it becomes much slower... 8sec/37sec before and after using this option. my cpu is Intel(R) Pentium(R) 4 CPU 2.40GHz the test code is just one...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
8
by: Jeff | last post by:
Hello everybody, I was doing one of the exercises in the K&R book, and I got something really strange. Here's the source code: /* * Exercise 2-2 from the K&R book, page 42 */ #include...
47
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
8
by: koorb | last post by:
I am starting a program from a module with the Sub main procedure and I want it to display two forms for the program's interface, but when I run the program both forms just open and then program...
11
by: felixnielsen | last post by:
What i have is not even a real problem, but i hope someone can help anyway, but first a piece of code_ @code start const unsigned short Size = 2; // 2^N std::bitset<Size*Size*Size> YZ;...
1
by: Promextheus Xex | last post by:
First php post... Hi there, I'm writing some code for my website to list people listening to my music. I have a loop for an array that dosen't seem to display all people. most of the time it...
5
by: =?Utf-8?B?Z215ZXJz?= | last post by:
Hello, I am attempting to start a cmd.exe process and pass several .vbs scripts (with additional parameters) and then read the output from the scripts and make "notes" in a DataTable (the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.