473,402 Members | 2,050 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,402 software developers and data experts.

can any bdy help me

hai,
i have developed a c-code to push integers into a stack (an array).
in the program there are three inputs which has to be given through
keyboard. the inputs are "choice", "option","S_R_NO" and "i" all are
defined as "int".
now when the inputs given are int type the program behaviour is as
required. but when the input given are char type the program is
executing infinite loop.i could not understand this behaviour and i am
unable to start. the code is developed on turbo-c and it is
executiable.

can any one help me in finnding the bug and tell me the better way of
doing.
----------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<iostream.h>

#define SIZE 6
int top=-1;

void main()
{
void push(int*);
int stack[SIZE], *stk_ptr;
int choice;
int S_R_NO,option;
stk_ptr=stack;

start: //here it starts
clrscr();
printf("\nEnter 1 to continue: ");
scanf("%d",&choice);

if(choice==1) {
push(stk_ptr);
printf("\nTo continue press 1 to exit 0:");
scanf("%d",&option);
if(option==1) goto start;
}
else{
printf("CAUTION:WRONG CHOICE, ENTER 1 TO CONTINUE 0 TO EXIT:
");
fflush(stdin);
scanf("%d",&S_R_NO);
if(S_R_NO==1) goto start;
}

end:
printf("\n------END OF THE PROGRAM-----");
getch();
}

/
*----------------------------------------------------------------------
*/

void push(int *stk)
{
register int i,j;
top+=1;
if(top==SIZE){ printf("\nOverflow of the stack");top-=1; }
else { printf("\nEnter the element you want to push: ");
scanf("%d",&i);
*(stk+top)=i;
printf("now the stack is:");
for(j=0;j<=top;j++)
printf("\nelement at <%dis <%d>",j,*(stk+j));
}
}
/
*---------------------------------------------------------------------------
*/
runtime problems:
1) first round input: if a char is entered for "i" program terminating
without asking for the next value of "option".
2) after first round of input: if char is given as input for any of
the variables "choice", "option","S_R_NO" or "i" the program is
executing infiniteloop.

thaking you.
regards
thandra.

May 31 '07 #1
5 1489
shanti wrote:
i have developed a c-code to push integers into a stack (an array).
in the program there are three inputs which has to be given through
keyboard. the inputs are "choice", "option","S_R_NO" and "i" all are
defined as "int".
now when the inputs given are int type the program behaviour is as
required. but when the input given are char type the program is
executing infinite loop.i could not understand this behaviour and i am
unable to start. the code is developed on turbo-c and it is
executiable.

