inFile >> x;
        while(!inFile.eof())
	{
		if(x == '\n')
		{
			cout << endl;
		}
		cout << x;
		inFile >> x;
	}

will this code detect end of line? I'm having trouble with this. Can anyone help?

Sometime newline ends with ^M .

Sometime newline ends with ^M .

Its not a matter of "sometimes". It either does, or it doesn't, depending on the operating system.
'\n' translates into whatever the operating system uses as end-of-line. MS-Windows: "\r\n". *nix: "\r". Not sure what MAC uses.

If the file was opened as a text file, then you will only ever see a \n. It's independent of how your OS chooses to represent newlines.

@OP
If it isn't working, you need to supply more information.

When a file is opened is in Text Mode, the operating system's new-line character is auto-magically converted to the standard '\n'.

>Not sure what MAC uses.
BTW Mac uses "\r".
"On unix systems, the standard end of line character is line feed ('\n')."
source: https://developer.mozilla.org/En/C___Portability_Guide

i'm writing this on visual studio with vista. I will be transfering it to unix soon. This does not work on neither.

after fiddling with it, I also notice that it doesn't detect whitespace either. My program is suppose to find the sum of each line, so if i cannot detect the end of line, i cannot get it to work :(.

>>My program is suppose to find the sum of each line
sum of each line??
what does that means?
and yeah, plz post the complete code to help us work upon.

ok, the after searching around I found the poblem. The reason why it is not reading the end of line is because the >> operator skips all isspace character. Instead use

inFile.get(x);

I had a program where I need to read in data,
I ended up tokenizing each line and looping while(!.eof())

while(!eof() ) doesn't detect eof like you and many others think. It will allow your program to process the last line or character twice.

why don't you just read a whole line at a time then you don't have to worry about eol character(s)

std::string line
ifstream in("filename.txt");
while( getline(in,line) )
{
    // do something with this line
}

while(!eof() ) doesn't detect eof like you and many others think. It will allow your program to process the last line or character twice.

why don't you just read a whole line at a time then you don't have to worry about eol character(s)

std::string line
ifstream in("filename.txt");
while( getline(in,line) )
{
    // do something with this line
}

the reason why I'm doing it the way I am is because I'm taking each character and putting it in a linked list stack to be computed.

example of how the file would look like.

5 + 7 * (9 - 6) + 3
8 + 4 / 2
(8 + 4) / 2
(6 + (7 - 3)) * ((9 / 3) + 2) * 4

each line will have an output of the computation of it.

BTW, how do you suggest I detect eof?

What I would suggest doing, if you are reading in lines like you gave, is what Ancient Dragon said but then using stringstream to break up the lines into characters. It would look something like this.

stringstream ss;
  while (getline (inFile, line))
        { ss << line;
          while (ss >> x)
                { cout << x; 
                  /* something you want to do */ }
          cout << endl;
          ss.clear();
        }

For how to determine eof, I just let the compiler handle it by using something like while(inFile) normally or some variation of it, like the one Ancient mentioned. There's probably a better way, but that's what I've done.

What I would suggest doing, if you are reading in lines like you gave, is what Ancient Dragon said but then using stringstream to break up the lines into characters. It would look something like this.

stringstream ss;
  while (getline (inFile, line))
        { ss << line;
          while (ss >> x)
                { cout << x; 
                  /* something you want to do */ }
          cout << endl;
          ss.clear();
        }

For how to determine eof, I just let the compiler handle it by using something like while(inFile) normally or some variation of it, like the one Ancient mentioned. There's probably a better way, but that's what I've done.

In your case, x is a single character right? that is what I need. Because I'm doing an infix stack operation on the lines posted above with linked list, so it is important that 'x' being stored into the linked list is a single character.

Yes, x was a character. stringstream allows you to manipulate strings as if there were input. So, I just treated a line as if it were input and had it do what you originally had.

here is what I have so far

stacks.cpp

#include <iostream>
#include "stacks.h"
using namespace std;

//append a new node with the data to the stack
void Stacks::push_opnd(Item_opnd entry)
{
	Node *temp;
	temp = new Node;
	temp -> data_opnd = entry;
	temp -> next = top;
	top = temp;
	++depth;
}

//remove the top node
int Stacks::pop_opnd()
{
	Node *temp;
	Item_opnd popped;

	popped = top -> data_opnd;
	temp = top;
	temp = top -> next;
	delete temp;
	--depth;
	return popped;
}

//append a new node with the data to the stack
void Stacks::push_optr(Item_optr entry)
{
	Node *temp;
	temp= new Node;
	temp -> data_optr = entry;
	temp -> next2 = top2;
	top2 = temp;
	++depth;
}

//remove the top node
char Stacks::pop_optr()
{
	Node *temp;
	Item_optr popped;

	popped = top2 -> data_optr;
	temp = top2;
	temp = top2 -> next2;
	delete temp;
	--depth;
	return popped;
}

//read the characters
void Stacks::read()
{
	Node *temp;
	temp = top2;
	if(temp == NULL)
	{
		cout << "no characaters left or input" << endl;
	}
	else
	{
		for(temp; temp != NULL; temp = temp -> next2)
		{
			cout << temp -> data_optr << endl;
		}
	}
	delete temp;
	cout << "made it" << endl;
}

//detect to see if the top node is empty
bool Stacks::empty()
{
	if(top2 == NULL && top == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}

stacks.h

#ifndef STACKS_H
#define STACKS_H
#include <iostream>

class Stacks{
	typedef int Item_opnd;
	typedef char Item_optr;

public:
	Stacks()
	{
		top = NULL;
		depth = 0;
	}
	//~Stacks();

	void push_opnd(Item_opnd);
	int pop_opnd();
	void push_optr(Item_optr);
	char pop_optr();
	void compute();

	void read();
	bool empty();
	
	

private:

	struct Node{
		int data_opnd;
		char data_optr;
		Node *next;
		Node *next2;
	};
	Node *top;
	Node *top2;
	int depth;
};

#endif

main program

#include <iostream>
#include "stacks.h"
#include <cctype>
#include <fstream>

using namespace std;

int main()
{
	ifstream inFile;
	char x;
	char filename[20];
	int y;

	int count = 20;
	Stacks S;

	cout << "enter the name of the file here -> ";
	cin >> filename;
	inFile.open(filename);
	inFile.get(x);
	while(inFile)
	{
		
		//S.compute();

		if(x != '\n')
		{
			if(x != ' ')
			{
				if(isdigit(x))
				{
					if(int(x) == 48)
						y = 0;
					else if(int(x) == 49)
						y = 1;
					else if(int(x) == 50)
						y = 2;
					else if(int(x) == 51)
						y = 3;
					else if(int(x) == 52)
						y = 4;
					else if(int(x) == 53)
						y = 5;
					else if(int(x) == 54)
						y = 6;
					else if(int(x) == 55)
						y = 7;
					else if(int(x) == 56)
						y = 8;
					else if(int(x) == 57)
						y = 9;
					S.push_opnd(y);
				}
				else
				{
					if(x != '(')
					{
						S.push_optr(x);
					}
				}
			}
		}
		else
		{
			/*if(!S.empty())
			{
				exit(1);
			}*/
		}
		cout << x;
		inFile.get(x);
	}
	cout << endl;
	S.read();
}

please disregard the commented lines of code. The program is incomplete and I put in the comments to test and make sure the part i'm focused on is working.

so far, the program reads inputs from a file, it collects the integers and put it into a linked list stack. It will collect everything else and put it into another stack. Then output the items from the non-integer stack. I'm getting core dumped now :( it is really driving me nutz.

It seems as if the error is either related to the output function or the collection of data in the main function.

omfg!!!! took 2 hours to figure this out. the problem was i didn't set

top2 == NULL!!!!! in stacks.h

Heh, don't you just hate when you have issues like that. I'm working on one where I had || instead of && and I was wondering why it wasn't jumping out. Glad that you figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.