Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error 'is_empty' was not declared in this scope.why does it give error?

output=[Error] ‘is_empty’ was not declared in this scope
must be:
Example
Input string : abbccbaabccbba message will be The string is valid
aaabbcb
bcbaab message will be The string is invalid
aadbxcy*ycxbdaa message will be Wrong character!!!
what should i do?

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

typedef struct
{
 char home[35];
 int top;
} My_stack;

void push(My_stack * s, char c) // push (insert) operation
{ // assume there is enough space for pushing next element!
    s -> top ++;
    s -> home[s -> top] = c;
}

int pop(My_stack * s) // pop (remove) operation
{
    if(is_empty (*s)) {
        printf("ERROR: Nothing to pop - program terminates\n");
        exit(1);
    }
    return (s ->home[s ->top --]);
}

int is_empty(My_stack * s) // checking whether stack is empty or not
{
    return(s -> top < 0 ? 1 : 0);
}
    
int main(){
    char ch[25];
    int i,l;
    My_stack stack;
    printf("give the string");
    scanf("%s",ch);
    l=strlen(ch);
    i=0;    
    while(ch[i]!='\0') {
        if(ch[i]!='A'&&ch[i]!='B'&& ch[i]!='*') {
            printf("the string is not accepted allowed caracters are A,B and * ");
            exit(0);
        }
        i++;
    }   
    i=0;    
    while(ch[i] != '*') {
        push(&stack, ch[i]);
        i++;
    }
    i++; // one step forward to pass '*
    while(ch[i] != '\0') {
        if(ch[i] != pop(&stack)) {
            printf("the string is not valid");
            exit(0);
        }
        i++;
    }
    printf("the string is valid");          
    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The reason is because you define your function is_empty after your first use. To prevent this error, you should declare your function first. This tells the compiler "hey, there exists this function with this signature, the definition is given later though" so then you can use it in your program before having implemented it explicitly.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

typedef struct
{
 char home[35];
 int top;
} My_stack;

int is_empty(My_stack * s); // <----- DECLARATION OF FUNCTION

void push(My_stack * s, char c) // push (insert) operation
{ // assume there is enough space for pushing next element!
    s -> top ++;
    s -> home[s -> top] = c;
}

int pop(My_stack * s) // pop (remove) operation
{
    if(is_empty (s)) {
        printf("ERROR: Nothing to pop - program terminates\n");
        exit(1);
    }
    return (s ->home[s ->top --]);
}

int is_empty(My_stack * s) // checking whether stack is empty or not
{
    return(s -> top < 0 ? 1 : 0);
}

Or, you put the entire definition before your first use so then there’s no need for a declaration:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

typedef struct
{
 char home[35];
 int top;
} My_stack;

int is_empty(My_stack * s) // checking whether stack is empty or not
{
    return(s -> top < 0 ? 1 : 0);
}

void push(My_stack * s, char c) // push (insert) operation
{ // assume there is enough space for pushing next element!
    s -> top ++;
    s -> home[s -> top] = c;
}

int pop(My_stack * s) // pop (remove) operation
{
    if(is_empty (s)) {
        printf("ERROR: Nothing to pop - program terminates\n");
        exit(1);
    }
    return (s ->home[s ->top --]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading