I need help

This is a discussion on I need help within the C++ Programming forums, part of the General Programming Boards category; Im trying to do this project for my class. I have my code but I know its not right. So ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    I need help

    Im trying to do this project for my class. I have my code but I know its not right. So I was hoping someone could help me out or at least point me in the right direction. I have to write a program in C++ that checks various lines of text (taken as arithmetical expressions) to see if the following 3 pairs of grouping symbols: '(', ')', '[', ']', '{', '}' are being used properly. I have to use a Stack with the array implementation using a class. Then I have to test my program on the following data:
    a+b
    (a-b)
    ((c-d)
    [(a-b)+(c+d)]
    (e+f]
    (f+g))
    [f-g}
    {c+d)
    {a*[(c-d)*(e+f)+(g-h)/(f+g)]+h}*b+c
    (((((a)))))

    Im a beginner and I have read the book but it doesn't help that much. Im using Visual Studio C++ 2010 and here is my code so far:



    Code:
    #include <list>
    using namespace
    
    int main();
    {
    
    template <class Elem> class Astack: public Stack<Elem> {
    private:
    	int top;
    	int size;
    	Elem *listArray;
    	public:
    		Astack(int sz =DefaultListSize)
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){ 
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		};

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,805
    I would start by getting the first three lines working.

    Code:
    #include <list>
    using namespace
    
    int main();
    {
    Im trying to do this project for my class. I have my code but I know its not right. So I was hoping someone could help me out or at least point me in the right direction. I have to write a program in C++ that checks various lines of text (taken as arithmetical expressions) to see if the following 3 pairs of grouping symbols: '(', ')', '[', ']', '{', '}' are being used properly. I have to use a Stack with the array implementation using a class. Then I have to test my program on the following data:
    Why are you including the "list" header?

    Code:
    #include <list>
    Nowhere in you description does it mention using a list.

    I'll leave it to you to discover what is wrong with the next two lines.

    I would start over on you class definition. Start by just declaring the class with no members, compile, add a member to the class and compile, add yet more, compile again, continue to completion.

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Oh should it be #include<iostream> ?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,805
    These are the three lines I was asking about.

    Code:
    using namespace
    
    int main();
    {
    What namespace are you using??

    Why do you have a semicolon on the end of int main() ???

    Jim

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok I changed it....

    #include<iostream>
    using namespace std;

    int main(){

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,805
    And where is your end of main()?

    Note the class should not be defined inside of main().

    Jim

  7. #7
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Now I have changed it to this. Do I need the #include<stack> since I will be using the stack in this program
    Code:
    #include <iostream>
    #include<stack>
    using namespace std;
    
    template <class Elem> class Astack: public Stack<Elem>
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem *listArray;	/*Array holding stack elements*/	
    int main(){
    
    	public:
    		Astack(int sz =DefaultListSize)
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;}

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok this is what I got so far. Is there anything you notice that I have incorrect?
    Code:
    #include <iostream>
    #include<stack>
    using namespace std;
    
    class Astack {
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	char Elem *listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    int main(){
    
    	
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,805
    ok this is what I got so far. Is there anything you notice that I have incorrect?
    Yes....

    Code:
    int main(){
    
    	
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    }
    In this code you are trying to define functions inside main(). For now just put main at the bottom of your file.

    Like:

    Code:
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    };             ///////////// Notice the semicolon ///////////////////////
    
    
    int main()
    {
       return(0);
    }
    Now does the code compile?

    No? What are the error messages? Please post the entire error messages, along with the modified code.

    Yes? Great. Now try to use the class.

    Jim

  10. #10
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Code:
    #include <iostream>
    #include<stack>
    using namespace std;
    
    class Astack {
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	char Elem *listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    int main(){
    	return 0;
    }
    1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
    1> Project1.cpp
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(8): error C2143: syntax error : missing ';' before '*'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(14): error C2143: syntax error : missing ',' before '&'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(20): error C2061: syntax error : identifier 'Elem'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(26): error C2061: syntax error : identifier 'Elem'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(39): fatal error C1075: end of file found before the left brace '{' at 'c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(5)' was matched
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,805
    Code:
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    };             ///////////// Notice the semicolon ///////////////////////
    What happened to this last line?

  12. #12
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I dont understand the errors after I try to build my solution. Here's my new code and the errors are at the bottom:

    Code:
    #include <iostream>
    #include<stack>
    using namespace std;
    
    class Astack {
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	char Elem listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    int main(){
    	int a;
    	cout << "Enter data: ";
    	cin >> a;
    return 0;
    }
    1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
    1> Project1.cpp
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(8): error C2146: syntax error : missing ';' before identifier 'listArray'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(14): error C2143: syntax error : missing ',' before '&'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(20): error C2061: syntax error : identifier 'Elem'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(26): error C2061: syntax error : identifier 'Elem'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(10): error C2065: 'listArray' : undeclared identifier
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(10): error C2061: syntax error : identifier 'Elem'
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(12): error C2065: 'listArray' : undeclared identifier
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(12): error C2541: 'delete' : cannot delete objects that are not pointers
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(16): error C2065: 'listArray' : undeclared identifier
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(16): error C2065: 'item' : undeclared identifier
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(22): error C2065: 'item' : undeclared identifier
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(22): error C2065: 'listArray' : undeclared identifier
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(28): error C2065: 'item' : undeclared identifier
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(28): error C2065: 'listArray' : undeclared identifier
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by jturner38; 11-01-2010 at 06:19 PM.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,805
    Your first error:
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(8): error C2146: syntax error : missing ';' before identifier 'listArray'
    This error is on line 8 in project1.cpp(8). Look at line 8.

    Code:
       char Elem listArray;	/*Array holding stack elements*/
    The compiler does not know what a listArray is. It has not been defined anywhere. You must first create a class listArray before you can use it.

    All of your errors are because of this first line............. Fix that and most of your errors will go away.


    You might want to take pencil, paper and your textbook and design the classes, and general program flow.

    Jim

  14. #14
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok it took me a few days but I finally got rid of all the errors. Now I got part of this project done. Now I just need to figure out how to do my main program in order to test it.
    Here is my edited version now so far:
    Code:
    #include <iostream>
    using namespace std;
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    int main(){
    return 0;
    }

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,805
    Great, you have a stack class that compiles.

    So what kind of questions/problems are you having?

    Jim

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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