# A practical help

• 12-28-2007
Ducky
A practical help
Would there be a way to do this simplier?

Code:

```    if (pre2 == "0\n"||pre2 == "1\n"||pre2 == "2\n"||         pre2 == "3\n"||pre2 == "4\n"||pre2 == "5\n"||         pre2 == "6\n"||pre2 == "7\n"||pre2 == "8\n"||pre2 == "9\n")```
Something like
(pre2 == int'\n')
I know its not good but you can get the idea what i mean.
• 12-28-2007
Zigs
Wouldn't it be possible to have a function which took an int and a string and returned the int and the string as one string and then inset the function to replace the "0/n" part.

Something like:
Edit: Sorry i just noticed you said || and not &&.
besides, Elysia got a way easier solution.
• 12-28-2007
Elysia
One way:
Code:

```bool bMatch = false; for (int i = 0; i <= 9; i++) {         if ( pre2[0] != ('0' + i) )         {                 bMatch = true;                 break;         } } if (pre[1] == '\n' && bMatch); // Your code```
Or:
Code:

`if (isdigit(pre2[0]) && pre2[0] == '\n')`
• 12-28-2007
Ducky
Nice! Thank you Elysia!
• 12-28-2007
robatino
Why not just
Code:

`if (pre2.length() == 2 && isdigit(pre2[0]) && pre2[1] == '\n')`
• 12-28-2007
Elysia
Incorrect code?
• 12-28-2007
brewbuck
Quote:

Originally Posted by Elysia
And
Code:

`if (pre2.length() == 2 && isdigit(pre2[0]) && pre2[1] == '\n')`
Could also be shortened to:
Code:

`if (isdigit(pre2[0]) && pre2[1] == '\n')`

Because out-of-bounds memory accesses are A-ok?
• 12-28-2007
Elysia
Not definitely not, but no-one says it IS out of bounds. I'll just have to assume you're doing some basic checks before.
• 12-28-2007
robatino
Depending on the context of the OP's code, it's very possible that one or two of the conditions in my test might be known true and could be skipped, but at the moment, only the OP knows that.
• 12-29-2007
Ducky
Yes Robatino on second thought you're right , it depends on the context, that just i forgot to specify.

Actually i cant use either one because im gonna read a whole file once and pre2[0] would be the first character in the file however what i want is to find the combination of a number with \n in the file.

Thanks though for the help , it helped me to understand more the problem.