problem in Object Oriented

This is a discussion on problem in Object Oriented within the C++ Programming forums, part of the General Programming Boards category; Do not be frustrated to read if the code looks too long but it is very simple program. I am ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    problem in Object Oriented

    Do not be frustrated to read if the code looks too long but it is very simple program. I am new in programing and I have done it without using the object oriented design , but for this one I have to use the object oriented design. So, I compiled the program and it has these following errors:

    Error E2040 workingArea2.cpp 6: Declaration terminated incorrectly
    Error E2040 workingArea2.cpp 17: Declaration terminated incorrectly
    Error E2275 workingArea2.cpp 29: { expected
    Error E2321 workingArea2.cpp 165: Declaration does not specify a tag or an identifier
    Error E2268 workingArea2.cpp 181: Call to undefined function 'newCode' in function main()
    *** 5 errors in Compile ***


    I am sure about the codes because I just copied them from the previous program that works the same. I am not sure how does the object oriented design look like so after reading a lot of topics about the object oriented design I came up with this style but I am not really sure about it. So, anyone has any hint or comment on my code that I should change ?


    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void SteptsCode()
    {
    
    		cout << "   1. The digits 4 and 5 must appear at least once.\n";
    		cout << "   2. Either the digit 8 and 9 must appear at least once.\n";
    		cout << "   3. The only valid letters are A,L,P,R,T,V,W, and X.\n";
    		cout << "   3. Each four-character subsquence (1-4, 5-8, 9-12) must contaon at least one digit and at least one letter.\n";
    		cout << "  \n"<<endl;
    };	
    
    // If all the conditions are valid, all the characters will be stored in an array from newCode(). Then, this method is going to print the code
    class printCode()
    {
    	int i=0;
    	string billy [12];
    
    	public:
    	for( i = 0; i< 13; i++)
    	{
    		cout<<billy[i]<<endl;
    	}
    };
    
    class void newCode()
    {
    	int tester;
    	string serial_number;
    	int length;
    	string mystr = serial_number;
    	int flag=0;
    	string mystrArray [12];
    	string billy [12];
    
    	public:
    	   cout<< "please enter a serial number of 12 characters:";
    		cin >> serial_number;
    		length = serial_number.length();
    		string mystr = serial_number;
    		
    		if (length!=12 )
    		cout << "There must be 12 characters exactly\n";
    		else for(int i=0;i<length;i++)
    			{
    				if (mystr[i]=='4')
    				{
    					flag=1;
    					break;
    				}
    			}
    			if(flag)
    			{
    				int flag1 =0;
    				for(int i=0;i<length;i++)
    				{
    					if (mystr[i]=='5')
    					{
    						flag1=1;
    						break;
    					}
    				}
    				if(flag1)
    				{
    					int flag2=0;
    					for(int i=0;i<length;i++)
    					{
    						if (mystr[i]=='8' || mystr[i]=='9')
    						{
    							flag2=1;
    							break;
    						}
    					}
    					if(flag2)
    					{
    						int flag3=0;
    						for(int i = 0; i< 4; i++)
    						{
    							if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    							{
    								flag3=1;
    								break;
    							}
    						}
    						if(flag3)
    						{
    							int flag4=0;
    							for(int i = 4; i< 8; i++)
    							{
    								if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    								{
    									flag4=1;
    									break;
    								}
    							}
    							if(flag4)
    							{
    								int flag5=0;
    								for(int i = 8; i< 12; i++)
    								{
    									if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    									{
    										flag5=1;
    										break;
    									}
    								}
    								if(flag5)
    								{
    									int flag6=0;
    									for(int i = 0; i< 12; i++)
    									{
    										if ((mystr[i] == 'Q') || (mystr[i] == 'E') || (mystr[i] == 'Y') || (mystr[i] == 'U') || (mystr[i] == 'I') || (mystr[i] == 'O')||(mystr[i] == 'S')||(mystr[i] == 'D')||(mystr[i] == 'F')||(mystr[i] == 'G')||(mystr[i] == 'H')||(mystr[i] == 'J')||(mystr[i] == 'K')||(mystr[i] == 'Z')||(mystr[i] == 'C')||(mystr[i] == 'B')||(mystr[i] == 'N')||(mystr[i] == 'M')||(mystr[i] == '~') || (mystr[i] == '@') || (mystr[i] == '#') || (mystr[i] == '$') || (mystr[i] == '&#37;') || (mystr[i] == '^')||(mystr[i] == '&')||(mystr[i] == '*')||(mystr[i] == '(')||(mystr[i] == ')')||(mystr[i] == '_')||(mystr[i] == '`')||(mystr[i] == '-')||(mystr[i] == '+')||(mystr[i] == '=')||(mystr[i] == 'q')||(mystr[i] == 'w')||(mystr[i] == 'e')||(mystr[i] == 'r') || (mystr[i] == 't') || (mystr[i] == 'y') || (mystr[i] == 'u') || (mystr[i] == 'i') || (mystr[i] == 'o')||(mystr[i] == 'p')||(mystr[i] == 'a')||(mystr[i] == 's')||(mystr[i] == 'd')||(mystr[i] == 'f')||(mystr[i] == 'g')||(mystr[i] == 'h')||(mystr[i] == 'j')||(mystr[i] == 'k')||(mystr[i] == 'l')||(mystr[i] == 'z')||(mystr[i] == 'x')||(mystr[i] == 'c')||(mystr[i] == 'v')||(mystr[i] == 'b')||(mystr[i] == 'n')||(mystr[i] == 'm')||(mystr[i] == '[')||(mystr[i] == ']')||(mystr[i] == '{')||(mystr[i] == '}')||(mystr[i] == ';')||(mystr[i] == '<')||(mystr[i] == '>')||(mystr[i] == ',')||(mystr[i] == '.')||(mystr[i] == '?')||(mystr[i] == '/'))
    										{
    											flag6=1;
    										}
    									}
    									if(flag6)
    									{
    										cout << "you have used not valid characters\n"<<endl;
    									}
    									else
    									{
    										cout << "This code is correct\n"<<endl;
    										cout << "Click on 2 to print the code\n"<<endl;
    										for ( int i=0;i<length;i++)
    										{
    											billy[i]=mystr[i];
    										}
    									}
    								}
    								else
    								{
    									cout << "The only valid letters are A,L,P,R,T,V,W, and X from 9 to 12 digits.\n"<<endl;
    								}
    									
    							}
    							else
    							{
    								cout << "The only valid letters are A,L,P,R,T,V,W, and X from 5 to 8 digits.\n"<<endl;
    							}
    						}
    						
    						
    						else 
    						{
    							cout << "The only valid letters are A,L,P,R,T,V,W, and X from 1 to 4 digits.\n"<<endl;
    						}
    					}
    					else
    					{
    						cout << "Either the digit 8 and 9 must appear at least once.\n"<<endl;
    					}
    				}
    				else
    					cout << "The digits 4 and 5 must appear at least once.\n"<<endl;
    			}
    			else
    			{
    				cout << "The digits 4 and 5 must appear at least once.\n"<<endl;
    			}
    		
    };
    
    int main()
    {
    	int tester;
    		while((tester > 0) || (tester < 5))
    		{
    			cout << "select one of these options\n";
     			cout << "(1) Insert a new code\n";
     			cout << "(2) Print the data\n";
    			cout << "(3) Program requirments\n";
    			cout << "(4) Exit the program\n";
    
    			cin >> tester;
    			
    			if(tester==1)
    				newCode();		
    			else if (tester==2)
    				printCode();
    		 	else if (tester==3)
    				SteptsCode();	
    			else if (tester==4)
    				break;
    			else
    				cout << "ERROR!!! You must choose one of the following options 1 to 4";
    		}
    return 0;
    }
    Last edited by aama100; 02-02-2008 at 06:53 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		cout << "   1. The digits 4 and 5 must appear at least once.\n";
    		cout << "   2. Either the digit 8 and 9 must appear at least once.\n";
    		cout << "   3. The only valid letters are A,L,P,R,T,V,W, and X.\n";
    		cout << "   3. Each four-character subsquence (1-4, 5-8, 9-12) must contaon at least one digit and at least one letter.\n";
    		cout << "  \n"<<endl;
    The above code is regular code-statements. They need to be inside some function, not directly in a class declaration. What you are doing is a bit like pooring cornflakes on the table without a bowl, if you see what I mean...

    I'm also quite sure that you can solve your "conditions" without nesting 7 levels of if-statements, if you think about it a bit more.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Sorry, but this is just too much code, next try to remove parts which dont add anything
    to the output of the error messages.
    I got also one improvement for you:
    remove the parantheses behind every class name. As a class is no function, it owns no
    parameter list (only the constructor of the class does, use google).

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    yeah I know my code has so many if statements.. I am thinking to change that using some methods, but for now I want to make sure that my program works correctly using the object oriented design then I will work on the actual code if there is any improvement..

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    you don't have to look at the actual code what is doing but just look on how the object oriented should look like. On the other hand, I am talking about the tamplete of the object oriented design for my program.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I cant move forward this way.. anyone can give me any hint about how to use the object oriented? .. I just need the starting point

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    You need to learn what classes are. And how to use them. What you're doing is now is just one big mess and won't work at all.
    Suggest you take a step back, learn classes properly, begin experimenting with them carefully and then go back writing your program. Make sure you learn what classes are and how you use them first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    If you'd like to understand what "Object Oriented" programming is, here's a little example for you:

    An "Object" is just a thing. It could be a boat, it could be a person, it could be a spaceship. Doesn't matter. "Things" have attributes and actions. We'll use a "battleship" in our example.

    What attributes does a battleship have? Size. Crew size. Types of weapons. etc.

    What actions can a battleship perform? Dock. Set sail. Fire torpedos. etc.

    To put this in code form:
    Code:
    class Battleship{
    
        public:
    
            // Constructor
            Battleship(int s, int c, string w) : size(s), crew(c), weapons(w) {};
    
            // Accessors (Give us access to the private data)
            int get_size(){ return size; };
            int get_crew(){ return crew; };
            string get_weapons(){ return weapons; };
    
            // Mutators (Allow us to modify private data)
            void set_size(int s){ size = s; };
            void set_crew(int c){ crew = c; };
            void set_weapons(string w){ weapons = w; };
    
            // Other Battleship functions might include:
            void dock();
            void set_sail();
            void fire_torpedos();
    
        private:
    
            // Here is the listing of your private data (the battleships attributes)
            int size;
            int crew;
            string weapons;
    };
    Object-oriented is not some mystical programming secret. It's simply a way for programmers to "abstract" things or problems in ways that make solving a particular problem easier to understand. C++ offers certain object-oriented "tools" if you will.

    These tools include: classes & structs, data-structures like lists, queues, stacks, etc. that are already defined in the STL, inheritance, polymorphism, etc.

    Hope this clarifies things for you. I know it takes a while to understand some of these things, especially after you've spent the majority of your learning studying C++ syntax, and control-structures and not focusing on OOP problem solving. Just hang in there, and keep coding, it will become second nature to you!

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    thanks dudeomanodude! now it makes better sense to me.

    I tried this code and works correctly:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    class codeClass {
    public:
    
    void code(int length) 
    {		
    	x=length;
    	if ( length != 4 )
    		cout << "NOT VALID"<<endl;
    	else
    		cout << "correct"<<endl;		
    
    }
    
    
    
    
    private:
    	int length;
    	int x;
    };
    
    
    int main()  {
    	string newvalue;
    
    	int length;
    
    	cout << "Insert a code of 4 characters:";
    	cin >> newvalue;
    	length = newvalue.length();	
     	codeClass first;
    
     	first.code(length);
      
    
    
    
      return 0;
    }


    but see this one I tried to follow the same steps but I don't understand my errors
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    class linkedListclass {
    public:
    
    void code(string newvalue) 
    {		
    	mystr = newvalue;
    	for( i=0;i<5;i++)
    	{
    		if (mystr[i]=='A')
    			cout << "correct"<<endl;
    		else
    			cout << "sorry";		
    	}
    }
    
    
    
    
    private:
      string x;
      string mystr;
      int i;
    };
    
    
    int main()  {
    	string newvalue;
    
    	int length;
     	linkedListclass first;
    
    	cout << "Insert a code of 4 characters:";
    	cin >> newvalue;
    	length = newvalue.length();	
    	
    	if ( length =! 4 )
    		cout << "NOT VALID"<<endl;
    	else
     		first.code(newvalue);
      
    
    
    
      return 0;
    }

  10. #10
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I'm not quite sure I understand what your program is supposed to do. What is the purpose of this program?

    In your private data, int i and string x don't seem to have a purpose, they can go.

    Your statement:
    Code:
    if ( length =! 4 )
    won't work, it needs to be:
    Code:
    if(length != 4)
    declaring an "int length" in main is uneccessary as you can rewrite your if statement like this:
    Code:
    if(newvalue.length() != 4)
    also the for loop in code(string newvalue) loops one too many times. If you're trying to process the 4 chars of the string your loop only needs to run 4 times:
    Code:
    for(int i = 0; i < 4; i++)
    because "i" in this case is initialized at 0. Which means the loop will execute at:
    0, 1, 2, and 3 (which is 4 times through)

    Other than that, like I said, I don't know the purpose of your program, so I can't help you beyond which you've got here.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    You are forcing the use of classes where it absolutely is not necessary. You are just using a class "because." A class is an object, not some code base or function for you to use as you will.
    You should learn it appropriately. Then you should learn how to split definition and implentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  2. Binary Search Tree - object instantiation problem
    By patricio2626 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2006, 02:11 AM
  3. object oriented programming
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2006, 10:18 AM
  4. Object Oriented Cube
    By Ti22 in forum Game Programming
    Replies: 3
    Last Post: 01-11-2005, 08:18 PM
  5. C++ and Object Oriented Programming
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2002, 06:27 AM

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