CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    23

    Problem using case '\n': with an inputted char.

    I'm trying to search a specified file for all instances of substring "abbaa."

    This is an easy problem, and I know I can do it better than I have, but the professor is making us use a 6x3 array in order to help us understand finite state machines.

    Well the code I wrote runs perfectly, but the case for '\n' doesn't catch. It just flows through without it. Any ideas? (it's probably something extremely simple )

    Thanks in advance!

    Significant portion of code:
    Code:
    		if(fin)
    		{
    			fin >> inChar;
    			while(fin)
    			{
    				switch(inChar)
    				{
    				case 'a':
    					currentState = state[currentState][a];
    					break;
    				case 'b':
    					currentState = state[currentState][b];
    					break;
    				case '\n':
    					lineCount++;
    					colCount = 0;
    					currentState = state[currentState][other];
    					break;
    				default:
    					currentState = state[currentState][other];
    					break;
    				}
    				fin >> inChar;
    				if(currentState == S5)
    					cout << endl << "* line " << lineCount << ", column " << colCount - 4;
    				colCount++;
    			}
    			quit = true;
    		}

    Program:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	//declerations and definitions
    	enum { S0, S1, S2, S3, S4, S5 };
    	enum { a, b, other };
    
    	fstream 
    		fin;
    
    	string
    		file;
    
    	char
    		inChar;
    
    	bool
    		quit = false;
    
    	int
    		state[6][3], //6x3 array for state
    		currentState = S0,
    		lineCount = 1,
    		colCount = 1;
    
    	
    	/*
    	currentState = &state[*currentState][a];
    	cout << *currentState;
    	*/
    	
    	//define the array
    	for(int i = S0; i <= S5; i++)
    		state[i][other] = S0;
    	
    	state[S0][a] = S1;
    	state[S1][a] = S1;
    	state[S2][a] = S1;
    	state[S3][a] = S4;
    	state[S4][a] = S5;
    	state[S5][a] = S1;
    
    	state[S0][b] = S0;
    	state[S1][b] = S2;
    	state[S2][b] = S3;
    	state[S3][b] = S0;
    	state[S4][b] = S2;
    	state[S5][b] = S2;
    	
    	while(quit == false)
    	{
    		//open the fin stream
    		cout << "Please enter the input file:" << endl 
    			 << "* ";
    		cin >> file;
    		fin.open(file,ios::in);
    
    		//check for fin stream
    		if(fin)
    		{
    			fin >> inChar;
    			while(fin)
    			{
    				switch(inChar)
    				{
    				case 'a':
    					currentState = state[currentState][a];
    					break;
    				case 'b':
    					currentState = state[currentState][b];
    					break;
    				case '\n':
    					lineCount++;
    					colCount = 0;
    					currentState = state[currentState][other];
    					break;
    				default:
    					currentState = state[currentState][other];
    					break;
    				}
    				fin >> inChar;
    				if(currentState == S5)
    					cout << endl << "* line " << lineCount << ", column " << colCount - 4;
    				colCount++;
    			}
    			quit = true;
    		}
    		else
    		{
    			do
    			{
    				system("CLS");
    				cout << "The input file was not opened successfully. Would you like to try again?" << endl
    					 << "(y/n):" << endl 
    					 << "* ";
    				cin >> inChar;
    				inChar = toupper(inChar);
    			} 
    			while(inChar != 'Y' && inChar != 'N');
    
    			if(inChar == 'N')
    				quit = true;
    			system("CLS");
    		}
    	}
    	cout << endl << endl;
    	system("PAUSE");
    	return 0;
    }
    Note: I know this code is pretty sloppy, but I've done it in just a few hours (most of which was trying to solve this bug). I will tidy it up later.
    Note2: When I get better at coding I plan to contribute to this community instead of just asking questions all of the time!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem using case '\n': with an inputted char.

    Quote Originally Posted by Filthy_Utter View Post
    Well the code I wrote runs perfectly, but the case for '\n' doesn't catch. It just flows through without it. Any ideas? (it's probably something extremely simple )
    Have you debugged your code using your compiler's debugger? What happens when the current character is a carriage return? What happens right before this (the very last character before the carriage return)? Is that processed successfully? If so, then use your debugger to step through the program to see what it does with the next character (the return).

    Note2: When I get better at coding I plan to contribute to this community instead of just asking questions all of the time!
    Programming isn't just about writing code and running code. It is also about debugging code that doesn't work. No programmer writes perfect code (unless the program is a toy program), so debugging is part and parcel of learning how to write programs.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 24th, 2012 at 02:30 AM.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem using case '\n': with an inputted char.

    istreams, by default, skip all manners of white spaces (space, tabs, carriages, etc...).

    You can use std::noskipws if you need to take ws into account.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Feb 2012
    Posts
    23

    Re: Problem using case '\n': with an inputted char.

    Thanks Paul, I do try really hard to learn from my mistakes. I know it will pay off one day.
    But, I had tried using the debugger and could not find out why it was skipping. It just wasn't catching.

    Monarch's advice worked, however. I knew you couldn't read white space from files, now I don't even know what I was expecting it to input!

    Thanks for the help and replies! =)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured