Hi ..

I have this program, its reding correctly but its not doing what i want
i.e. it suppose to read the input file and call the functions according to the action saved in the file

this is the problem :

#include <iostream>
#include <Fstream>

using namespace std;

void move();
void turn();
void turnOff();

int x, y;
char a, d;
int A[30][30];

int main()
{
	ifstream infile;

	infile.unsetf( ios::skipws );

	infile.open( "input.txt" );
	infile>>x>>y;
	infile>>d;

	if( x<0 || y<0 )
	{
		cout<<"the parameters should be positive!/n/n";
		exit(0);
	
	}
	else
		A[x][y];


	cout<<"BEGINNING-OF-PROGRAM\n"<<"\n\nThe initial Position: ("<<x<<", "<<y<<")\n";
	cout<<"The initial Direction: "<<d<<endl<<endl;

	while( !infile.eof() )
	{
		infile>>a;
		switch(a){
		case'm':case'M':do
				{//********** HERE IS THE PROBLEM ***************
					infile>>a;
					if( a=='o' || a=='v' || a=='e' )
					{
						move();
						break;
					}
					else
					{
						cout<<"\n1)Error in the input file! ";
						return 0;
					}

				}while( a!=';');break;

		case't':case'T':do
				{
					infile>>a;
					if( a=='u' || a=='r' || a=='n' )
					{
						if( a==';' )
						{
							turn();
							break;
						}
						else if( a=='O' || a=='f' || a=='f')
						{
							turnOff();
							break;
						}

					}
					else
					{
						cout<<"\nError in the input file! ";
						return 0;
					}

				}while( a!=';');break;

		default:break;
		}//*********************

	}


	cout<<"END-OF-PROGRAM\n"<<"\n\nThe Final Position: ("<<x<<", "<<y<<")\n";
	cout<<"The Fial Direction: "<<d<<endl<<endl;


	infile.close();

	return 0;

}

void move()
{
         cout<<"Move\n";
}

void turn()
{
	cout<<"Turn\n";
}

void turnOff()
{
       cout<<"TurnOff\n";

}

the input file :

4 5 
E
move;
move;
move;
turn;
turn;
move;
turn;
move;
move;
move;
turnOff;

Please somebody HELP!!!!:S

Please describe the problem you are having more clearly. What are you expecting it to do and how does its behavior differ from the expected?

this program suppose to read the input text file and if the input is move it should out put move in the screan else if it is turn it should turn and so on..

the problem is : when it reads turn it does not go to the (turnOff) function !!
becase they have the same name .. What should I do?!!

You need to read and analyze the whole line as a single string instead of reading and analyzing individual characters.

Your read method for the first 2 lines of the file should be okay. But, starting with the third line, you should switch to either C-style strings or std::string objects instead of single/individual chars. Then you have to use normalization and comparison functions in conjunction with if statements.

like this!!

#include <iostream>
#include <Fstream>
#include <string>

using namespace std;

void move();
void turn();
void turnOff();

int x, y;
char a, d;
int A[30][30];


int main()
{
	ifstream infile;
//	ofstream outfile;
	std::string act;

//	infile.unsetf( ios::skipws );

	infile.open( "input.txt" );
	infile>>x>>y;
	infile>>d;

	if( x<0 || y<0 )
	{
		cout<<"the parameters should be positive!/n/n";
		exit(0);
	
	}
	else
		A[x][y];


	cout<<"BEGINNING-OF-PROGRAM\n"<<"\n\nThe initial Position: ("<<x<<", "<<y<<")\n";
	cout<<"The initial Direction: "<<d<<endl<<endl;

	getline(infile,act);

	while( !infile.eof() )
	{
		
		if( act == "Move;" || act == "move;" )
		{
			move();
			break;
		}	
		else if ( act == "Turn;" || act == "turn;" )
		{
			turn();
			break;
		}
	    else if( act == "TurnOff;" || act == "turnOff;")
		{
			turnOff();
			break;
		
		}
		else
		{
			cout<<"\nError in the input file! ";
			return 0;
		
		}

		getline(infile,act);

	}


	cout<<"END-OF-PROGRAM\n"<<"\n\nThe Final Position: ("<<x<<", "<<y<<")\n";
	cout<<"The Final Direction: "<<d<<endl<<endl;


	infile.close();
//	outfile.close();

	return 0;

}

void move()
{
	cout<<"Move\n";
}

void turn()
{
	cout<<"Turn\n";
}

void turnOff()
{
	cout<<"TurnOff\n";
	
}

its not outputing anything!!

There's no need to get all excited.

When I execute your program, I get this:

BEGINNING-OF-PROGRAM


The initial Position: (4, 5)
The initial Direction: E


Error in the input file!

Are you getting this, or something else?

I suspect the issue may be a dangling newline in the input stream because you are changing from one type of input to another. Try throwing in a infile.ignore() at Line 27 (after you read in 'd').

Also, as a matter of good practice, immediately after attempting to open the file I would suggest you throw in a check to make sure your file is actually being opened successfully:

if (!inputFileStream.is_open()) {
  cout << "The file was not opened successfully!\n";
  exit(1);
}

I would also recommend that you reorganize your input statement(s) so that you aren't using ifstream::eof() as the condition for your while statement. It's generally not a good idea. Additionally, for temporary debugging reasons, I would suggest that you output "act" immediately after reading it to verify the value. Try this instead:

while( getline(infile,act) ) {
  std::cout << act << std::endl;
  //...
}

Edited 5 Years Ago by Fbody: n/a

I dont want just to output this statements , iuse the output to see is the program calling the right function or not!!


here its outputing the initial position as (0, 0)!!!!
and stops at the statement "error in the input file"?

while( 	getline(infile,act) )
	{
		
		if( act == "Move;" || act == "move;" )
		{
			move();
			break;
		}	
		else if ( act == "Turn;" || act == "turn;" )
		{
			turn();
			break;
		}
	    else if( act == "TurnOff;" || act == "turnOff;")
		{
			turnOff();
			break;
		
		}
		else
		{
			cout<<"\nError in the input file! ";
			return 0;
		
		}

		

	}

I change it to this and it the sam problem!!

I just want it to go to the functions !!

Because your program is now based on if statements, you don't want the break statements any more. The break statement, in addition to terminating switch cases, is also used to end a loop prematurely. This is not desirable behavior with the current layout of your code.

Have you added the ignore() statement that I mentioned earlier (in this post)? If you add that and remove the breaks, you should be good. When I make those changes, I get this:

BEGINNING-OF-PROGRAM


The initial Position: (4, 5)
The initial Direction: E

move;
Move
move;
Move
move;
Move
turn;
Turn
turn;
Turn
move;
Move
turn;
Turn
move;
Move
move;
Move
move;
Move
turnOff;
TurnOff
END-OF-PROGRAM


The Final Position: (4, 5)
The Final Direction: E

Edited 5 Years Ago by Fbody: n/a

Actually its work !!

thanks Mr. Fbody
thaaaaaaaanx alot..^^..

This question has already been answered. Start a new discussion instead.