Hello, basically I want to read an input .txt file, and check to see if '+' or '*' is at the beginning or end of the file. The file can have a space at the beginning, and that should be ok.


This is the code that I think should work, but does not. If anyone could offer me some helpful advice, it would be much appreciated. Also if you could tell me where I went wrong, that would be great too.

char firstChar[256];
  size_t chars_read= 0; 
  while (input.good())
  {
	  nextChar = (char) input.get();
	  firstChar[chars_read]=(char)input.get();
	  if(input)
		  ++chars_read;

	  if(firstChar[1]=='+')
			badInput=true;
			
	  
		  
	  
	   if(firstChar[ chars_read-1]=="+")
		  badInput=true;
		  

	   if(badInput)
		   break;
char firstChar[256];
size_t chars_read= 0; 
int currentLocation = 0;

while (input.good())
{
  nextChar = (char) input.get();
  firstChar[chars_read++]=(char)input.get();
}
 
while(firstChar[currentLocation] == ' ' || firstChar[currentLocation] == '\t' || firstChar[currentLocation] == '\n' ) \\ ignore all white spaces
currentLocation++;

if (firstChar[currentLocation]=='+' || firstChar[currentLocation]=='*')
	badInput=true;

currentLocation = chars_read; 

while(firstChar[currentLocation] == ' ' || firstChar[currentLocation] == '\t' || firstChar[currentLocation] == '\n' ) \\ ignore all white spaces
currentLocation--;


if (firstChar[currentLocation]=='+' || firstChar[currentLocation]=='*')
	badInput=true;

You should check for syntax error as i did not compile this :)

small correction to sundip code

currentLocation = chars_read;

It should be
currentLocation = chars_read - 1; //since in while loop it is post incremented

char firstChar[256];
size_t chars_read= 0; 
int currentLocation = 0;

while (input.good())
{
  nextChar = (char) input.get();
  firstChar[chars_read++]=(char)input.get();
}

So now in the firstChar[] array you have every other (even numbered) characters from the file. 1st, 3rd, 5th ... characters are not in the array. And this is supposed to help how?

char firstChar[256];
size_t chars_read= 0; 
int currentLocation = 0;

while (input.good())
{
  nextChar = (char) input.get();
  firstChar[chars_read++]=(char)input.get();
}
 
while(firstChar[currentLocation] == ' ' || firstChar[currentLocation] == '\t' || firstChar[currentLocation] == '\n' ) \\ ignore all white spaces
currentLocation++;

if (firstChar[currentLocation]=='+' || firstChar[currentLocation]=='*')
	badInput=true;

currentLocation = chars_read; 

while(firstChar[currentLocation] == ' ' || firstChar[currentLocation] == '\t' || firstChar[currentLocation] == '\n' ) \\ ignore all white spaces
currentLocation--;


if (firstChar[currentLocation]=='+' || firstChar[currentLocation]=='*')
	badInput=true;

Hello, I tried to compile the code, it compiled without errors but did not give the correct output. My input file was " +43+3+3 +32", and that should have resulted in a badinput because the file begins with a '+'.

I tried to alter your code several different ways, but 2 hours later, I am still unable to figure this out. This is what I have so far,

