My static stack code is not running properly - c

#include <stdio.h>
#include <stdlib.h>
#define N 5
int stack[N];
int top = -1;
void push()
{
int x;
printf("Enter data");
scanf("%d", &x);
if (top == N - 1)
{
printf("Overflow");
}
else
{
top++;
stack[top] = x;
}
}
void pop()
{
int item;
if (top == -1)
{
printf("Underlfow");
}
else
{
item = stack[top];
top--;
printf("%d", item);
}
}
void peek()
{
if (top == -1)
{
printf("Underflow");
}
else
{
printf("%d", stack[top]);
}
}
void display()
{
int i;
for (i = top; i >= 0; i--)
{
printf("%d\n", stack[top]);
}
}
int main()
{
int ch;
do
{
printf("\n1.push\n2.pop\n3.peek\n4.display");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("invalid key pressed");
break;
}
} while (ch != 0);
return 0;
}
So it is the code which i wrote following a tutorial on youtube
In push function if i excced the array size which is 5 it will still take values instead of printing overflow and when i try to display it, it will display all values same
before i was geting a error at getch(); while using void main() so i change it to int main() and used return 0; it is still not working.

So it is the code which i wrote following a tutorial on youtube In push function if i excced the array size which is 5 it will still take values instead of printing overflow and when i try to display it, it will display all values same
Your logic is to always allow you to enter values. But they are not inserted in the stack (and instead "Overflow" is printed). This is because you ask for the value before checking if the stack is full. Just make the test before, and put the scan inside the else part of the if statement.
void push()
{
int x;
if (top == N - 1) {
/* put \n chars at end of lines in output statements
* like this ---vv */
printf("Overflow\n");
} else {
printf("Enter data ");
fflush(stdout);
scanf("%d", &x);
top++;
stack[top] = x;
}
}
The problem of printing always the top of the stack is basically that you print stack[top], instead of stack[i], in the display() function loop. You should use this code instead.
void display()
{
int i;
for (i = top; i >= 0; i--)
{
/* use i here --------------v */
printf("%d: %d\n", i, stack[i]);
}
}
This is not an error, but will save you trouble in the future. Get used to put the \n char at the end of the printing format, instead of at the beginning, if you are going to print a complete line. I understand that you want to avoid it when prompting the user, so you avoid it. But the stdio library uses buffering, so it doesn't print things when you ask it for, so it delays the printing of strings (on an interactive session) until you do printf a '\n' char (if the output device is a terminal), or before reading from stdin (also, if the input device is a terminal). This can make a mess if you print your strings without the trailing '\n'. And more, if you redirect your output, this means using a pipe (so, it is not a terminal). Then, no output is done until the buffer fills completely (meaning over 10kb of data, usually 16kb on modern unix/linux systems) You will see your program doing things while no output has been output, and you won't know why.
You don't use getch() in your code, so i think you have posted a different version of the code you are talking about.
My code, after edition, leads to:
#include <stdio.h>
#include <stdlib.h>
#define N 5
int stack[N];
int top = -1;
void push()
{
if (top == N - 1) {
/* no overflow yet, not if we check beforehand :) */
printf("Stack full, cannot push()\n");
} else {
int x;
printf("Enter data ");
fflush(stdout); /* to flush the output (no \n at end) */
scanf("%d", &x);
top++;
stack[top] = x;
printf("Push: %d to position %d\n", x, top);
}
}
void pop()
{
if (top == -1) {
/* the appropiate message is stack empty, no underflow has
* occured yet. We are preventing it */
printf("Stack empty, cannot pop()\n");
} else {
int item = stack[top];
printf("Pop: %d from pos %d\n", item, top);
top--;
}
}
void peek()
{
if (top == -1) {
/* no underflow yet, we are preventing it */
printf("Stack empty, cannot peek()\n");
} else {
printf("Peek: %d on pos %d\n", stack[top], top);
}
}
void display()
{
int i;
printf("Stack contents:\n");
for (i = top; i >= 0; i--) {
printf("> %d: %d\n", i, stack[i]);
}
}
int main()
{
int ch = 5; /* so we exit if no valid input is made in scanf() */
do {
printf("1.push\n"
"2.pop\n"
"3.peek\n"
"4.display\n"
"5.exit\n");
scanf("%d", &ch);
switch (ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
break;
default:
printf("Input invalid option\n");
break;
}
} while (ch != 5); /* option 5 is to exit */
return 0;
}

Related

Complete Stack implementation in C

