Fixed up my code a little bit.

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »

86 Replies - 2231 Views - Last Post: 26 February 2018 - 08:26 AM Rate Topic: -----

#31 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: Fixed up my code a little bit.

Posted 28 January 2018 - 06:53 PM

A lot of things confusion our OP is encountering with C++ I/O would likely not b present if he had gone through the natural progression of learning through a well structured modern C++ book. Alas, he is learning C++ in a scatter shot pattern.

In my mind, the only way to supplement those gaps in learning is by reading the documentation. Not a very efficient way to learn the language.
Was This Post Helpful? 0
  • +
  • -

#32 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 222
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 01 February 2018 - 02:15 PM

You might be right, probably gonna go and pick and new book, if it really is this big of a deal. :) But for the time being can you tell me how to prevent my problem?
Was This Post Helpful? 0
  • +
  • -

#33 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Fixed up my code a little bit.

Posted 01 February 2018 - 02:58 PM

Quote

But for the time being can you tell me how to prevent my problem?

Since the topic got a little off topic I recommend you restate the problem and post the smallest possible complete program that illustrates the problem.


Jim
Was This Post Helpful? 0
  • +
  • -

#34 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: Fixed up my code a little bit.

Posted 02 February 2018 - 11:31 AM

I believe that the problem he is referring to is from post #24, #27, and #29

View PostXrey274, on 28 January 2018 - 04:38 PM, said:

Also one question if i have cin.get() and I type something it will get transferd to the cin>>variable_name after it. To be more specific I am talking about the beggining of the game. Why does this happen and how can I prevent it?

View PostXrey274, on 28 January 2018 - 06:03 PM, said:

Just realised that it still doesn't work. When you are at cin.get() type something that is longer than a single character and you'll see.

View PostXrey274, on 28 January 2018 - 08:20 PM, said:

what happens for me is that cin.get() retrieves the first character then cin>> is skipped and inside the string is placed the thing i typed on cin.get() without the first character.

Example:

At cin.get() I type "smart"

after cin.get() I have cin>>word;
it skips the cin>>word; and just displays the output
cout<<"The word you typed is: "<<word; - The word you typed is mart


To prevent that problem from happening, don't do that. Just use the formatted extractor operator >> to get the entire word, instead of what you are currently doing of calling get() followed by >>.

The reason why this is happening is (in a simplified way) is that the cin object is wrapper around the C API stdin input stream. The way reading from stdin works is that if there are characters are available, it is returned right away. If there are no characters available, then the user is prompted for input until the user presses the Enter key. Then all the input entered goes into the stream. get() will get ONE character from the input stream. The formatted extractors will get characters from the input stream that satisfy parsing into the extracted type. For most types, leading whitespace is skipped. For strings that means that it will stop getting characters when whitespace is hit.

So applying that to your usage pattern, here's the timeline of your code and the input buffer on Windows:
Code:                    Input buffer
enter cin.get();         <empty>
                         smart<CR><LF>
leave cin.get();         mart<CR><LF>
enter cin >> word;       mart<CR><LF>
leave cin >> word;       <CR><LF>


On *nix, pressing the Enter key will only put in the <LF>.
Was This Post Helpful? 0
  • +
  • -

#35 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 222
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 02 February 2018 - 11:57 AM

IS the C++ Programming Language a good C++ book?

Just found out about cin.sync() which clears the input stream. Is it ok for me to use it?
Was This Post Helpful? 0
  • +
  • -

#36 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Fixed up my code a little bit.

Posted 02 February 2018 - 11:57 AM

Quote

I believe that the problem he is referring to is from post #24, #27, and #29

Possibly correct, but IMO those posts are rather vague which is why I recommended restating the problem and providing the smallest possible complete program that illustrates the problem and ask specific questions based on that code.

Jim
Was This Post Helpful? 0
  • +
  • -

#37 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: Fixed up my code a little bit.

Posted 02 February 2018 - 02:41 PM

View PostXrey274, on 02 February 2018 - 01:57 PM, said:

Just found out about cin.sync() which clears the input stream. Is it ok for me to use it?

Read the documentation more closely. The behavior is implementation dependent. So one library may clear the input, while another library may do nothing.

While reading the documentation, you'll find that there is a call to discard the rest of the input.

The bigger question is: Why do you need to call cin.get()? What behavior are you trying to implement?
Was This Post Helpful? 0
  • +
  • -

#38 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 222
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 03 February 2018 - 05:23 AM

When they press a button they screen is cleared and the menu which request you to enter your nickname, that's why I use cin.get().
Was This Post Helpful? 0
  • +
  • -

#39 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: Fixed up my code a little bit.

Posted 03 February 2018 - 09:59 AM

You will have to use platform specific API call to get the "press any key" effect. With you current code notice that the user did press a key and nothing happened until the user pressed the Enter key.

A slightly less platform specific way is to use a library that abstracts away the platform specifics, but at your current level of experience, and with your -- too put it bluntly -- hard headedness, it will be a frustrating experience for both you, and us trying to advise you.
Was This Post Helpful? 0
  • +
  • -

#40 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 222
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 05 February 2018 - 05:24 PM

Can i ask you what does this do? - temp[p] = '/', temp is a string, not an array of strings and p is an integer.
Was This Post Helpful? 0
  • +
  • -

#41 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: Fixed up my code a little bit.

Posted 05 February 2018 - 05:29 PM

Post the complete code. Again realize that we are not sitting there beside you. There is not enough context in the question you posted.
Was This Post Helpful? 1
  • +
  • -

#42 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3782
  • View blog
  • Posts: 13,709
  • Joined: 08-August 08

Re: Fixed up my code a little bit.

Posted 05 February 2018 - 05:44 PM

View PostXrey274, on 05 February 2018 - 07:24 PM, said:

Can i ask you what does this do? - temp[p] = '/'

'/' is a character and p is an index into the array of characters that make up the string temp. Have you tried this? What do you think it should do?
Was This Post Helpful? 1
  • +
  • -

#43 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 222
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 06 February 2018 - 01:44 PM

Thanks for your advice! Thanks to you I learned a lot of new things and was able to optimize my code! Also I just want to mention that I also replaced the repetitive code in the hadouken function as well. Now it's a lot more like the heart function. Thanks!
Was This Post Helpful? 0
  • +
  • -

#44 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 222
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 09 February 2018 - 02:55 PM

I know this a dumb question, but I can't remember. Can I call a function that is inside main.cpp from a class?
Was This Post Helpful? 0
  • +
  • -

#45 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Fixed up my code a little bit.

Posted 09 February 2018 - 03:02 PM

Probably, did you try?

Jim
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »