Thread: Help again pls

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    Help again pls

    I m once again having problems. I need to get a sentence into an array that I can work with I need to check it for being a palindrome. I ve got it all set up and debugged(or did before playing with it) but cant find the right way to get this sentence put into the stack and que like I need it. Here is the code pls help if you can tks in advance for anything you can give.
    Code:
    #include <iostream>
    const int maxsize = 50;
    
    class stackchar
    {
    public: stackchar();
    	void enqueue(char ch);
    	char front();
    	void dequeue();
    	bool isfull();
    	bool isempty();
    	bool isempty2();
    	bool isfull2();
    	void push(char newchar);
    	void pop();
    	char topelement();
    
    private:
    	int frontelement;
    	int rear;
    	char s1[maxsize];
    	char s2[maxsize];
    	char s3[maxsize];
    	int top;
    	int vecsize;
    	int top2;
    };
    	
    -------------------------------------------------------------------------
    #include "s2p1.h"
    #include <iostream>
    int vecsize = maxsize;
    stackchar::stackchar()
    {
    	rear = frontelement = 0;
    }
    bool stackchar::isempty()
    {
    	return(rear == frontelement);
    }
    bool stackchar::isfull()
    {
    	return(rear+1%vecsize==frontelement);
    }
    void stackchar::enqueue(char ch)
    {
    	rear =(ch)%vecsize;
    }
    char stackchar::front()
    {
    	return s1[frontelement+1%vecsize];
    }
    void stackchar::dequeue()
    {
    	frontelement=(frontelement+1)%vecsize;
    }
    bool stackchar::isfull2()
    {
    	return(top==maxsize);
    }
    bool stackchar::isempty2()
    {
    	return(top==-1);
    }
    void stackchar::push(char newchar)
    {
    	s2[++top]=newchar;
    }
    char stackchar::topelement()
    {
    	return s2[top];
    }
    void stackchar::pop()
    {
    	top--;
    }
    }
    -------------------------------------------------------------
    #include "s2p1.cpp"
    #include<iostream>
    #include<fstream.h>
    #include<string>
    #include <istream>
    int main()
    {
    	stackchar s1;
    	stackchar s2;
    	stackchar s3;
    	int frontelement;
    	int rear;
    	char ch[50];
    	int top2;
    	
    	cout<<"Please enter a sentence to be tested $ to exit: ";
    	cin.getline(ch, 50);
    	while (!s2.isfull())
    	{
    		s2.push(ch);
    		s1.enqueue(ch);
    		
    	}
    	while (!s2.isempty2() && s2.topelement() == s1.front())
    	{
    			s2.pop();
    			s1.dequeue();
    	}
    	if (!s2.isempty2())
    	{
    		cout<<"Sentence is not a palindrome";
    	}
    	else
    	{
    		cout<<"Sentence is a palindrome when applying stack and queue";
    	}
    		return 0;
    }
    I haven't worked much with char. and that is where my problem lies I think.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    1 - Please dont post threads with titles like this one. Choose something that is meaningful. Remember, most people need help, that's why they post

    2 - When you post a message, the foum software sometimes changes characters to smilies, like it has in the middle of your code. You can turn this feature off by ticking "Disable Smilies in This Post" before submitting the post.

    3 - The real help... This:
    >>void stackchar :: push (char newchar)
    takes a single char as a parameter, whereas this:
    >>s2.push(ch);
    is passing an array (a pointer to a list of chars).

    I suggest you research the char array concept before going any further.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    sorry I will try to be more helpfull in my message title. I looked around a little and hope I learned a bit I changed the code to look like this. will this take a sentence and store it into an array called s1 for me? it compiles fine but crashed when run.
    Code:
    cout<<"Please enter a sentence to be tested $ to exit: ";
    	while( (c=getchar( )) != '\n' )
    	 { 	
    		if( n < 100 ) 
    		ch[n] = c;
    		 n++; 
    	}
    	while (!s2.isfull())
    	{
    		s1.enqueue(ch[n]);
    		s2.push(ch[n]);
    		n--;
    	}
    the rest of the program is the same.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lookup cin.getline(). That will read a complete line of data from the user, without the need for the loop.

    I haven't looked at the rest of your code...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the line:

    cin.getline(ch, 50);

    wasn't the real problem, although it might be safer to use:

    cin.getline(ch, 49);

    but that's another issue. The problem was use of the array ch as a single char when passed to s2.push() or s1.enqueue(). Your revised code went further that it needed to in it's revisions. If you to retain cin.getline() as in your first version then you could do something like this instead to keep lines of code to a minimum:


    for(i = 0; i < strlen(ch); ++i)
    {
    s2.push(ch[i]);
    s1.enqueue(ch[i]);
    }

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    Well I cant tell if either is working both ways compile but both also crash when run. The getline gives a warning of compairing signed and unsigned values in the for loop. The crashing is now the real problem I cant do much else till I get that fixed.
    Tks everyone for their help so far.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do you really have to do this with a stack?
    You know there is a much easier way to check palindromes in about 3 lines with pointers?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Yeah, Stoned_Coder is right. It can be done much easier without the stack- but it is a good exercise using the stack ADT.

    Mr. c.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to find out where it is crashing add statements to display results and hold screen so you can see results as you work your way through each loop, or use your debugger if you have one.

    Code:
    cin.getline(ch, 50);
    cout << ch;//check input obtained appropriately
    char temp;
    cin >> temp;
    
    while (!s2.isfull())
    {
       for(i = 0; i < strlen(ch); ++i)
       {
          cout << ch[i];
          s2.push(ch[i]);
          cout << s2.topelement();//check on push() and topelement()
          s1.enqueue(ch[i]);
          cout << s1.topelement();/check on enfqueue and topelement();
          cin >> temp;
       }
    }
    If you do that I think you'll find a several problems. First, you never initialize top in the class. therefore when you try to increment top in the push() function, it is junk and crashes your program.

    Second, what do think happens when you do try to use modulo on a char variable as in enqueue()? Check it out by putting a

    cout << rear;
    char temp2;
    cin >> temp2;
    in the enqueue function.

    Third, check each of the loops and each of the class methods by similar technique to look for other problems.

    Fourth, I would encourage you not to use the same variable name for different variables in your program. It is a recipe for disaster. In particular you use ch for both a string in main() and as a single char in a number of class methods and you use s1, s2, and s3 as array names/data members in the class and as instances of stackchar in main(). Don't do it!!

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    Well guys I m working on totally redoing this code. I dont have a lot of choice in this matter it is an assignment and the teacher didn't give us much room to chose our own variables etc. many of the variable names I cant even seem to get to work because they are the same as the function names. I m redoing it a bit hope to have something not so worthless written soon. Man I m not starting this week right.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    17
    PS I really do appreciate the help this assignment really has me messed up and it's due tomarrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  2. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 03:24 PM
  3. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM