Bench's Profile User Rating: *****

Reputation: 857 Master
Group:
Expert
Active Posts:
2,343 (0.88 per day)
Joined:
20-August 07
Profile Views:
21,594
Last Active:
User is offline Nov 02 2014 06:36 AM
Currently:
Offline

Previous Fields

Country:
GB
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
200
Expert In:
C/C++

Latest Visitors

Icon   Bench has not set their status

Posts I've Made

  1. In Topic: Functions Code bring up strange results

    Posted 22 Oct 2014

    View PostRemington_Andrews, on 22 October 2014 - 10:14 PM, said:

    float get_input()
    {
    	int num1;
    	printf("Enter a Real Number Greater than 0:  \n");
    	scanf_s("%d", &num1);
    	while (num1 < 0)
    	{
    		printf("Invalid Number, try again \n");
    		scanf_s("%d", &num1);
    	}
    	return num1;
    }
    
    Are you trying to use floating-point numbers or integers? Your program seems to be somewhat confused where you read an int but return a float, and pass floating-point numbers around elsewhere. Which are you really expecting to use?

    More information on scanf here - http://en.cppreferen...m/w/c/io/fscanf
  2. In Topic: Logical XOR in C++

    Posted 17 Oct 2014

    View Postsbesch, on 16 October 2014 - 09:54 PM, said:

    I don't want to sound like a trouble maker on my first post, but I'm afraid you are all wrong in essence. To wit, unless you want to depend on compiler implementation and good user behavior, I would not suggest using any of the solutions given without qualification.
    My initial reply to this thread is not in any way dependent on compiler implementation, it depends upon the fact that there is a logical equivalence between 'XOR' and (!(x&&y)) && (x||y).

    This is a mathematical truth, so it's actually impossible for that logic to fail, unless your compiler is completely broken.


    View Postsbesch, on 16 October 2014 - 09:54 PM, said:

    Even if you assume that all compilers on earth use the same standard values for true and false, I can pretty much guarantee that there will be some nutty user somewhere who will typecast and int to a boolean, then pass in all manner of non-zero values which when XOR'd will give non-zero results. In truth, the only guaranteed way to make this work is by writing an XOR function:
    bool XOR (bool a, bool b ){
     int ba=a?1:0;
     int bb=b?1:0;
     return ((ba ^ bb)==0);
    }
    
    
    That essentially performs the same logic as the function in my original reply by forcing the integer representation to one or zero. The disadvantage to your example 'in the real world', is that calling code may suffer warnings for types which do not implicitly cast to bool, such as void*.

    View Postsbesch, on 16 October 2014 - 09:54 PM, said:

    This still assumes that FALSE is zero, but I'm a lot more comfortable with that assumption than I am with the assumption that TRUE will always be the same positive value (such as 1). I agree that this is a pain. I agree that C++ should include this operator both for type safety and for cross compiler consistency. But it doesn't. Ah well
    There is no need to "assume" that false is a zero value because the ISO C++ standard provides an absolute cast-iron guarantee that false will always evaluate to zero for a standard-conforming C++ compiler.
  3. In Topic: Is there a better way to parse & format phone numbers?

    Posted 4 Sep 2014

    RegEx is specifically intended for pattern matching - so it's a very appropriate tool for parsing data such as phone numbers.

    In general, there's never an absolutely right/wrong way to create working code; although the preferred solutions are nearly always the simplest ones, and the ones which clearly show your intent at the time of writing it (i.e. intent to solve a particular problem - code which is expressive enough that its rationale is clear to anyone else reading - or yourself in 6 months time).

    While RegEx syntax can very easily become cryptic (some people avoid it for this reason), and 'how-it-works' is not always clear (especially not to those who don't use RegEx very often), the intent of code which uses RegEx pattern matching is often a lot more obvious, even if the pattern itself is hard-to-grok by casually glancing over it. RegEx itself is usually a lot more powerful and flexible for parsing nontrivial string formats too.

    As an example - the string 1-888-8888 could be matched with a pattern \d-\d{3}-\d{4} (You can validate the RegEx itself with any regex tool - e.g. http://regexhero.net/tester/ ) - with a little tinkering, you could extend that pattern to match different ways of expressing phone numbers.

    There's some decent example code for std::regex here (assuming you're using a recent C++11 compiler) - http://en.cppreference.com/w/cpp/regex


    It's also 'OK' to do things with a whole bunch of nested conditional/loops as well, but you need to consider how easy it is for code like that to get out-of-control - just a few special cases "over here" can lead to a few more hacks and special cases "over there" (particularly when you're dealing with magic numbers and fixed positions within a sequence); and you can easily end up with a couple of loops/If statements turning into a multi-headed hydra with dozens of paths and probably code which may be less flexible and whose intent is murky.
  4. In Topic: Logic error on C/C++ input-output

    Posted 13 Sep 2013

    char is a single character (single byte) in memory. the getchar() function will read a single character value; however, typing multiple characters as your input (for example "12") is classified as a string of characters.

    If you type in "12" then you're looking at 2 chars - '1' and '2' - that won't fit into a single char, so getchar() will only return '1'.

    Furthermore, the character code for '1' is not the same as integer 1. (Assuming you're using a system with an ASCII character set, then character '1' has an integer value of 49 - http://www.asciitable.com/

    If you're looking to read a different type, such as an integer, take a look at the scanf family of functions: http://www.cplusplus...e/cstdio/scanf/


    As a general comment, in visual studio you can set break-points to examine your code while it's running, in order to give yourself a way to inspect what's happening in memory while your program is running. If you click on a line in the code window and press F9 (or through the menu DEBUG -> Toggle Breakpoint), you'll get a red 'dot' next to the line. When you run your program with F5, the execution will pause at that line, and you will be able to mouse-over num and inspect its value - doing this can help show you exactly what's going wrong.
  5. In Topic: Not sure how to go about this program.

    Posted 13 Sep 2013

    Is your compiler showing any warnings related to loss of precision? Rememebr that an int is only capable of storing whole numbers (integers). It's also worth noting that float has very limited precision (on a typical 32-bit desktop, you can expect float to be accurate for no more than 7 significant figures).

    The default floating-point type in C and C++ is double

My Information

Member Title:
D.I.C Lover
Age:
32 years old
Birthday:
August 15, 1982
Gender:
Location:
UK
Years Programming:
11
Programming Languages:
C, C++, C#

Contact Information

E-mail:
Private
Website URL:
Website URL  http://

Comments

Page 1 of 1
  1. Photo

    Sy-Coderz Icon

    23 Jun 2012 - 09:47
    Hi,
    bro please add me at : x1d@hotmail.com
    add me please i need some help
    i wait u
  2. Photo

    OLH064 Icon

    28 Aug 2011 - 11:09
    That is the best signature ever.
  3. Photo

    Jeff H Icon

    19 Jun 2011 - 00:54
    Hey Bench,
    I have dual monitors and was looking over at the other one as I was trying to give you a + rep and accidently hit the - rep.
    Just let me know what I need to do to help get it changed. I already left a reply in the thread.
  4. Photo

    Jeff H Icon

    19 Jun 2011 - 00:54
    Hey Bench,
    I have dual monitors and was looking over at the other one as I was trying to give you a + rep and accidently hit the - rep.
    Just let me know what I need to do to help get it changed. I already left a reply in the thread.
  5. Photo

    sleepybug Icon

    20 Feb 2010 - 06:45
    hey bench!!thanks for the simple code concerning node,pointer and linked list ,you posted..now after learning pointers i understand your that program much more clearly and its simple working..thanks for help buddy.
  6. Photo

    neptunusmaris Icon

    16 Feb 2010 - 10:56
    Thanks
  7. Photo

    CodingDesire Icon

    16 Feb 2010 - 09:21
    Hello there, thank you very much for your help!
Page 1 of 1