char firstChar[256];
size_t chars_read= 0; 
int currentLocation = 0;
  while (input.good())
  {
	  nextChar = (char) input.get();
	  firstChar[chars_read++]=(char)input.get();


	  while(firstChar[currentLocation] == ' ' || firstChar[currentLocation] == '\t' || firstChar[currentLocation] == '\n' ) // ignore all white spaces
currentLocation++;
 
if (firstChar[currentLocation]=='+' || firstChar[currentLocation]=='*')
		badInput=true;
		goto skip;
	

currentLocation = chars_read-1; 
 
while(firstChar[currentLocation] == ' ' || firstChar[currentLocation] == '\t' || firstChar[currentLocation] == '\n' ) // ignore all white spaces
currentLocation--;
 
 
if (firstChar[currentLocation]=='+' || firstChar[currentLocation]=='*')
	badInput=true;
	goto skip;

//If there is no '+' or '*' at beginning or end of file, then continue with rest of code.



//skip is here
if(badinput)
out<<file is invalid

Can anyone tell me what I did wrong?

Can anyone tell me what I did wrong?

Using code that obviously isn't designed to work, as I stated already.

You are making this much more complicated than it has to be.

How large do you expect the file to be? (maximum)
How many lines are you expecting to have? (maximum)

Using code that obviously isn't designed to work, as I stated already.

You are making this much more complicated than it has to be.

How large do you expect the file to be? (maximum)
How many lines are you expecting to have? (maximum)

Hi walt, the maximize size the file can be is 60kb. And no more than 20 lines.
You said the code isn't designed to work, were you referring to the code i originally posted? Or to sundips code?

You said the code isn't designed to work, were you referring to the code i originally posted? Or to sundips code?

Sundips. Didn't you bother reading my post between his code and your attempt? I explained exactly what was wrong with his code.

The maximize size the file can be is 60kb. And no more than 20 lines.

To test the 1st character, read the first line. Test for and ignore all initial SPACES. If the first non-SPACE is a digit, you're OK, otherwise a bad file.
To test the last character, read the rest of the file. When you get to EOF, test the last characters in your buffer for whitespace (which includes the \n) which you skip. The first non-whitespace character also needs to be a digit, otherwise the file is bad.

Sundips. Didn't you bother reading my post between his code and your attempt? I explained exactly what was wrong with his code.


To test the 1st character, read the first line. Test for and ignore all initial SPACES. If the first non-SPACE is a digit, you're OK, otherwise a bad file.
To test the last character, read the rest of the file. When you get to EOF, test the last characters in your buffer for whitespace (which includes the \n) which you skip. The first non-whitespace character also needs to be a digit, otherwise the file is bad.

Hi walt, so I used getline to read the first line and to check for spaces. here is my code

string firstChar;
int chars_read= 0; 
int currentLocation = 0;
  while (input.good())
  {
	  nextChar = (char) input.get();
	  getline(input,firstChar);
	  while(firstChar[chars_read]==' ' || firstChar[chars_read]=='\t' ||firstChar[chars_read]=='\n')
		  chars_read++;
	  if(firstChar[chars_read]<'0' || firstChar[chars_read]>'9')
		  {badInput=true;
	  goto skip;
	  }

my input file is "67+43+4+2" and the code will only output the first number, this file is ok and my code should output the sum.... which it is able to do if i take out the above code out of my program. But if i leave this code in, then it only outputs the first number and not the sum.

second: my input file is '+32+32+3+4", this file is invalid, but the code above outputs a '0' instead.

Can you give me some advice on what I did wrong? Sorry If i am so stupid, this is my first c++ class, and i am very eager to learn :)

Look, you can't program by
1) running the code,
2) seeing that it doesn't work
3) arbitrarily changing something
4) going back to 1

You need to
A) Think about what you are doing
B) Test what you try
C) If it doesn't work, find out why
D) Fix it

I want you to do three things:
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

And if your present code doesn't look like it will be easy to fix, start over and do only the beginning-file test. Once that works, add the end-file test.

> I used getline to read the first line and to check for spaces

By default, formatted input ignores whitespaces. input >> nextChar ; // nextChar contains the next non-whitespace char So,

#include <fstream>
#include <cctype>

// good if there are digits at the beginning and end of file
bool good_file( const char* const path )
{
    std::ifstream file(path) ;

    // try to read the first non-ws char and check if it is a digit
    char first_non_ws ;
    if( file >> first_non_ws && std::isdigit(first_non_ws) )
    {
        char last_non_ws = first_non_ws ;
        while( file >> last_non_ws ) ; // read till the last non-ws char
        return std::isdigit(last_non_ws) ;
    }
    else return false ;
}
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.