Queue Stack Palindrome

This is a discussion on Queue Stack Palindrome within the C++ Programming forums, part of the General Programming Boards category; I got it Thanks everyone...

  1. #16
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    I got it
    Thanks everyone

  2. #17
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Question

    Hi , I had a question (forgive me I'm new). I was able to get my code working properly thanks to all of your help but had a question regarding your code (RobC). How is it that you did not have to write the pop, front, empty functions in your code. As a beginner I've always had to write the entire stack in a header file and include this file in the main. In your code you didn't have to and I was wondering how was that accomplished. My guess is that the functions I've been taught (pop, top, push) are all included in a library but which one and why wouldn't they teach us that in class

    Thanks

  3. #18
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >In your code you didn't have to and I was wondering how was that accomplished.
    The standard C++ library contains a bunch of useful functions and classes. Among them are templates for a queue and a stack. I didn't have to write them because they were already written and waiting for me to use.

    >but which one
    Notice that I included <queue> and <stack>. Those are the headers that hold the declarations.

    >and why wouldn't they teach us that in class
    Most classes will try to teach you the hard way first, then leave you to the wolves so that you can learn the easy way, the hard way. If you haven't guessed, I don't subscribe to that method. I think you should learn how to use the language productively first, then you can delve into details as necessary.

  4. #19
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Quote Originally Posted by Robc
    Code:
      if (!in) {
        cerr<<"Unable to open input file"<<endl;
        return EXIT_FAILURE;
      }
      if (!out) {
        cerr<<"Unable to open output file"<<endl;
        return EXIT_FAILURE;
      }
    I've seen this a lot, but shouldn't it be
    Code:
    	if (!in)
    	{
    		cerr << "Unable to open input file" << endl;
    		if (out)
    			out.close();
    		return EXIT_FAILURE;
    	}
    	if (!out)
    	{
    		cerr << "Unable to open output file" << endl;
    		in.close();
        		return EXIT_FAILURE;
    	}

  5. #20
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >but shouldn't it be
    There's no need. Since the destructor will take care of closing the file, you would only be adding redundant clutter to the code. If you were using FILE pointers, however, things would be different and a little more care would be in order:
    Code:
    FILE *in, *out;
    
    in = fopen("input.txt", "r");
    if (!in) {
      cerr<<"Error opening input file"<<endl;
      return EXIT_FAILURE;
    }
    out = fopen("input.txt", "w");
    if (!out) {
      cerr<<"Error opening output file"<<endl;
      fclose(in);
      return EXIT_FAILURE;
    }

  6. #21
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Right, thanks.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Queue and Stack: Comparing help!!! (palindrome)
    By advocation in forum C++ Programming
    Replies: 8
    Last Post: 03-09-2005, 07:46 PM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM

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