I have been asked to do a stack implementation. I need to the following functions;
Push
Pop
isFull
isEmpty
peek
Display the whole array
This is what I wrote.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
/* Stack Structure */
struct stack
{
int s[SIZE];
int top;
}st;
int main()
{
int option;
printf("+-------------------------------------+\n");
printf("1.Push\n2.Pop\n3.Check whether the stack is full\n4.Check whether the stack is empty\n5.Check the Top Element\n6.Display the Stack\n7.Exit\n");
printf("+-------------------------------------+\n");
printf("Enter Choice:\t");
scanf("%d", &option);
while(option == -99)
{
switch(option)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
isFull();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
display();
break;
case 7:
printf("You Exited from the program");
break;
}
}
return 0;
}
/*Function to add an element to the stack*/
void push ()
{
int num;
if (st.top == (SIZE - 1))
{
printf ("Stack is Full\n");
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
st.top ++;
st.s[st.top] = num;
}
}
/*Function to delete an element to the stack*/
int pop()
{
int num;
if (st.top == -1)
{
printf ("Stack is Empty\n");
return st.top;
}
else
{
num = st.s[st.top];
printf ("Popped element is = %d", st.s[st.top]);
st.top --;
}
return (num);
}
/*Function to Check whether the stack is full*/
void isFull()
{
if(st.top == SIZE - 1)
printf("Stack is Full");
else
printf("Stack has %d elements", st.top - 1);
}
/*Function to Check whether the stack is Empty*/
void isEmpty()
{
if(st.top == -1)
printf("Stack is Empty");
else
printf("Stack has %d elements", st.top - 1);
}
/* Function to display the top element*/
void peek()
{
printf("Top most element: \t%d", st.s[st.top]);
}
/* Function to display the stack*/
void display ()
{
int i;
if (st.top == -1)
{
printf ("Stack is empty\n");
}
else
{
printf ("\n The status of the stack is \n");
for (i = st.top; i >= 0; i--)
{
printf ("%d\n", st.s[i]);
}
}
printf ("\n");
}
0 errors, 11 warnings are shown.
But when I run the programme it ends after asking the choice.
output:
+-------------------------------------+
1.Push
2.Pop
3.Check whether the stack is full
4.Check whether the stack is empty
5.Check the Top Element
6.Display the Stack
7.Exit
+-------------------------------------+
Enter Choice: 1
Process returned 0 (0x0) execution time : 6.304 s
Press any key to continue.
I really need to have it done. It is one of my assignments. Please help me and thank you very much for your time. :-)
You're exiting because you are entering an option that's between 1 and 7, but your while loop is checking for -99. So the while loop gets skipped and you exit.
I'm guessing what you actually want to do is keep prompting the user for actions until they exit. Try considering what functionality you actually want looped over in your program.
Also don't be afraid to put print statements in your code and trace the flow line by line. That will help you a lot with debugging.
Best of luck on the assignment!
Of course process stops working when you choose number. The program works while variable option is equal to -99, which never happens, because your choice is always between numbers 1-7.
The problem may be solved by writing option >= 1 && option <=7 in while loop.

Program keeps running even after the error appeared

I have a program that retrieves some numbers from a file into an array. The last problem that I have is that my program keeps running even though it cannot open the file, instead of ending it. Even when it says program is over, the menu from main appears again but doesn't show any data
int main (void)
{
int choice, max, min;
float avg;
int test[MAX_NUMBER_OF_STUDENTS];
int file_opened;
int number_of_students;
file_opened = get_test_scores(test, &number_of_students);
if (file_opened == 0)
{
do
{
choice = menu();
switch (choice)
{
case 0: printf("\nProgram is over.\n");
break;
case 1: test_avg(&avg, test, number_of_students);
printf("\nAverage score on test = %5.2f\n", avg);
break;
case 2: test_max_min(number_of_students, &max, &min, test);
printf("\nMaximum score = %3d\n"
"Minimum score = %3d\n", max, min);
break;
case 3: print_test(test,number_of_students);
break;
default:
printf("This should never happen!");
}
} while (choice != 0);
}
return 0;
}
int get_test_scores(int test[], int* size)
{
FILE* sp_input; // Pointer to the input stream (from a file)
int i;
sp_input = fopen("a20.dat", "r");
if (sp_input == NULL)
printf("\nUnable to open the file a20.dat\n");
else
{
while( fscanf(sp_input, "%d", &test[i])!=EOF)
{
i=i+1;
++*size;
}
fclose(sp_input); // Close the stream
}
return 1;
}
Yes, it keeps running...
if (file_opened == 0)
{
do
{
means that if the file has not been opened, to do the loop. You want:
if (file_opened != 0)
See also comments about other bugs.

Why is my static stack not working?

static stack implementation
this is also not deleting according to the lifo principle
static stack implementation:
it is not taking name for the second time
this is the new code now tell me why is it not working
please help
typedef struct student {
char name[20];
int roll;
int age;
} mystruct;
#define size 40
int top;
static mystruct s[size];
void push()
{
if (top == size - 1) {
printf("\noverflow"); //
} else {
printf("\nenter the name of the student");
gets(s[top].name);//not taking name for d 2 time
printf("\nenter the roll number");
scanf("%d", &s[top].roll);
printf("\nenter the age of the student");
scanf("%d", &s[top].age);
++top;
}
}
void pop()
{
if (top == -1)
{
printf("\nunderflow");
} else {
printf("%s", s[top].name);
printf("%d", s[top].roll);
printf("%d", s[top].age);
printf("\npopped");
--top;
}
}
void display()
{
int i;
if (top == -1) {
printf("\nstack is empty");
} else {
for (i = top; i > 0; i--) {
printf("\nthe name of the student is%s", s[top].name);
}
printf("\nthe roll no of the student is%d", s[top].roll);
printf("\nthe age of the student is%d", s[top].age);
}
}
main()
{
top = -1;
char ch;
while (1) {
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = getche();
if (ch == '0') {
break;
}
switch (ch) {
case '1':
push();
break;
case '2':
pop();
break;
case '3':
display();
break;
default:
printf("choice not valid");
break;
}
}
}
The first problem I noticed was that top is initialized to -1. Trying to access the member data of s[top] when top is initialized to -1 will result in unpredictable behavior.
I would suggest changing the line
top = -1;
to
top = 0;
That changes the basic assumption you have made in push, pop, and display about when the stack is empty and when it is full. Instead of checking if ( top == -1 ), you have to now check if (top == 0 ). Instead of checking if ( top == size - 1 ), you have to now check if ( top == size ).
In pop, you have to use top-1 instead of top.
The for loop in display is not scoped correctly. You need to use:
for (i = top-1; i >= 0; i--) {
printf("\nthe name of the student is %s", s[i].name);
printf("\nthe roll no of the student is %d", s[i].roll);
printf("\nthe age of the student is %d", s[i].age);
}
Also, reading the options for the menu and reading the subsequent input is little bit tricky.
After you read the menu option, you have to make sure that you eat up all the input until the next newline. Otherwise, gets() will read everything after your menu option until the end of the line. If you typed 1 for the menu and then typed Return/Enter, the name will be automatically accepted as "\n". Hence, I suggest the lines:
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = fgetc(stdin);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
Also, after you read the age of the object, you have to eat everything up to the newline. Otherwise, the newline character is read in as the choice for the next menu option.
scanf("%d", &s[top].age);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
Here's the fully working file. I have replaced gets by fgets and getche by fgetc.
#include <stdio.h>
#include <string.h>
typedef struct student {
char name[20];
int roll;
int age;
} mystruct;
#define size 40
int top;
static mystruct s[size];
void push()
{
if (top == size) {
printf("\noverflow"); //
} else {
printf("\nenter the name of the student: ");
fgets(s[top].name, 20, stdin);//not taking name for d 2 time
// The newline character is part of s[top].name when fgets is
// finished. Remove that.
s[top].name[strlen(s[top].name)-1] = '\0';
printf("\nenter the roll number: ");
scanf("%d", &s[top].roll);
printf("\nenter the age of the student: ");
scanf("%d", &s[top].age);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
++top;
}
}
void pop()
{
if (top == 0)
{
printf("\nunderflow");
} else {
printf("%s, ", s[top-1].name);
printf("%d, ", s[top-1].roll);
printf("%d", s[top-1].age);
printf("\npopped");
--top;
}
}
void display()
{
int i;
if (top == 0) {
printf("\nstack is empty");
} else {
for (i = top-1; i >= 0; i--) {
printf("\nthe name of the student is %s", s[i].name);
printf("\nthe roll no of the student is %d", s[i].roll);
printf("\nthe age of the student is %d", s[i].age);
}
}
}
main()
{
top = 0;
char ch;
while (1) {
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = fgetc(stdin);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
if (ch == '0') {
break;
}
switch (ch) {
case '1':
push();
break;
case '2':
pop();
break;
case '3':
display();
break;
default:
printf("choice, %c, not valid", ch);
break;
}
}
}
You need to change getche() to getchar()
Note: getche() is a non-standard function.
Maybe this will be useful http://www.delorie.com/djgpp/doc/libc/libc_385.html
Pay attention to implementation note:
"If you can detect the situation when one of the conio functions is called for the very first time since program start, you could work around this problem by calling the gppconio_init function manually"
or just replace it with getchar(). And there meaned conio included.

C program switch statement

I'm new to programming in C. I have a quick question about Switch Statements.
I have a menu that presents a list of options like so:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
struct Video {
char name[1024]; // Yvideo name
int ranking; // Number of viewer hits
char url[1024]; // YouTube URL
};
struct Video Collection[MAX];
int tail = 0;
//-- Forward Declaration --//
void printall();
void insertion();
void savequit();
void load();
void branching(char);
void menu();
int main()
{
char ch;
load(); // load save data from file
printf("\n\nWelcome\n");
do {
menu();
fflush(stdin); // Flush the standard input buffer
ch = tolower(getchar()); // read a char, convert to lower case
branching(ch);
} while (ch != 'q');
return 0;
}
void menu()
{
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("i: Insert a new favorite\n");
printf("p: Review your list\n");
printf("q: Save and quit\n");
printf("\n\nPlease enter a choice (i, p, or q) ---> ");
}
void branching(char option)
{
switch(option)
{
case 'i':
insertion();
break;
case 'p':
printall();
break;
case 'q':
savequit();
break;
default:
printf("\nError: Invalid Input. Please try again...");
break;
}
}
so far entering 'i' (for inserting a new entry) and q (for save and quit) work perfectly. However every time I enter 'p' I get the default case. (Error: Invalid Input. Please try again...). What is it that I am doing wrong? I believe the syntax for the switch is correct? I've tried changing the 'p' to a different letter and I still got the the default case. Here is my printall() method if that helps...
void printall()
{
int i;
printf("\nCollections: \n");
for(i = 0; i < tail; i++)
{
printf("\nName: %s", Collection[i].name);
printf("\nRanking (Hits): %d", Collection[i].ranking);
printf("\nURL: %s", Collection[i].url);
printf("\n");
}
}
What about something like:
char b[5];
do {
menu();
if(fgets(b,5,stdin)==NULL)
return -1;
ch = tolower(b[0]); // read a char, convert to lower case
while(strlen(b)>=4&&b[3]!='\n'){
check=fgets(b,5,stdin);
if(check==NULL)
return -1;
}
branching(ch);
} while (ch != 'q');
You can output the invalid char in your default case. That may help you understand how your input are handled.
default:
printf("\nError: Invalid Input ('%c'). Please try again...", option);
break;
fflush(stdin) is undefined as fflush is define only for output streams. To clear the newline char, you can simply use another getchar().
Try this for the loop part:
do {
menu();
ch = tolower((unsigned char)getchar());
getchar();
branching(ch);
} while (ch != 'q');

How to write a console menu in ANSI/ISO C?

I'm trying to create a simple program in c, where the user has to choose between several options:
char command = '1';
while(command!='0') {
printf("Menu:\n");
printf("1. First option\n");
printf("2. Second option\n");
printf("0. Exit\n");
printf("Choose: 0,1,2?: ");
command = getchar();
while(getchar()!='\n');
switch(command) {
case '0': break;
case '1': functionCall1(); break;
case '2': functionCall2(); break;
}
}
The problem with my code is, that every second time I enter 1,2 or 0, nothing happens, only the menu prints itself again. With the debugger I can see, that the value of command, after command = getchar() equals '', every second time. I thought that eating the newline character is enough?
Try my customized menu, I implemented it to make my life easier when I work with programs which contains multiple choices operations.
Menu is navigable (arrows:up, down, left, right), and for making a selection you only need to press enter key, menu orientation can be set vertically or horizontally, padding can be set to a group of items(childrens), child starting position, and update with delay.
Menu call example (vertically):
int response = menu("OPTIONS","[*]","->",
1,3,3,0,5,
"PROFILES","ACTIVITY","VIDEO","SOUND","GAMEPLAY");
The most important thing is because function implementation takes only 60 lines of code.
Menu implementation:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <windows.h>
// LXSoft
// mod: cui/menu_021
// stdarg.h -> used for variable list of arguments (va_list, va_start ...)
// windows.h -> used for Sleep function, for *nix use unistd.h
typedef unsigned short int usint_t;
// Menu function prototype
int menu(char* name, char* prefix, char* cursor, usint_t orientation,
usint_t padding, usint_t start_pos, usint_t delay,
usint_t num_childs, ...);
int main()
{
int response = menu("OPTIONS","[*]","->",1,3,3,0,5,
"PROFILES","ACTIVITY","VIDEO","SOUND","GAMEPLAY");
switch(response)
{
case 1:
// doSomethingFoo1();
break;
case 2:
//doSomethingFoo2();
break;
/*
* .
* .
* .
* case n:
* break;
*/
}
printf("\nYour choice is: %d", response);
return 0;
}
// Menu implementation
int menu
(
char *name, // Menu name (eg.: OPTIONS)
char *prefix, // Menu prefix (eg.: [*])
char *cursor, // Menu cursor (eg.: ->)
usint_t orient, /*
* Menu orientation vertical or horzontal.
* 0 or false for horizontal
* 1 or true for vertical
*/
usint_t padding, // Menu childrens padding (eg.: 3)
usint_t start_pos, // Menu set active child (eg.: 1)
usint_t delay, // Menu children switch delay
usint_t childs, // Number of childrens
... /*
* Variable list of arguments char* type.
* Name of the childrens.
*/
)
{
va_list args;
int tmp=0,pos;
char chr;
usint_t opt=start_pos;
char* format=malloc
(
(
strlen(name)+strlen(prefix)+strlen(cursor)+
3+ /* menu suffix (1 byte) and backspace (2 bytes) */
(2*childs)+ /* newline (2 bytes) times childs */
(padding*childs)+ /* number of spaces times childs */
childs*15 /* children name maxlen (15 bytes) times childs*/
)*sizeof(char)
);
do
{
if(tmp!=0)chr=getch();
if(chr==0x48||chr==0x4B)
(opt>1&&opt!=1)?opt--:(opt=childs);
else if(chr==0x50||chr==0x4D)
(opt>=1&&opt!=childs)?opt++:(opt=1);
else {/* do nothing at this time*/}
strcpy(format,"");
strcat(format,prefix);
strcat(format,name);
strcat(format,":");
va_start(args,childs);
for (tmp=1;tmp<=childs;tmp++)
{
(orient)?strcat(format,"\n"):0;
pos=padding;
while((pos--)>0) strcat(format," ");
if(tmp==opt)
{
strcat(format,"\b");
strcat(format,cursor);
}
strcat(format,va_arg(args,char*));
}
/*if(tmp!=childs)
{
fprintf(stderr,"%s: recieved NULL pointer argument,"
" child not named", __func__);
return -1;
}*/
Sleep(delay);
system("cls");
printf(format);
va_end(args);
}while((chr=getch())!=0x0D);
return opt;
}
May be you should try to use int x as a key to use a desirable command, may be like that:
while(x != 0)
{
scanf("%d", &x);
switch (x)
{
printf("input '2' to...\n");
printf("input '3' to...\n");
printf("input '4' to...\n");
printf("input '5' to...\n");
printf("input '6' to...\n");
case 1:
head = Enqueue(head);
break;
case 2:
head1 = spisokMagazinovScenoiMensheiZadannoi(head, head1);
break;
case 3:
head1 = udalenieElementa(head1);
break;
case 4:
head1 = addNewMagazin(head1);
break;
case 5:
head1 = addNewMagazin(head1);
break;
case 6:
printToTheFile(head);
break;
}
}
I used it in my previous homework. Hope it will be usefull for you
it's my exaple of func menu.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <windows.h>
int menu(char s[][80],int kolop){
int pos = 0,push = 0,i,flag = 0;
printf("MENU:\n>");
puts(s[0]);
for(i = 1;i < kolop;i++){
printf(" ");
puts(s[i]);
}
printf("Enter 5 to move the cursor down 8 to the top(Press ENTER to end)\n");
do{
if(flag == 1)
printf("Error try to press 5,8 or ENTER\n");
push = getch();
flag = 1;
}
while(push != 56 && push != 53 && push != 13);
flag = 0;
system("cls");
while(push != 13){
if(push == 53){
puts("MENU:");
for(i = 0;i < kolop;i++){
if(i == pos + 1 && flag == 0){
printf(">");
puts(s[i]);
flag = 1;
pos++;
}
else{
printf(" ");
puts(s[i]);
}
}
}
if(push == 56){
puts("MENU:");
for(i = 0;i < kolop;i++){
if(i == pos - 1 && flag == 0){
printf(">");
puts(s[i]);
flag = 1;
pos--;
}
else{
printf(" ");
puts(s[i]);
}
}
}
_flushall();
printf("Enter 5 to move the cursor down 8 to the top(Press ENTER to end)\n");
flag = 0;
do{
if(flag == 1)
printf("Error try to press 5,8 or ENTER\n");
push = getch();
flag = 1;
}
while(push != 56 && push != 53 && push != 13);
flag = 0;
system("cls");
}
pos++;
return pos;
}
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
char s[][80] = {"1","2","3","4","5","6","7"};
int i,pos = 0,push,flag = 0;
pos = menu(s,7);
printf("%d",pos);
//system("cls");
return 0;
}

Resources