A practical help

This is a discussion on A practical help within the C++ Programming forums, part of the General Programming Boards category; Would there be a way to do this simplier? Instead of this Code: if (pre2 == "0\n"||pre2 == "1\n"||pre2 == ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    A practical help

    Would there be a way to do this simplier?

    Instead of this
    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.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    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.
    Last edited by Zigs; 12-28-2007 at 01:17 PM. Reason: Yeah, I'm a noob >_>"

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    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')
    Last edited by Elysia; 12-28-2007 at 04:02 PM. Reason: Typo
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Nice! Thank you Elysia!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Why not just
    Code:
    if (pre2.length() == 2 && isdigit(pre2[0]) && pre2[1] == '\n')

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    Incorrect code?
    Last edited by Elysia; 12-28-2007 at 04:00 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Elysia View Post
    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?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Practical C Programming, Third Edition
    By jc99 in forum C Programming
    Replies: 10
    Last Post: 06-19-2009, 09:24 PM
  2. practical programming
    By IXxAlnxXI in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2008, 08:06 AM
  3. practical c programming - oreilly problem ?
    By goosematt in forum C Programming
    Replies: 18
    Last Post: 12-09-2004, 07:25 AM
  4. making it practical
    By zbap in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2002, 09:51 AM
  5. Satalite Internet in UK
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-19-2002, 07:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21