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

what's wrong?

[CODE]../* Program function: Simulate the stack using a stack limit of 10. Display
a menu for the the following.
[C] Create a stack
[i] Insert an item in the stack
[P] Pop an item from the stack
[E] Peep/view the item at the top of the stack
[V] Current value of top
[W] View all items stored in the stack
[Q] Quit
*the stack accepts lowercase & uppercase characters only from A-Z
*any other symbol (e.g. punctuation marks, numbers, etc. should not be accepted by the program
*the stack should not accept DUPLICATE ENTRIES
Note:
*[C]create a stack
-deletes all items in the stack after a confirmation if
there are existing items stored in it.
*Display appropriate messages esp when
* trying to insert an item in a stack that is already full
* trying to pop items from the stack that is empty
* duplicate items in the stack ( note: lowercase and
uppercase letters are accepted as two different entries)
*insertion of invalid values
*/

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define stacklimit 10
int top,i;
typedef enum boolean{FALSE, TRUE} boolean;

wrong(int error)
{
printf("\n");
switch(error)
{
case 0: printf("Incorrect input\n"); break;
case 1: printf("Please enter only Y or N\n"); break;
case 2: printf("Please enter a letter from A-Z\n"); break;
case 3: printf("Input is already in stack\n"); break;
}
}

createstack(char stack[])
{
for(i=0; i<stacklimit; i++)
stack[i]=NULL;
return stack[];
}
char initialize_stack(char stack[])
{
int error=0;
char choice= 'Y';
clrscr();
if(top!=-1)
{
do{
printf("Do you want to reintialize the stack? [Y/N]");
choice=toupper(getch());
if((choice!='Y')||(choice!='N'))
{ error=1;
wrong(error);
}
else;
}while(error!=0);
if(choice=='Y')
{ top=-1;
createstack(stack);
return stack[];
}
else;
}
else
{ top=-1;
createstack(stack);
return stack[];
}
}

int FULLSTACK()
{ int x;
if(top==stacklimit-1)
{ x=1;
return x;
}
else
{ x=0;
return x;
}
}

int checker(char input, char stack[])
{ int flag=0;
for(i=0;i<stacklimit;i++)
{ if(stack[i]==input)
flag=1;
}
return flag;
}

push(char input, char stack[])
{
char temp_stack[stacklimit];
int x=1;
top+=1;
for(i=0;i<stacklimit;i++)
temp_stack[i]=stack[i];
stack[0]=input;
for(i=0;i<stacklimit;i++)
{ stack[x]=temp_stack[i];
x++;
}
return stack[];
}

input_a(char stack[])
{
int error=0, full;
char input,check;
full=FULLSTACK();
if(full==1)
{ printf("Stack is already full");
return;
}
else
{
do{
printf("Please enter a letter from A-Z: ");
scanf("%c", &input);
check=toupper(input);
if(check!='A'||check!='B'||check!='C'||check!='D'| |
check!='E'||check!='F'||check!='G'||check!='H'||ch eck
!='I'||check!='J'||check!='K'||check!='L'||check!= 'M'
||check!='N'||check!='O'||check!='P'||check!='Q'||
check!='R'||check!='S'||check!='T'||check!='U'||ch eck
!='V'||check!='W'||check!='X'||check!='Y'||
check!='Z')
{ error=2;
wrong(error);
}
else if(checker(input, stack))
{
error=3;
wrong(error);
}
else;
}while(error!=0);
push(input, stack);
}
}

boolean EMPTYSTACK()
{
if(top==-1)
return TRUE;
else
return FALSE;
}

char pop(char stack[])
{
char x=0;
boolean empty;

empty=EMPTYSTACK();
if(empty==TRUE)
{
printf("Stack is empty");
getch();
return x;
}
else
{
x=stack[top];
top-=1;
return x;
}
}

view(char stack[])
{
for(i=0;i<stacklimit;i++)
printf("\n %d", stack[i]);
}
main()
{
int quit=0,error;
char choice , stack[stacklimit];
createstack(stack);
do{
clrscr();
printf("Main Menu");
printf("\n[C] Create a stack");
printf("\n[i] Insert an item in the stack");
printf("\n[P] Pop an item from the stack");
printf("\n[E] Peep/View the item at the top of the stack");
printf("\n[V] Current value of top");
printf("\n[W] View all items stored in the stack");
printf("\n[Q] Quit\n");
choice=toupper(getch());
switch(choice)
{
case 'C': initialize_stack(stack); break;
case 'I': input_a(stack); break;
case 'P': pop(stack); break;
case 'E': printf("\n%d", stack[0]); break;
case 'V': printf("\n%i", top); break;
case 'W': view(stack); break;
case 'Q': quit=1; break;
default: error=0; wrong(error); getch(); break;
}
}while(quit==0);
}..[\CODE]
Oct 7 '06 #1
3 2717
tyreld
144 100+
That is a good question. Does your code compile? If not what errors does it give you? If it does what kind of runtime problems do you have? This is information you should supply if you expect people to actually look at your code and help you fix it.

Just glancing at your code I can say this.

Several of your functions are missing return types. If you don't include a return type in the function signature the compiler will assume "int". On a poorly implemented compiler you might even get undefined behavior. To say the least it is poor style to not include a return type. If you are returning nothing then the return type should be "void".

if you aren't including a block of code in an "else" statement then you don't need it (ie "else;" is not necessary).

You can't return arrays in the manner you are trying to. Actually, you don't need to return the array. Unless you want to start dealing with pointers. Arrays in C are past by reference. So, any changes you make in your function effect the original array you passed into the function.

Finally, why not use "isalpha" which is defined in "ctype.h" for testing for a-zA-z instead of that ugly if condition you are using?
Oct 8 '06 #2
the problem is that it wont execute due to the ff. errors:

the return array thing

and something about the function prototypes of the boolean FULLSTACK and EMPTYSTACK not being defined in other functions

as for the isalpha, our teacher didn't discuss that at all...
Oct 8 '06 #3
never mind, figured it out by myself :), thanks for the tip about isalpha though, it really helped
Oct 8 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.