Input Stream

Why the extra loop?

Page 1 of 1

5 Replies - 1193 Views - Last Post: 22 January 2009 - 10:07 PM Rate Topic: -----

#1 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

Input Stream

Posted 21 January 2009 - 08:38 PM

I am reading a simple text file with precisely 4 lines. Each line is in infix notation.

A+B*(C-D)
C*D*A+B
B-(C/D)*A
B+C*(D+A)

I want to find the post fix notation to each one. That's not to difficult to do. The problem I am having is that
when I read in the text file using the getline function the do while loop will loop 5 times.
How can I get it to loop 4 times? The extra loop does not appear to have any thing in it.
Please help me.
Here is a snippet of the code in main.
   do
   {
	  getline(readindata, infix);
	  convert(infix, postfix);/*This function converts
								infix to postfix expression.*/
   }
   while (readindata.fail( ) == false);



I tried making it a while loop only. No help. Why does it loop 5 times? There are only 4 lines of text.

Is This A Good Question/Topic? 0
  • +

Replies To: Input Stream

#2 FrozenSnake   User is offline

  • En man från Sverige!

Reputation: 123
  • View blog
  • Posts: 1,033
  • Joined: 30-July 08

Re: Input Stream

Posted 21 January 2009 - 08:44 PM

I might be incorrect but
1 run = find text = readindata.fail( ) == false
2 run = find text = readindata.fail( ) == false
3 run = find text = readindata.fail( ) == false
4 run = find text = readindata.fail( ) == false
5 run = didnt find text = readindata.fail( ) != false break loop

the code is in:
do { this } while();
So it will run the loop as long as the while is true.

This post has been edited by FrozenSnake: 21 January 2009 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

Re: Input Stream

Posted 21 January 2009 - 09:59 PM

Well at first I tried just a while loop.
while( )
{ do something here}

But that didn't work. So I threw this in the do-while loop.
do
{
	  if (readindata.fail( ) == true) // This was suppossed to catch it on the fifth loop!
		 break;
	  getline(readindata, infix);
	  convert(infix, postfix);/*This function converts
								infix to postfix expression.*/
}
while (readindata.fail( ) == false);



The loop still loops 5 times! Please advise. :crazy:
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,509
  • Joined: 10-May 07

Re: Input Stream

Posted 21 January 2009 - 10:05 PM

I don't believe that break is going to exit the do loop.

With do being on the top, this means the check will happen at the end of your loops. So the result of the last loop will be at the start of your loop.

You want to put the conditional while check at the top, because this will find when readindata.fail( ) equals true prior to running through the loop.
Was This Post Helpful? 0
  • +
  • -

#5 matthew180   User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: Input Stream

Posted 21 January 2009 - 10:30 PM

You don't seem to have any checks for blank lines in your code, which would be read in just like any other line. Something like the code below might work for you.

Matthew

--- Output ---
Line: 1 [A+B*(C-D)]
Line: 2 [C*D*A+B]
Line: 3 [B-(C/D)*A]
Line: 4 [B+C*(D+A)]

--- Output for input file with extra blank lines ---
Line: 1 [A+B*(C-D)]
Line: 3 [C*D*A+B]
Line: 5 [B-(C/D)*A]
Line: 7 [B+C*(D+A)]


#include <stdio.h>
#include <string.h>

#define MAX_LINE 80

int
main(void)
{
	FILE *fp;
	char linebuf[MAX_LINE + 1];
	char *line;
	size_t len;
	int linecount;

	fp = fopen("infix.txt", "r");
	if ( fp == NULL )
		return(1);

	linecount = 0;

	while ( (line = fgets(linebuf, MAX_LINE, fp)) != NULL )
	{
		linecount++;
		len = strlen(line);

		// Skip blank lines
		if ( len < 2 )
			continue;

		// Chop off any newline character
		if ( line[len - 1] == '\n' )
		{
			len--;
			line[len] = 0;
		}

		printf("Line: %d [%s]\n", linecount, line);
	}

	return(0);
}
// main()


Was This Post Helpful? 0
  • +
  • -

#6 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

Re: Input Stream

Posted 22 January 2009 - 10:07 PM

After a closer inspection of the code I provided recently you will notice that the
readindata.fail( ) becomes true about half way into the loop after a getline. So even though
the getline doesn't get anything and readindata.fail( ) becomes true there is still a function called after.
This is what was causing massive problems.

Modified the code looks like this...
while (readindata.fail( ) != 1)
{
	  getline(readindata, infix);
	  if (readindata.fail( ) == 1)//This will handle the input failure, causing program to break on loop 5. 
		 break;
	  postfix = convert(infix);/*This function converts
								infix to postfix expression.*/
	  assembly = convertToAssembly(postfix);
	  //Do something with the assembly or postfix here.
}



Oh and if any one wants this program I can get you a copy. I created a stack that is used to create infix notation
expressions to post fix notation (RPN). The post fix can easily be converted to assembly language for CPUs.
Anybody have a robot that needs some custom assembly language code? B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1