problems on stack.h

This is a discussion on problems on stack.h within the C Programming forums, part of the General Programming Boards category; i have a code that causes errors in running the program...i use my header file stack.h on my program but ...

  1. #1
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy problems on stack.h

    i have a code that causes errors in running the program...i use my header file stack.h on my program but it didn't work...what is the problem of that program?

    Code:
    #define DEFAULT_SIZE 10
    
    class stack//declaration syntax error
    {
    private:
    	int size;
    	int top;
    	int *values;
    public:
    	stack(int size=DEFAULT_SIZE);
    	/*virtual*/~stack();
    	int isFull();
    	int isEmpty();
    	void push(int);
    	int pop(int);
    }
    
    stack::stack(int size)
    {
    this->size=size;
    values=new int [size];
    top=-1;
    }
    stack::~stack()
    {
    delete [] values;
    }
    int stack::isFull()
    {
    if(top<size-1)
    	{
    	return 0;
    	}
    else
    	{
    	return 1;
    	}
    }
    int stack:: isEmpty()
    {
    if(top==-1)
    	{
    	return 1;
    	}
    else
    	{
    	return 0;
    	}
    }
    void stack:: push(int x)
    {
    if(!isFull())
    	{
    	top++;
    	values[top]=x;
    	}
    }
    int stack::pop(int top)
    {
    	int retVal=0;
    		if(! isEmpty())
    		{
    		retVal=values[top];
    		top--;
    		}
    return retVal;
    }
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    class as well as new/delete are C++ keywords

    So this header cannot be used in the C program

    I suppose - even usage of it in the C++ program will cause problems (which are fixable)... But for the C-language this file is totally unusable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Maybe it's the lack of indentation.

    Have you tried it with a really simple main(), which say creates a stack, pushes 1 value, pops it off the stack and then crashes?

    Posting a small complete example which crashes in a specific way is good.

    Posting just a small part of some otherwise massive program with some vague "it doesn't work" is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    class stack//declaration syntax error
    {
    private:
        int size;
        int top;
        int *values;
    public:
        stack(int size=DEFAULT_SIZE);
        /*virtual*/~stack();
        int isFull();
        int isEmpty();
        void push(int);
        int pop(int);
    };
    Missing semicolon?

    But yeah... can a mod move this?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy files invlove character stacking

    if the stack is not used, i tried to use a stack to read the content of the file and displays the content in reversed

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include "stack.h"
    void main()
    {
    clrscr();
    char str[1500];
    char file[10];
    char sfile[10];
    int x;
    FILE *fp1, *fp2;
    printf("Enter filename:");
    gets(file);
    fp1=fopen(file,"r");
    printf("Save to:");
    gets(sfile);
    fp2=fopen(sfile,"w");
    
    if(fgetc(str, sizeof(str), fp1)!=NULL)
               {
                          fputs(str,fp2);
                          printf("Content is:\n &#37;s", str);
               }
    else
               {
               printf("cannot open file!!!");
               }
    
    int y=strlen(str);
    
    for(i=0;i<y;i++)
        {
    	stach->push(str[i]);
        }
    printf("The reversed content is:");
    for(int i=0;i<y;i++)
        {
    	cout<<stack->pop(i);
        }
    fclose(fp1);
    fclose(fp2);
    getch();
    }
    by using stack.h in a turbo c library...the improper typedef happens on push and pop stack...what are things do you suggests in this code?
    Last edited by blacksnake; 06-20-2007 at 06:59 AM.
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    1. int main() faq
    2. clrscr is not portable
    3. don't use gets. use fgets faq
    4. I think you want fgets, not fgetc
    5. stach->push(str[i]);
    6. what is up with your indenting?
    7. did you even try to compile this?

  7. #7
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy

    Quote Originally Posted by robwhit View Post
    1. int main() faq
    2. clrscr is not portable
    3. don't use gets. use fgets faq
    4. I think you want fgets, not fgetc
    5. stach->push(str[i]);
    6. what is up with your indenting?
    7. did you even try to compile this?
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include "stack.h"
    int main()
    {
    
    char str[1500];
    char file[10];
    char sfile[10];
    int x;
    FILE *fp1, *fp2;
    printf("Enter filename:");
    fgets(file);
    fp1=fopen(file,"r");
    printf("Save to:");
    fgets(sfile);
    fp2=fopen(sfile,"w");
    
    if(fgetc(str, sizeof(str), fp1)!=NULL)
               {
                          fputs(str,fp2);
                          printf("Content is:\n &#37;s", str);
               }
    else
               {
                          printf("cannot open file!!!");
               }
    
    int y=strlen(str);
    
    for(i=0;i<y;i++)
              {
    	              stack->push(str[i]);//improper typedef
              }
    printf("The reversed content is:");
    for(int i=0;i<y;i++)
              {
    	              cout<<stack->pop(i);//improper typedef
              }
    fclose(fp1);
    fclose(fp2);
    getch();
    
    return 0;
    }
    6. there is nothing wrong with the identing
    7. i compile this program, but the errors are in the improper typedef
    Last edited by blacksnake; 06-20-2007 at 07:51 AM.
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Compiling as C++ I assume, So why'd you post in the C forum?
    Pay close attention to point #6...

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    /* #iclude <conio.h> */
    #include <string.h>
    #include "stack.h"
    
    /* void main()*/ 
    int main()
    {
         /* clrscr(); */
         
         char str[1500];
         char file[10];
         char sfile[10];
         int x;
         
         FILE *fp1, *fp2;
         printf("Enter filename:");
         /* gets(file); */
         fgets(file, sizeof(file), stdin);
         
         fp1=fopen(file,"r");
         printf("Save to:");
         /* gets(sfile); */
         fgets(file, sizeof(sfile), stdin);
         
         fp2=fopen(sfile,"w");
    
         if(fgetc(str, sizeof(str), fp1)!=NULL)
         {
              /* You just a single char here, whats up to the rest of them in the file */
              fputs(str,fp2);
              printf("Content is:\n &#37;s", str);
         }
         else
              printf("cannot open file!!!");
    
         int y=strlen(str);
         
         for(i=0;i<y;i++)
            stach->push(str[i]);
    
         printf("The reversed content is:");
         
         for(int i=0;i<y;i++)
            /* cout<<stack->pop(i); // This is a C++ standard dont use this in C */
            printf("%c",stack->pop(i));
            
         fclose(fp1);
         fclose(fp2);
         
         /* getch(); */
         getchar();     
         
         return 0;
    }
    ssharish2005

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by blacksnake View Post
    Code:
    fgets(file); // not how you use fgets. (<--link) also check out the links in my previous post.
    
    fgets(sfile); // ditto
    fp2=fopen(sfile,"w");
    
    if(fgetc(str, sizeof(str), fp1)!=NULL) // and still
    6. there is nothing wrong with the indenting
    not technically, but I still find it it a bit... unusual. Why do you space the braces so far except for the main() function? Why don't you indent code one level in inside the main() function?
    7. i compile this program, but the errors are in the improper typedef
    that's because of something in your header file. There are no typedef's in the code you just posted.

  11. #11
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy

    Quote Originally Posted by ssharish2005 View Post
    Code:
    #include <stdio.h>
    /* #iclude <conio.h> */
    #include <string.h>
    #include "stack.h"
    
    /* void main()*/ 
    int main()
    {
         /* clrscr(); */
         
         char str[1500];
         char file[10];
         char sfile[10];
         int x;
         
         FILE *fp1, *fp2;
         printf("Enter filename:");
         /* gets(file); */
         fgets(file, sizeof(file), stdin);
         
         fp1=fopen(file,"r");
         printf("Save to:");
         /* gets(sfile); */
         fgets(file, sizeof(sfile), stdin);
         
         fp2=fopen(sfile,"w");
    
         if(fgetc(str, sizeof(str), fp1)!=NULL)
         {
              /* You just a single char here, whats up to the rest of them in the file */
              fputs(str,fp2);
              printf("Content is:\n %s", str);
         }
         else
              printf("cannot open file!!!");
    
         int y=strlen(str);
         
         for(i=0;i<y;i++)
            stach->push(str[i]);
    
         printf("The reversed content is:");
         
         for(int i=0;i<y;i++)
            /* cout<<stack->pop(i); // This is a C++ standard dont use this in C */
            printf("%c",stack->pop(i));
            
         fclose(fp1);
         fclose(fp2);
         
         /* getch(); */
         getchar();     
         
         return 0;
    }
    ssharish2005
    added to stack.h:
    Code:
    #define dsize 10000
    
    class stack{
    public:
    	stack(int size=dsize);
    	/*virtual*/~stack(void);
    	int isFull();
    	int isEmpty();
    	void push(int);
    	int pop(int);
    private:
    	char size;
    	char top;
    	char *values;
    };
    
    stack::stack(int size)
    {
    this**size=size;
    values=new char [size];
    top=-1;
    }
    stack::~stack()
    {
    delete [] values;
    }
    int stack::isFull()
    {
    if(top<size-1)
    	{
    	return 0;
    	}
    else
    	{
    	return 1;
    	}
    }
    int stack:: isEmpty()
    {
    if(top==-1)
    	{
    	return 1;
    	}
    else
    	{
    	return 0;
    	}
    }
    void stack:: push(int x)
    {
    if(!isFull())
    	{
    	top++;
    	values[top]=x;
    	}
    }
    int stack::pop(int top)
    {
    	int retVal=0;
    		if(! isEmpty())
    		{
    		retVal=values[top];
    		top--;
    		}
    return retVal;
    }
    too many errors found when i compile it...is there a problem in a program or a .h file? why?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21