5 Replies - 556 Views - Last Post: 23 October 2009 - 01:36 PM Rate Topic: -----

#1 nebrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-September 09

how to fix this strange error

Posted 23 October 2009 - 11:11 AM

Here is my code,
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

main()
{
	while(1)
	{
		ifstream file("run.inf");
		string line;
		string value0 = "protect = 0";
		string value1 = "protect = 1";

		do
		{
			getline(file, line);
		} while(!file.eof());

		// strange behavior of the program
		if(line.find(value1))
		{
			cout << "protecting" << endl;
			return 0;
		}
		else if(line.find(value0))
		{
			cout << "no protection" << endl;
			return 0;
		}
	}

	return 0;
}


how can I fix this error, when there is file conatining "protect = 1" program prints "no protecting" and vice versa?

How can I check if file exists?

Is This A Good Question/Topic? 0
  • +

Replies To: how to fix this strange error

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: how to fix this strange error

Posted 23 October 2009 - 11:26 AM

Its only checking the last line of the file -- is that what it should do?

And that do-while loop can be simplified to just this
while( getline(file, line) )
  ;



What you probably want is this:
while( getline(file, line) )
{
		if(line.find(value1))
		{
			cout << "protecting" << endl;
			return 0;
		}
		else if(line.find(value0))
		{
			cout << "no protection" << endl;
			return 0;
		}
}



This post has been edited by Ancient Dragon: 23 October 2009 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 nebrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-September 09

Re: how to fix this strange error

Posted 23 October 2009 - 01:02 PM

this doesn't solve the problem, the program never comes to the else if, when when the first if loop isn't true.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: how to fix this strange error

Posted 23 October 2009 - 01:08 PM

Read the manual, and take note of the return values for find.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: how to fix this strange error

Posted 23 October 2009 - 01:10 PM

if(line.find(value1,0)!=string::npos)


Was This Post Helpful? 0
  • +
  • -

#6 nebrad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-September 09

Re: how to fix this strange error

Posted 23 October 2009 - 01:36 PM

if I delete returns from if and else if loops program loops, I don't want that I want my program to print the message once for correct string in file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1