0

Hi, I am trying to read an input text file. And read the last letter only and check if it is an '+' or '*'.

Here is the code I have written so far, if anyone can help me, I would be very greatful.

while(input>>lastChar) //put file contents into lastChar
  {
	  input.seekg(ios::end,lastChar);  //go to the end of file
	  if(lastChar=='+'  || lastChar=='*') //check if it is + or *
	  {
	  		badInput=true;
		goto skip;
	  }
	  else
	  {
		cout<<"true file"<<endl;
		goto startHere;
	  }
  }
4
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by WaltP
0

To get the last letter from a file you would use

//...
ifstream fin("input.txt");
fin.seekg(ios::end);
char ch;
fin >> ch;
if (ch == '+' || ch == '*')
//...

Also it is bad practice to use the goto command

0

I thought this looked familiar, then when I went and looked it was your thread.

You're not using seekg() correctly. The second parameter is the seek-direction (position really, ios::end in this case), while the first is an offset from that position (in your case 0, or possibly -1, see what works on a file with known content). cplusplus.com is a great reference.

Now think about what your loop needs to do, and the order in which it needs to do it. And what's with the goto statements? You're not programming in BASIC any more.

P.S. Apparently Nathan was answering at the same time I was! :)

Edited by raptr_dflo: forgot link to cplusplus.com

0

And I'm still waiting for you to

1) Place comments on each line of your code that explains why that line is there. "Because someone said so" is not acceptable.
2) Write down the steps needed in detail to check the beginning of the file for your bad file indicators
3) Start outputting values to see if each variable, each array element, etc contains what you think it should -- based on #2

I didn't suggest this to be mean. I suggested this so you can learn something. It will help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.