Thread: Tips for improvement

  1. #16
    Registered User
    Join Date
    Apr 2013
    Posts
    21
    The final "correct" code:
    Code:
    #include <cstdio>#include <cstdlib>
    #include <iostream>
    #include <vector>
    
    
    
    
    using namespace std;
    
    
    
    
    char flagcheck ( char );
    
    
    
    
    char flagcheck( char flag )
    {
    	while (( flag != 'Y' ) && ( flag != 'y' ) && ( flag != 'N' ) && ( flag != 'n' ))
    	{
    		cout<<"Wrong input! Try again!" ;
    		cin>>flag;
    	}
    
    
    	return flag;
    }
    
    
    
    
    int main()
    {
    	vector<char> state;
    	vector<char> symbol;
    	vector<int> finstateflag;
    
    
    	char a, flag, c;
    	int i, j, k, statenos, symnos;
    
    
    
    
    	cout<<"Enter the initial state: ";
    	cin>>a;
    	state.push_back (a);
    	finstateflag.push_back (0);
    
    
    	
    	for  ( i = 2; a != '0'; i++ )
    	{
    		cout<<"Enter state "<<i<<". Press 0 to end: ";
    		cin>>a;
    		if ( a == '0')
    		{
    			break;
    		}
    		state.push_back (a);
    
    
    		cout<<"Press Y if the state is a final state, else press N: ";
    		cin>>flag;
    		
    		flag = flagcheck( flag );
    		
    		switch(flag)
    		{
    		case 'Y':
    			finstateflag.push_back (1);
    			break;
    		
    		case 'y':
    			finstateflag.push_back (1);
    			break;
    		
    		case 'N':
    			finstateflag.push_back (0);
    			break;
    		
    		case 'n':
    			finstateflag.push_back (0);
    			break;
    
    
    		default:
    			cout<<"Wrong input! Aborted!";
    			exit(0);
    		}
    	}
    
    
    	cout<<"Enter symbol 1: ";
    	cin>>a;
    	symbol.push_back (a);
    	
    	for  ( i = 2; a != '0'; i++ )
    	{
    		cout<<"Enter symbol "<<i<<". Press 0 to end: ";
    		cin>>a;
    		if ( a == '0')
    		{
    			break;
    		}
    		symbol.push_back (a);
    	}
    
    
    	statenos = state.size();
    	symnos = symbol.size();
    
    
    	vector < vector < vector < char > > > NextState ( state.size(), vector < vector < char > > ( symbol.size(), vector < char > ( state.size(), 'A' )));
    	vector < vector < int > > counter ( state.size(), vector < int > ( symbol.size(), 0 ));
    
    
    		
    	for ( i = 0; i < statenos; i++ )
    	{
    		if (finstateflag[i] == 1)
    			continue;
    		else
    		{
    			for ( j = 0; j < symnos; j++ )
    			{
    				k = 0;
    
    
    				do
    				{
    					cout<<"Enter Next State for "<<state[i]<<" when "<<symbol[j]<<" is entered. ";
    					cout<<"Press 0 if there is no state left : ";
    					cin>>c;
    					
    					if ( c != '0')
    					{
    						NextState[i][j][k] = c;
    						k++;
    						counter[i][j] = k; 
    					}
    					else
    						break;
    				} while ( c != '0');
    				
    			}
    		}
    	}
    
    
    	for ( i = 0; i < statenos; i++ )
    	{
    		if (finstateflag[i] == 1)
    		{
    			cout<<endl<<state[i]<<" -> lamda";
    			continue;
    		}
    		else
    		{
    
    
    			for ( j = 0; j < symnos; j++ )
    			{
    				k = counter[i][j] - 1;
    				while ( k >= 0 )
    				{				
    					cout<<endl<<state[i]<<" -> "<<symbol[j]<<NextState[i][j][k];
    					k--;
    				}
    			}
    		}
    
    
    	}
    
    
    	getchar();
    }
    Doesn't seem too neat. I'll try to do this using class next week. Any tips for doing so will be appreciated.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    A couple of things to first is the use of exit(). You should avoid using this C function in a C++ program. It doesn't properly call C++ destructors and can lead to problems. Since the only function you are using this in main() a simple return(1) would be preferred.

    Second in the following snippet:
    Code:
        for ( i = 0; i < statenos; i++ )
        {
            if (finstateflag[i] == 1)
            {
                cout<<endl<<state[i]<<" -> lamda";
                continue;
    There is no need for the continue statement, the next statement after the if else block is the end of the loop. The same with the break in the previous loop. That loop will end without the break if the if condition is not met.


    Jim
    Last edited by jimblumberg; 04-18-2013 at 10:21 AM.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Be aware that the index operator ([]) in vectors do not do bounds checking. In this case, since you are using Visual Studio, it is fine since the debugger will by default trigger an assert if you do an out of bounds access, but this may not apply to other implementations in other compilers. When in doubt, use .at() to help catch those pesky bugs.
    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.

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    21
    Thank you, Jim and Elysia. You certainly made coding in C++ easier for me. Actually, I'm more familiar with C. C++ is a uncharted territory for me and I'm not aware of the norms and etiquette of programming in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any Room For Improvement?
    By Clinthill98 in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2010, 07:11 AM
  2. An improvement for the board...
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-19-2006, 05:32 PM
  3. code improvement before i go further!!
    By samirself in forum C Programming
    Replies: 2
    Last Post: 06-24-2005, 11:52 PM
  4. Howz this for improvement!
    By cyberCLoWn in forum C++ Programming
    Replies: 5
    Last Post: 01-18-2004, 06:58 PM
  5. C++ improvement
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-17-2003, 12:46 AM

Tags for this Thread