Thread: Problems with cin and GetAsyncKeyState

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no loop shown in your code, so I expect the endless loop is elsewhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    No even in a simple code without any loops, this error will also happen.
    See Cprogramming.com : FAQ > How do I...(Level 2) > Why does my program enter an infinite loop if the user inputs invalid data? (C++).
    I just want to know what I should do to my code that you can see to prevent the user from entering invalid data.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KgNe View Post
    No even in a simple code without any loops, this error will also happen.
    See Cprogramming.com : FAQ > How do I...(Level 2) > Why does my program enter an infinite loop if the user inputs invalid data? (C++).
    I just want to know what I should do to my code that you can see to prevent the user from entering invalid data.
    Well, there are several approaches to "avoid user inputting invalid data" - the most common one is to read data with a fgets (in C) or getline (in C++). That way, the user can type in anything he/she likes, and the application will not misbehave. It is then up to the rest of the code to figure out if the input is valid/useful with suitable conversion functions to conver the data into the correct format - e.g. string to integer, string to date, string to limited length string [e.g. you input a line of up to 500 characters, but a "username" is only allowed to be 30 characters].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    But I used cin.get(person1.name,50,'\n') so why won't it work? If cin.get is significantly different from getline could you tell me how I could use getline to replace cin.get in the code i posted earlier?

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    getline and get behave the same way. You can only get the infinite loop of bad input you appear to be talking about if you try to read numeric input directly. You can't get an infinite loop on inputting a string or characters.

  6. #21
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    I'm using cin.get to get a string aren't I? So why does it still error?

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by KgNe View Post
    I'm using cin.get to get a string aren't I? So why does it still error?
    I assume the error is on the very next line after the bit you posted where it says cin >> person1.age. I will continue to believe that until you convince me otherwise. (In other words: I have been completely unable to make the snippet you posted behave in any violent manner.)

  8. #23
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Ok I fixed it. It occured to me that since I'm using a character array, it would error if the user didn't cin a character, which apparently was what a certain something in the buffer or something although I supposedly cleared the buffer, but once I used string and getline, it worked. Anyway does anyone know what is classing exactly?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KgNe View Post
    Anyway does anyone know what is classing exactly?
    Not sure. In what context does it occur?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Like I have to use it to make my code for this project look neater or something

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KgNe View Post
    Like I have to use it to make my code for this project look neater or something
    Is this in English, or are you translating from something else. I can't see the word on this page [other than in your post and my quote of your post]. Google does have some results for classing, but they appear to relate to wool or cotton, and I don't think producing good quality cloth is much related with good quality code [other than "lint" being loose fibers on cloth, and a tool that detect potential bugs in C code].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Hmm apparently it's just using classes in my program to handle functions. How should I go about doing that? Why is there a need to use classes to handle my functions?

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no application that NEEDS to be solved with classes. There are three reasons to use classes:
    1. The boss/teacher says so. [this is the most important one if you want to succeed in a job/course].
    2. The problem becomes easier to solve by using classes.
    3. Someone else says so. This is the poorest of all reasons.

    Almost all programs that exist today have been written in plain C - if you want to use C++, then you have the possibility to use classes, and they can often help make a problem easier to solve. But there is no "must" in this.

    Without you describing what the actual goal of the project is, it would be impossible for me to advice whether this is a project where classes are beneficial [of course, there are some people who would say that all programs are best written using classes, and when it comes to LARGE systems, object oriented techniques will definitely be beneficial - even if the language is not C++ - other than that, it becomes a question of belief and principles rather than "must/can't"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Ok this project is to make a simple text-based game, of course we have experienced people who are going crazy with the project, people doing way over their limits, people doing what they can, etc etc. So currently, what interests me about your post is the 2nd reason, and it would be nice if you could show me a very simple example on how classes can do that.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KgNe View Post
    Ok this project is to make a simple text-based game, of course we have experienced people who are going crazy with the project, people doing way over their limits, people doing what they can, etc etc. So currently, what interests me about your post is the 2nd reason, and it would be nice if you could show me a very simple example on how classes can do that.
    This is, I presume, a school project?

    As to how you classes help, that's an awfully large subject.

    One aspect may be that you want to have different objects that the player can carry around, and possibly use as weapons.

    So you may start with a basic object:
    Code:
    class baseobject
    {
    private: 
       string shortDesc;
       string longDesc;
    public:
       baseobject(const string &sdesc,  const string &ldesc);
       virtual void Describe(bool brief);
       virtual bool isWeapon() { return false; }
    };
    
    class weapon: public baseobject
    {
       weapon(const string &sdesc,  const string &ldesc, float damageValue);
       bool isWeapon() { return true; }
       void useWeaponOn(...);
    };
    This can be expanded quite a bit, but it shows the basic principle.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed