473,839 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_N O" 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<iostre am.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",&cho ice);

if(choice==1) {
push(stk_ptr);
printf("\nTo continue press 1 to exit 0:");
scanf("%d",&opt ion);
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("\nOverf low 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("\neleme nt 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_N O" or "i" the program is
executing infiniteloop.

thaking you.
regards
thandra.

May 31 '07 #1
5 1512
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_N O" 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<iostre am.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",&cho ice);
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",&opt ion);
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("\nOverf low 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("\neleme nt 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_N O" 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**********@h p.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)gma il.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_N O" 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<iostre am.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.securityfoc us.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.comwro te:
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_N O" 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<iostre am.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",&cho ice);
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",&opt ion);
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("\nOverf low 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("\neleme nt 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_N O" 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
11260
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) on the server because of that. Our site will have an SSL certificate next week, so I would like to use AIM instead of SIM, however, I don't know how to send data via POST over https and recieve data from the Authorize.net server over an https...
2
5860
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 to execute the code until the browser send his reply to the header instruction. So an exit(); after each redirection won't hurt at all
3
23048
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 field is completed.
0
8504
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. 354 roberto@ausone:Build/php-4.3.2> ldd /opt/php4/bin/php libsablot.so.0 => /usr/local/lib/libsablot.so.0 libstdc++.so.5 => /usr/local/lib/libstdc++.so.5 libm.so.1 => /usr/lib/libm.so.1
1
8617
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 column below. The viewer can select states from the drop down lists above the other two columns as well. If the viewer selects only one, only one column fills. If the viewer selects two states, two columns fill. Etc. I could, if appropriate, have...
4
18313
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 user comes back to a page where he had a submitted POST data the browser keeps telling that the data has expired and asks if repost. How to avoid that? I tried registering all POST and GET vars as SESSION vars but
1
6882
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 http://www.mis.gla.ac.uk/biquery/training/ but each of the courses held have maximum of 8 people that could be
2
31455
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 to :parameter I dont like the idea of making the SQL statement on the fly without binding parameters as I dont want a highly polluted SQL cache.
3
23612
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 results of the picture half the size. The PHP I have installed support 1.62 or higher. And all I would like to do is take and image and make it fit a 3x3. Any suggestions to where I should read or look would be appreciated.
0
9697
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10587
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10649
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7829
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7018
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.