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

Reputation: 854 Master
Group:
Expert
Active Posts:
2,338 (0.96 per day)
Joined:
20-August 07
Profile Views:
19,833
Last Active:
User is offline Nov 06 2013 12:48 PM
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: 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.
  2. 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
  3. In Topic: Cannot go to another level in RPG game

    Posted 4 Aug 2013

    At the top of your file (in the global scope... ) You have the following code:
    int Gamespeed = 100;
    int Level = 1;
    bool stopgame = false;
    
    int main()
    {
    


    Then later on..

    	while(stopgame == false && Level == 2)
    	{
    		system("cls");
    		cout << "This is Level 2..." << endl;
    		cout << endl;
    
    
    		int Gamespeed2 = 100;
    		int Level = 2;
    


    You are re-declaring your variable called level within your second while loop. - You have two different variables (areas of memory) in your program, although the name of the second level causes the first level to be hidden.

    Which means that when you reach this line of code, the value of Level is 1
    	while(stopgame == false && Level == 3)
    



    Just a few ideas to think about:
    1) You should spend some time learning how to use your debugger - particularly so that you can "break" the execution of your program and inspect the values of particular variables; it will let you see what's really going on inside your program, and you can check whether your assumptions are right or not

    2) You've got a huge amount of repetition in your program - You could cut down the complexity (and therefore make it easier to see what's happening) if you put all of your repeated code in functions. Functions allow you to reuse the same basic program logic using different values (Values such as arrays and array sizes...)

    3) Try to avoid using "global" variables (variables declared outside of a function, such as those before your int main). When you start learning how to use functions, and your programs get more complicated, global variables can get really messy, and make your life harder when trying to find out what's really going on!

    JackOfAllTrades has written a couple of really useful guides which are worth reading:
    http://www.dreaminco...t-i-the-basics/
    http://www.dreaminco...ter-and-arrays/
  4. In Topic: Exam: concepts, need some clarification

    Posted 21 Oct 2012

    View PostFrozenSnake, on 20 October 2012 - 11:39 PM, said:

    Data type: A data type tells us what values a variable can contain. For example, an int can only contain integers; if the programmer would enter a number with decimals the decimals would be truncated. For example would 1.1 be 1.
    A data type also provides you:
    - Information about the sizeof (in bytes) for an an object of that data type in memory
    - An interface to objects of that type (operators form part of an object's interface as much as constructors, member functions, etc)


    View PostFrozenSnake, on 20 October 2012 - 11:39 PM, said:

    Function: A snippet of code that is used to solve the same problem several times in different parts in the program without having to re-solve it.
    However re-use of a function isn't necessary. Sometimes functions are simply used to break larger chunks of code down into smaller ones. A shorter definition of 'function' would be a named block of code



    View PostFrozenSnake, on 20 October 2012 - 11:39 PM, said:

    Polymorphism: Polymorphism is when we use a function without knowing which class it belongs too. Example on this is in the Invaders game we made, for example when we are doing collision detection. We fetch i:s x- and y-coordinates and j:s x- and y-coordinates. But we do not know if x is a ship, bullet or enemy and the same applies for j. This is handled by the program. These functions doesn’t have to behave the same as the mother class function, it has to have the same “head” but the body can be different.
    C++ has two types of polymorphism. The type you're describing here is known as Dynamic polymorphism (at runtime). a.k.a "late binding" or "dynamic binding"

    C++ additionally includes the concept of static polymorphism (polymorphism at compile time); which is more commonly used under the umbrella of Template Metaprogramming.

    Both static and dynamic polymorphism follow the same basic concept - manipulating different objects in code through a common interface without needing specific data type information about those objects


    View PostFrozenSnake, on 20 October 2012 - 11:39 PM, said:

    Capsulation: capsulation can happen on different levels and be used to hide data. Depending on level we can do different things with functions and variables.
    Do you mean Encapsulation? Note that 'implementation hiding' applies equally to the behaviour of an object (i.e. the details which describe how it works); data hiding is only one part of this.

    For example, a badly-encapsulated class called DeckOfCards might include member functions called GetNumberOfCards() and SetNumberOfCards(). Both of these are probably bad because they "leak" the internal behaviour of the object, expecting the user of the class to do the leg-work. a class modelling a deck of cards is more likely to want member functions such as DrawCardFromTop, ReplaceCardToBottom, ShuffleDeck, IsDeckEmpty etc.


    View PostFrozenSnake, on 20 October 2012 - 11:39 PM, said:

    Inheritance: Inheritance makes it possible for subclasses to inherit functions and variables from one or more classes. The subclasses will inherit both functions and variables.
    The act of inheriting a class could also be described as defining a new class whose interface and implementation are an extension of an existing class. Another word would be Specialisation.
  5. In Topic: abstract base class and shared_ptr

    Posted 29 Sep 2012

    The semantics of shared_ptr are deliberately designed to mirror raw pointers as closely as possible; similarly, the semantics of make_shared are deliberately designed to mirror the new operator as closely as possible. The idea being that you should be able to use them as direct drop-in-replacements for legacy code with minimal change to the code. (Think of them as abstractions of pointers and new in the same way that iterators are designed as abstract pointers)

    So, a shared_ptr<Derived> has semantically the same relationship to shared_ptr<Base> as you would expect between 'raw' pointers Derived* and Base*.

    In the dark days before shared_ptr, you'd be forced to do code like this:
    Base* b = new Derived; 
    

    With shared_ptr, you would typically expect to see the replacement code look like:
    std::shared_ptr<Base> b = std::shared_ptr<Derived>(new Derived); 
    

    make_shared is not actually required - so it's "OK" to pass a parameter using 'new' if you wish, but the code looks clunky. make_shared helps everything look cleaner. (Notably, it reduces repetition of the name Derived)
    std::shared_ptr<Base> b = std::make_shared<Derived>(); 
    

    or, of course, even cleaner:
    auto b = std::make_shared<Derived>(); 
    


    Where make_shared<Derived() is an abstraction of new Derived();, the same is true for make_shared<T>( arg1, arg2) - it is semantically equivalent to new T( arg1, arg2 );.

    The implementation of make_shared is achieved using variadic template arguments which are forwarded to new.

My Information

Member Title:
D.I.C Lover
Age:
31 years old
Birthday:
August 15, 1982
Gender:
Location:
UK
Years Programming:
10
Programming Languages:
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