Why doesn't my code work in visual studio?
up vote
-2
down vote
favorite
this is my code
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
bool a = false;
char b='p';
int c=0;
while (a != true) {
if (_kbhit()) {
b = _getch();
}
if (b=='w') {
c++;
cout << c << " ";
}
else if (b == 'c') {
cout << "hello";
}
}
system("pause");
return 0;
}
The problem is where when I press 'w' I want it to print out the value of c and it should be repeating until i press another input for _kbhit()
right? because now it add 1 to c then prints c and when i press w again samething. What's wrong with my visual studio I'm using community 2017 I've tried to uninstall it and install it again but same problem occurs.
c++ visual-studio
add a comment |
up vote
-2
down vote
favorite
this is my code
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
bool a = false;
char b='p';
int c=0;
while (a != true) {
if (_kbhit()) {
b = _getch();
}
if (b=='w') {
c++;
cout << c << " ";
}
else if (b == 'c') {
cout << "hello";
}
}
system("pause");
return 0;
}
The problem is where when I press 'w' I want it to print out the value of c and it should be repeating until i press another input for _kbhit()
right? because now it add 1 to c then prints c and when i press w again samething. What's wrong with my visual studio I'm using community 2017 I've tried to uninstall it and install it again but same problem occurs.
c++ visual-studio
The code you posted cannot possibly compile.
– Neil Butterworth
Nov 11 at 16:05
3
Once you get the program to build properly and without error or warnings, then I suggest that you learn how to debug your programs. Especially learn how to use the Visual Studio debugger to step through your code line by line.
– Some programmer dude
Nov 11 at 16:08
well i can compile it but i know im missing the <> on the conio.h and iostream but when i put them around the conio.h and iostream they just dissapeared
– SavannPojken
Nov 11 at 16:41
@SavannPojken "but when i put them around the conio.h and iostream they just dissapeared " Only if you miss to indent your code correctly by four blanks. Also note thatconio.h
isn't a standard c++ header.
– πάντα ῥεῖ
Nov 11 at 16:48
I think the poster has done a perfectly reasonable job of asking a question, to which it turns out the answer is not immediately obvious at all. It does not deserve the down-votes it's gotten.
– Jerry Coffin
Nov 11 at 17:33
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
this is my code
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
bool a = false;
char b='p';
int c=0;
while (a != true) {
if (_kbhit()) {
b = _getch();
}
if (b=='w') {
c++;
cout << c << " ";
}
else if (b == 'c') {
cout << "hello";
}
}
system("pause");
return 0;
}
The problem is where when I press 'w' I want it to print out the value of c and it should be repeating until i press another input for _kbhit()
right? because now it add 1 to c then prints c and when i press w again samething. What's wrong with my visual studio I'm using community 2017 I've tried to uninstall it and install it again but same problem occurs.
c++ visual-studio
this is my code
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
bool a = false;
char b='p';
int c=0;
while (a != true) {
if (_kbhit()) {
b = _getch();
}
if (b=='w') {
c++;
cout << c << " ";
}
else if (b == 'c') {
cout << "hello";
}
}
system("pause");
return 0;
}
The problem is where when I press 'w' I want it to print out the value of c and it should be repeating until i press another input for _kbhit()
right? because now it add 1 to c then prints c and when i press w again samething. What's wrong with my visual studio I'm using community 2017 I've tried to uninstall it and install it again but same problem occurs.
c++ visual-studio
c++ visual-studio
edited Nov 11 at 16:49
πάντα ῥεῖ
71.4k972134
71.4k972134
asked Nov 11 at 16:03
SavannPojken
61
61
The code you posted cannot possibly compile.
– Neil Butterworth
Nov 11 at 16:05
3
Once you get the program to build properly and without error or warnings, then I suggest that you learn how to debug your programs. Especially learn how to use the Visual Studio debugger to step through your code line by line.
– Some programmer dude
Nov 11 at 16:08
well i can compile it but i know im missing the <> on the conio.h and iostream but when i put them around the conio.h and iostream they just dissapeared
– SavannPojken
Nov 11 at 16:41
@SavannPojken "but when i put them around the conio.h and iostream they just dissapeared " Only if you miss to indent your code correctly by four blanks. Also note thatconio.h
isn't a standard c++ header.
– πάντα ῥεῖ
Nov 11 at 16:48
I think the poster has done a perfectly reasonable job of asking a question, to which it turns out the answer is not immediately obvious at all. It does not deserve the down-votes it's gotten.
– Jerry Coffin
Nov 11 at 17:33
add a comment |
The code you posted cannot possibly compile.
– Neil Butterworth
Nov 11 at 16:05
3
Once you get the program to build properly and without error or warnings, then I suggest that you learn how to debug your programs. Especially learn how to use the Visual Studio debugger to step through your code line by line.
– Some programmer dude
Nov 11 at 16:08
well i can compile it but i know im missing the <> on the conio.h and iostream but when i put them around the conio.h and iostream they just dissapeared
– SavannPojken
Nov 11 at 16:41
@SavannPojken "but when i put them around the conio.h and iostream they just dissapeared " Only if you miss to indent your code correctly by four blanks. Also note thatconio.h
isn't a standard c++ header.
– πάντα ῥεῖ
Nov 11 at 16:48
I think the poster has done a perfectly reasonable job of asking a question, to which it turns out the answer is not immediately obvious at all. It does not deserve the down-votes it's gotten.
– Jerry Coffin
Nov 11 at 17:33
The code you posted cannot possibly compile.
– Neil Butterworth
Nov 11 at 16:05
The code you posted cannot possibly compile.
– Neil Butterworth
Nov 11 at 16:05
3
3
Once you get the program to build properly and without error or warnings, then I suggest that you learn how to debug your programs. Especially learn how to use the Visual Studio debugger to step through your code line by line.
– Some programmer dude
Nov 11 at 16:08
Once you get the program to build properly and without error or warnings, then I suggest that you learn how to debug your programs. Especially learn how to use the Visual Studio debugger to step through your code line by line.
– Some programmer dude
Nov 11 at 16:08
well i can compile it but i know im missing the <> on the conio.h and iostream but when i put them around the conio.h and iostream they just dissapeared
– SavannPojken
Nov 11 at 16:41
well i can compile it but i know im missing the <> on the conio.h and iostream but when i put them around the conio.h and iostream they just dissapeared
– SavannPojken
Nov 11 at 16:41
@SavannPojken "but when i put them around the conio.h and iostream they just dissapeared " Only if you miss to indent your code correctly by four blanks. Also note that
conio.h
isn't a standard c++ header.– πάντα ῥεῖ
Nov 11 at 16:48
@SavannPojken "but when i put them around the conio.h and iostream they just dissapeared " Only if you miss to indent your code correctly by four blanks. Also note that
conio.h
isn't a standard c++ header.– πάντα ῥεῖ
Nov 11 at 16:48
I think the poster has done a perfectly reasonable job of asking a question, to which it turns out the answer is not immediately obvious at all. It does not deserve the down-votes it's gotten.
– Jerry Coffin
Nov 11 at 17:33
I think the poster has done a perfectly reasonable job of asking a question, to which it turns out the answer is not immediately obvious at all. It does not deserve the down-votes it's gotten.
– Jerry Coffin
Nov 11 at 17:33
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
It seems like the program behaves the way you want it to. If I press 'w', it goes into an infinite loop of increasing the value of c
and printing it. Pressing any other key stops printing and pressing 'c' goes on the same infinite loop and prints hello
. Doesn't seem to be any problems as far as the posted code is concerned. Also, I would like to say the same as @Someprogrammerdude, if it compiles, but doesn't behave the way you want it to, it's an issue with the code, not the IDE and/or compiler.
Hypothetical answer: Your computer might always be thinking that a key is pressed, thus kbhit()
always returns true. This maybe caused by a bad mouse/keyboard/controller driver and/or configuration. The code is fine, your PC is not.
how can i find out what's wrong with my pc lmao?
– SavannPojken
Nov 11 at 17:20
Inside the 'if(_kbhit) ` block try couting something. This way you can determine if my hypothesis is correct. If it is, try using keyboard hooks and see which ones are active for whatever reason. That' s my best guess anyway.
– Habitate
Nov 11 at 17:24
add a comment |
up vote
0
down vote
The problem you're running into seems to be a result of a recently added bug in _getch()
/_kbhit
.
For an extended key (e.g., a cursor key) it's documented that _getch()
returns either a 0x0 or 0xe0 followed by the scan code for the key that was actually pressed. What's not documented is that if the user presses a non-extended key, _kbhit
will still return true
twice in succession, and calls to _getch()
will return the key code the first time, and 0x0 the second time.
In your code, when the user presses 'w' or 'c', _kbhit
will return true
not just once (as you'd expect) but twice. The first time you call it, it'll return the scan code of the key, and the second it'll return a 0 byte.
What's happening in your code is that you're reading the scan code, printing something appropriately, then _kbhit
is returning true
again, so you read the '' byte, set b
to '', and then (since you don't have any code to do anything when b
is 0) you (repeatedly) do nothing until the next time the user presses a key.
Reference
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017
well the same problem ocurs when i set b to NULL or so i think it's something is missing in my visual studio because it works on my school computer but not on my home pc
– SavannPojken
Nov 11 at 19:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250554%2fwhy-doesnt-my-code-work-in-visual-studio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It seems like the program behaves the way you want it to. If I press 'w', it goes into an infinite loop of increasing the value of c
and printing it. Pressing any other key stops printing and pressing 'c' goes on the same infinite loop and prints hello
. Doesn't seem to be any problems as far as the posted code is concerned. Also, I would like to say the same as @Someprogrammerdude, if it compiles, but doesn't behave the way you want it to, it's an issue with the code, not the IDE and/or compiler.
Hypothetical answer: Your computer might always be thinking that a key is pressed, thus kbhit()
always returns true. This maybe caused by a bad mouse/keyboard/controller driver and/or configuration. The code is fine, your PC is not.
how can i find out what's wrong with my pc lmao?
– SavannPojken
Nov 11 at 17:20
Inside the 'if(_kbhit) ` block try couting something. This way you can determine if my hypothesis is correct. If it is, try using keyboard hooks and see which ones are active for whatever reason. That' s my best guess anyway.
– Habitate
Nov 11 at 17:24
add a comment |
up vote
0
down vote
It seems like the program behaves the way you want it to. If I press 'w', it goes into an infinite loop of increasing the value of c
and printing it. Pressing any other key stops printing and pressing 'c' goes on the same infinite loop and prints hello
. Doesn't seem to be any problems as far as the posted code is concerned. Also, I would like to say the same as @Someprogrammerdude, if it compiles, but doesn't behave the way you want it to, it's an issue with the code, not the IDE and/or compiler.
Hypothetical answer: Your computer might always be thinking that a key is pressed, thus kbhit()
always returns true. This maybe caused by a bad mouse/keyboard/controller driver and/or configuration. The code is fine, your PC is not.
how can i find out what's wrong with my pc lmao?
– SavannPojken
Nov 11 at 17:20
Inside the 'if(_kbhit) ` block try couting something. This way you can determine if my hypothesis is correct. If it is, try using keyboard hooks and see which ones are active for whatever reason. That' s my best guess anyway.
– Habitate
Nov 11 at 17:24
add a comment |
up vote
0
down vote
up vote
0
down vote
It seems like the program behaves the way you want it to. If I press 'w', it goes into an infinite loop of increasing the value of c
and printing it. Pressing any other key stops printing and pressing 'c' goes on the same infinite loop and prints hello
. Doesn't seem to be any problems as far as the posted code is concerned. Also, I would like to say the same as @Someprogrammerdude, if it compiles, but doesn't behave the way you want it to, it's an issue with the code, not the IDE and/or compiler.
Hypothetical answer: Your computer might always be thinking that a key is pressed, thus kbhit()
always returns true. This maybe caused by a bad mouse/keyboard/controller driver and/or configuration. The code is fine, your PC is not.
It seems like the program behaves the way you want it to. If I press 'w', it goes into an infinite loop of increasing the value of c
and printing it. Pressing any other key stops printing and pressing 'c' goes on the same infinite loop and prints hello
. Doesn't seem to be any problems as far as the posted code is concerned. Also, I would like to say the same as @Someprogrammerdude, if it compiles, but doesn't behave the way you want it to, it's an issue with the code, not the IDE and/or compiler.
Hypothetical answer: Your computer might always be thinking that a key is pressed, thus kbhit()
always returns true. This maybe caused by a bad mouse/keyboard/controller driver and/or configuration. The code is fine, your PC is not.
edited Nov 11 at 17:10
answered Nov 11 at 17:04
Habitate
115
115
how can i find out what's wrong with my pc lmao?
– SavannPojken
Nov 11 at 17:20
Inside the 'if(_kbhit) ` block try couting something. This way you can determine if my hypothesis is correct. If it is, try using keyboard hooks and see which ones are active for whatever reason. That' s my best guess anyway.
– Habitate
Nov 11 at 17:24
add a comment |
how can i find out what's wrong with my pc lmao?
– SavannPojken
Nov 11 at 17:20
Inside the 'if(_kbhit) ` block try couting something. This way you can determine if my hypothesis is correct. If it is, try using keyboard hooks and see which ones are active for whatever reason. That' s my best guess anyway.
– Habitate
Nov 11 at 17:24
how can i find out what's wrong with my pc lmao?
– SavannPojken
Nov 11 at 17:20
how can i find out what's wrong with my pc lmao?
– SavannPojken
Nov 11 at 17:20
Inside the 'if(_kbhit) ` block try couting something. This way you can determine if my hypothesis is correct. If it is, try using keyboard hooks and see which ones are active for whatever reason. That' s my best guess anyway.
– Habitate
Nov 11 at 17:24
Inside the 'if(_kbhit) ` block try couting something. This way you can determine if my hypothesis is correct. If it is, try using keyboard hooks and see which ones are active for whatever reason. That' s my best guess anyway.
– Habitate
Nov 11 at 17:24
add a comment |
up vote
0
down vote
The problem you're running into seems to be a result of a recently added bug in _getch()
/_kbhit
.
For an extended key (e.g., a cursor key) it's documented that _getch()
returns either a 0x0 or 0xe0 followed by the scan code for the key that was actually pressed. What's not documented is that if the user presses a non-extended key, _kbhit
will still return true
twice in succession, and calls to _getch()
will return the key code the first time, and 0x0 the second time.
In your code, when the user presses 'w' or 'c', _kbhit
will return true
not just once (as you'd expect) but twice. The first time you call it, it'll return the scan code of the key, and the second it'll return a 0 byte.
What's happening in your code is that you're reading the scan code, printing something appropriately, then _kbhit
is returning true
again, so you read the '' byte, set b
to '', and then (since you don't have any code to do anything when b
is 0) you (repeatedly) do nothing until the next time the user presses a key.
Reference
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017
well the same problem ocurs when i set b to NULL or so i think it's something is missing in my visual studio because it works on my school computer but not on my home pc
– SavannPojken
Nov 11 at 19:04
add a comment |
up vote
0
down vote
The problem you're running into seems to be a result of a recently added bug in _getch()
/_kbhit
.
For an extended key (e.g., a cursor key) it's documented that _getch()
returns either a 0x0 or 0xe0 followed by the scan code for the key that was actually pressed. What's not documented is that if the user presses a non-extended key, _kbhit
will still return true
twice in succession, and calls to _getch()
will return the key code the first time, and 0x0 the second time.
In your code, when the user presses 'w' or 'c', _kbhit
will return true
not just once (as you'd expect) but twice. The first time you call it, it'll return the scan code of the key, and the second it'll return a 0 byte.
What's happening in your code is that you're reading the scan code, printing something appropriately, then _kbhit
is returning true
again, so you read the '' byte, set b
to '', and then (since you don't have any code to do anything when b
is 0) you (repeatedly) do nothing until the next time the user presses a key.
Reference
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017
well the same problem ocurs when i set b to NULL or so i think it's something is missing in my visual studio because it works on my school computer but not on my home pc
– SavannPojken
Nov 11 at 19:04
add a comment |
up vote
0
down vote
up vote
0
down vote
The problem you're running into seems to be a result of a recently added bug in _getch()
/_kbhit
.
For an extended key (e.g., a cursor key) it's documented that _getch()
returns either a 0x0 or 0xe0 followed by the scan code for the key that was actually pressed. What's not documented is that if the user presses a non-extended key, _kbhit
will still return true
twice in succession, and calls to _getch()
will return the key code the first time, and 0x0 the second time.
In your code, when the user presses 'w' or 'c', _kbhit
will return true
not just once (as you'd expect) but twice. The first time you call it, it'll return the scan code of the key, and the second it'll return a 0 byte.
What's happening in your code is that you're reading the scan code, printing something appropriately, then _kbhit
is returning true
again, so you read the '' byte, set b
to '', and then (since you don't have any code to do anything when b
is 0) you (repeatedly) do nothing until the next time the user presses a key.
Reference
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017
The problem you're running into seems to be a result of a recently added bug in _getch()
/_kbhit
.
For an extended key (e.g., a cursor key) it's documented that _getch()
returns either a 0x0 or 0xe0 followed by the scan code for the key that was actually pressed. What's not documented is that if the user presses a non-extended key, _kbhit
will still return true
twice in succession, and calls to _getch()
will return the key code the first time, and 0x0 the second time.
In your code, when the user presses 'w' or 'c', _kbhit
will return true
not just once (as you'd expect) but twice. The first time you call it, it'll return the scan code of the key, and the second it'll return a 0 byte.
What's happening in your code is that you're reading the scan code, printing something appropriately, then _kbhit
is returning true
again, so you read the '' byte, set b
to '', and then (since you don't have any code to do anything when b
is 0) you (repeatedly) do nothing until the next time the user presses a key.
Reference
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017
edited Nov 11 at 17:34
answered Nov 11 at 17:28
Jerry Coffin
382k48462902
382k48462902
well the same problem ocurs when i set b to NULL or so i think it's something is missing in my visual studio because it works on my school computer but not on my home pc
– SavannPojken
Nov 11 at 19:04
add a comment |
well the same problem ocurs when i set b to NULL or so i think it's something is missing in my visual studio because it works on my school computer but not on my home pc
– SavannPojken
Nov 11 at 19:04
well the same problem ocurs when i set b to NULL or so i think it's something is missing in my visual studio because it works on my school computer but not on my home pc
– SavannPojken
Nov 11 at 19:04
well the same problem ocurs when i set b to NULL or so i think it's something is missing in my visual studio because it works on my school computer but not on my home pc
– SavannPojken
Nov 11 at 19:04
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250554%2fwhy-doesnt-my-code-work-in-visual-studio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The code you posted cannot possibly compile.
– Neil Butterworth
Nov 11 at 16:05
3
Once you get the program to build properly and without error or warnings, then I suggest that you learn how to debug your programs. Especially learn how to use the Visual Studio debugger to step through your code line by line.
– Some programmer dude
Nov 11 at 16:08
well i can compile it but i know im missing the <> on the conio.h and iostream but when i put them around the conio.h and iostream they just dissapeared
– SavannPojken
Nov 11 at 16:41
@SavannPojken "but when i put them around the conio.h and iostream they just dissapeared " Only if you miss to indent your code correctly by four blanks. Also note that
conio.h
isn't a standard c++ header.– πάντα ῥεῖ
Nov 11 at 16:48
I think the poster has done a perfectly reasonable job of asking a question, to which it turns out the answer is not immediately obvious at all. It does not deserve the down-votes it's gotten.
– Jerry Coffin
Nov 11 at 17:33