can any one help me in finnding the bug and tell me the better way of
doing.
----------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
Non-standard and unnecessary header.
#include<iostream.h>
Ditto.
#define SIZE 6
int top=-1;
Global variable for stack index: bad idea. Keep it local
to `main` and have `push` take an extra parameter. Later
when you know about `struct` you'll be able to keep the
index and the stack (and the size) all together.
void main()
The portable return type for `main` is `int`.
{
void push(int*);
Usual style is to have function declarations outside
functions.
int stack[SIZE], *stk_ptr;
int choice;
int S_R_NO,option;
All these uninitialised variables make me nervous.
stk_ptr=stack;

start: //here it starts
You don't need labels-and-gotos for this; that's what a
`while` loop is `for`. One so rarely needs a `goto` in
C that a good rule of thumb is `never use goto`; eventually
you will discover circumstances where it's the preferred
construct. Eventually.
clrscr();
Non-standard, unnecessary, and IMAO unforgivable here.
printf("\nEnter 1 to continue: ");
scanf("%d",&choice);
There's now stuff in the input stream -- at least a newline.
What do you plan to do if the user doesn't input a digit?
Why enter /1/ to continue -- instead of `y`, say? And
why "continue" when we haven't really started yet?
if(choice==1) {
push(stk_ptr);
printf("\nTo continue press 1 to exit 0:");
scanf("%d",&option);
if(option==1) goto start;
See above.
}
else{
printf("CAUTION:WRONG CHOICE, ENTER 1 TO CONTINUE 0 TO EXIT:
");
DON'T SHOUT AT ME, YOU STUPID MACHINE.

Um. If you weren't going to let me not-continue, why did you
offer me a choice in the first place?
fflush(stdin);
It's undefined behaviour to fflush an input stream. Don't do that.
(You want to eat characters until a newline, as a best approximation.)
scanf("%d",&S_R_NO);
if(S_R_NO==1) goto start;
}

end:
printf("\n------END OF THE PROGRAM-----");
DON'T SHOUT AT ME, YOU STUPID MACHINE.

You say that, but ...
getch();
.... means that it /isn't/ the end.
}

/
*----------------------------------------------------------------------
*/

void push(int *stk)
Thr's n shrtg f vwls n snt. Call it `stack`.
{
register int i,j;
`register` isn't terribly useful here.
top+=1;
if(top==SIZE){ printf("\nOverflow of the stack");top-=1; }
else { printf("\nEnter the element you want to push: ");
`value` is better than `element`. (Usually `element` refers to the
position in the stack, but that's not what you're asking for.)
scanf("%d",&i);
See above.
*(stk+top)=i;
Um, this is idiomatically written `stk[top] = i;`. Why are you using
the `*(stk+top)` form?
printf("now the stack is:");
for(j=0;j<=top;j++)
printf("\nelement at <%dis <%d>",j,*(stk+j));
(indent) (ditto)
}
}
/
*---------------------------------------------------------------------------
*/
runtime problems:
1) first round input: if a char is entered for "i" program terminating
without asking for the next value of "option".
2) after first round of input: if char is given as input for any of
the variables "choice", "option","S_R_NO" or "i" the program is
executing infiniteloop.
Yes. You don't check for these possibilities, so when they happen,
things go badly.

--
"We are on the brink of a new era, if only --" /The Beiderbeck Affair/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 31 '07 #2
Chris Dollin <ch**********@hp.comwrote:
shanti wrote:
int stack[SIZE], *stk_ptr;
int choice;
int S_R_NO,option;
All these uninitialised variables make me nervous.
I'm not sure what one would initialize choice and option to; from
their names alone it would seem to be clear that they are intended to
store input from the user; my religion indicates that initialization
is therefore optional at best. (S_R_NO, on the other hand, might be
profitably initialized, although the question of "to what" is not easy
to answer without deciphering its very poor name.)
DON'T SHOUT AT ME, YOU STUPID MACHINE.
My Commodore 64 shouted at me a lot, and was only pacified by magical
incantations such as "LOAD *, 8, 1" (or some such nonsense) :-)
Um. If you weren't going to let me not-continue, why did you
offer me a choice in the first place?
For the same reason that users always got the option to "abort, retry,
fail" no matter how much the real choices might have been "fail, fail,
fail"...

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
May 31 '07 #3
"Chris Dollin" writes:
Um. If you weren't going to let me not-continue, why did you
offer me a choice in the first place?
He may have interned at Microsoft. They are experts at promising things
that won't happen. Press "Cancel" on a download for example.
May 31 '07 #4
shanti wrote:
>
i have developed a c-code to push integers into a stack (an array).
in the program there are three inputs which has to be given through
keyboard. the inputs are "choice", "option","S_R_NO" and "i" all are
defined as "int".
now when the inputs given are int type the program behaviour is as
required. but when the input given are char type the program is
executing infinite loop.i could not understand this behaviour and i
am unable to start. the code is developed on turbo-c and it is
executiable.

can any one help me in finnding the bug and tell me the better way of
doing.
-------------------------------------------------------------------
#include<stdio.h>
This is OK
#include<conio.h>
#include<iostream.h>
These don't exist in standard C. You would also be better off
inserting spaces, as "#include <stdio.h>".

Thus no answer is available here, where we deals solely with
standard C as described in the C standard. Look for some newsgroup
with Borland or Turbo in its name. Or, better, look for an
up-to-date compiler.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

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

May 31 '07 #5
On 31 May, 11:53, shanti <prasanth.than...@gmail.comwrote:
hai,
i have developed a c-code to push integers into a stack (an array).
in the program there are three inputs which has to be given through
keyboard. the inputs are "choice", "option","S_R_NO" and "i" all are
defined as "int".
now when the inputs given are int type the program behaviour is as
required. but when the input given are char type the program is
executing infinite loop.i could not understand this behaviour and i am
unable to start. the code is developed on turbo-c and it is
executiable.

can any one help me in finnding the bug and tell me the better way of
doing.
Well, you could start by reading the FAQ - http://c-faq.com/stdio/scanfprobs.html
would probably be a good start.

Reading a decent book on C coding (or coding in general, actually)
would also be useful.
----------------------------------------------------------------------------------
#include<stdio.h>
OK
#include<conio.h>
#include<iostream.h>
Non-standard and not really necessary, as far as I can tell.
>
#define SIZE 6
int top=-1;

void main()
the standard says otherwise.
{
void push(int*);
int stack[SIZE], *stk_ptr;
int choice;
int S_R_NO,option;
stk_ptr=stack;

start: //here it starts
It's better to use "/*...*/" for comments. Many compilers don't accept
this form, and it's poor for cutting and pasting from newsgroup posts.
clrscr();
I will assume this clears the screen. It's not
printf("\nEnter 1 to continue: ");
scanf("%d",&choice);
You don't check the return value of scanf() - a Bad Thing.
What will scanf do with non-numeric input? (Hint: probably not what
you want). Read the FAQ.
if(choice==1) {
push(stk_ptr);
printf("\nTo continue press 1 to exit 0:");
scanf("%d",&option);
if(option==1) goto start;
"Any references to Goto (an obscure Japanese admiral) are obscene,
unfit for your eyes and anyway don't mean what you think they do -
delete them" (notes on the Algol manual from a university course in
the 1970s).

You almost never need a goto in C.
}
else{
printf("CAUTION:WRONG CHOICE, ENTER 1 TO CONTINUE 0 TO EXIT:
");
fflush(stdin);
According to the standard, this is meaningless.
scanf("%d",&S_R_NO);
Same comments as before about scanf().
if(S_R_NO==1) goto start;
}

end:
As far as I can see you never use this label.
printf("\n------END OF THE PROGRAM-----");
getch();

}
Your formatting (indentation) is appalling. Don't you have a tool to
do it, perhaps in your editor?
>
/
*----------------------------------------------------------------------
*/

void push(int *stk)
{
register int i,j;
top+=1;
if(top==SIZE){ printf("\nOverflow of the stack");top-=1; }
else { printf("\nEnter the element you want to push: ");
scanf("%d",&i);
*(stk+top)=i;
printf("now the stack is:");
for(j=0;j<=top;j++)
printf("\nelement at <%dis <%d>",j,*(stk+j));
}}
Are you kidding?
1) The formatting of the code is awful
2) the mixing of interaction with the user and manipulation of the
stack is hopeless.

The natural prototype for push() in your code is much more likely to
be
int push(int *stack, int value); /* return 1 for success, 0 for
failure (overflow) */

/
*---------------------------------------------------------------------------
*/
runtime problems:
1) first round input: if a char is entered for "i" program terminating
without asking for the next value of "option".
Read the FAQ - and perhaps the standard - and find out what scanf()
would return in this case.
2) after first round of input: if char is given as input for any of
the variables "choice", "option","S_R_NO" or "i" the program is
executing infiniteloop.
Similarly.

Use something other than scanf() for a start...

In your position, I'd start by writing code to maintain a stack - with
push and pop operations, and write a simple non-interactive testcase
to exercise it, the only I/O would be a few printf() statements to
show the state of the stack at various points in the code.

Once I'd done that, I'd look at how to add some interactive code to my
testcase.

May 31 '07 #6

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
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
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
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.