ncurses printw bug when embedded in double for loop, c++
I am making a simple game in c++ that outputs to the console. Im using printw for this with ncurses. To print the grid, I have a for loop as follows:
for (int j; j < height + 2; j ++){
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
}
The output of this is just one line of #'s. At first I thought maybe it had something to do with the variable height, so I replaced it with its value, 22. Same, failed result. Then I physically typed out two for loops like this:
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
for (int i; i<width+2; i++){
printw("#");
}
and I got two lines of #'s! Really weird bug. I also tried while loops, but I had the same failed result. I'm new to c++, I come from python, so I could be missing something super obvious here. Thanks in advance!
c++ ncurses
add a comment |
I am making a simple game in c++ that outputs to the console. Im using printw for this with ncurses. To print the grid, I have a for loop as follows:
for (int j; j < height + 2; j ++){
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
}
The output of this is just one line of #'s. At first I thought maybe it had something to do with the variable height, so I replaced it with its value, 22. Same, failed result. Then I physically typed out two for loops like this:
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
for (int i; i<width+2; i++){
printw("#");
}
and I got two lines of #'s! Really weird bug. I also tried while loops, but I had the same failed result. I'm new to c++, I come from python, so I could be missing something super obvious here. Thanks in advance!
c++ ncurses
On Stack Overflow, we generally don't edit "SOLVED" into questions; accepting answers automatically marks it as "solved" by changing the colour showed in question listings etc. It's fine -- that's not written in the tour -- but I'm just letting you know.
– wizzwizz4
Nov 13 '18 at 17:03
thanks. i didnt know that.
– Deejpake
Nov 13 '18 at 21:55
add a comment |
I am making a simple game in c++ that outputs to the console. Im using printw for this with ncurses. To print the grid, I have a for loop as follows:
for (int j; j < height + 2; j ++){
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
}
The output of this is just one line of #'s. At first I thought maybe it had something to do with the variable height, so I replaced it with its value, 22. Same, failed result. Then I physically typed out two for loops like this:
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
for (int i; i<width+2; i++){
printw("#");
}
and I got two lines of #'s! Really weird bug. I also tried while loops, but I had the same failed result. I'm new to c++, I come from python, so I could be missing something super obvious here. Thanks in advance!
c++ ncurses
I am making a simple game in c++ that outputs to the console. Im using printw for this with ncurses. To print the grid, I have a for loop as follows:
for (int j; j < height + 2; j ++){
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
}
The output of this is just one line of #'s. At first I thought maybe it had something to do with the variable height, so I replaced it with its value, 22. Same, failed result. Then I physically typed out two for loops like this:
for (int i; i<width+2; i++){
printw("#");
}
printw("n");
for (int i; i<width+2; i++){
printw("#");
}
and I got two lines of #'s! Really weird bug. I also tried while loops, but I had the same failed result. I'm new to c++, I come from python, so I could be missing something super obvious here. Thanks in advance!
c++ ncurses
c++ ncurses
edited Nov 13 '18 at 17:03
wizzwizz4
3,44011635
3,44011635
asked Aug 16 '18 at 6:00
DeejpakeDeejpake
1157
1157
On Stack Overflow, we generally don't edit "SOLVED" into questions; accepting answers automatically marks it as "solved" by changing the colour showed in question listings etc. It's fine -- that's not written in the tour -- but I'm just letting you know.
– wizzwizz4
Nov 13 '18 at 17:03
thanks. i didnt know that.
– Deejpake
Nov 13 '18 at 21:55
add a comment |
On Stack Overflow, we generally don't edit "SOLVED" into questions; accepting answers automatically marks it as "solved" by changing the colour showed in question listings etc. It's fine -- that's not written in the tour -- but I'm just letting you know.
– wizzwizz4
Nov 13 '18 at 17:03
thanks. i didnt know that.
– Deejpake
Nov 13 '18 at 21:55
On Stack Overflow, we generally don't edit "SOLVED" into questions; accepting answers automatically marks it as "solved" by changing the colour showed in question listings etc. It's fine -- that's not written in the tour -- but I'm just letting you know.
– wizzwizz4
Nov 13 '18 at 17:03
On Stack Overflow, we generally don't edit "SOLVED" into questions; accepting answers automatically marks it as "solved" by changing the colour showed in question listings etc. It's fine -- that's not written in the tour -- but I'm just letting you know.
– wizzwizz4
Nov 13 '18 at 17:03
thanks. i didnt know that.
– Deejpake
Nov 13 '18 at 21:55
thanks. i didnt know that.
– Deejpake
Nov 13 '18 at 21:55
add a comment |
2 Answers
2
active
oldest
votes
You're not initialising your variables to 0
, so they could be anything! Rewrite it like this:
for (int j = 0; j < height + 2; j ++){
for (int i = 0; i<width+2; i++){
printw("#");
}
printw("n");
}
Thanks! That worked!
– Deejpake
Aug 16 '18 at 16:16
add a comment |
In Python, a for
loop is used for iterating over a sequence (that is either a list, a tuple or a string). This is less like the for
keyword in other programming language, and works more like an iterator
method as found in other object-orientated programming languages.
The traditional for
loop in C++ is different from Python's. (There is a version of for
which is called range-based for
introduced in C++11 which is similar to Python's.)
In C++, you have to initialize the variables declared in the for
loop before using them, otherwise the behaviour would be unexpected.
Thank you! Was just a syntax error after all.
– Deejpake
Aug 16 '18 at 16:16
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',
autoActivateHeartbeat: false,
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%2f51870490%2fncurses-printw-bug-when-embedded-in-double-for-loop-c%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
You're not initialising your variables to 0
, so they could be anything! Rewrite it like this:
for (int j = 0; j < height + 2; j ++){
for (int i = 0; i<width+2; i++){
printw("#");
}
printw("n");
}
Thanks! That worked!
– Deejpake
Aug 16 '18 at 16:16
add a comment |
You're not initialising your variables to 0
, so they could be anything! Rewrite it like this:
for (int j = 0; j < height + 2; j ++){
for (int i = 0; i<width+2; i++){
printw("#");
}
printw("n");
}
Thanks! That worked!
– Deejpake
Aug 16 '18 at 16:16
add a comment |
You're not initialising your variables to 0
, so they could be anything! Rewrite it like this:
for (int j = 0; j < height + 2; j ++){
for (int i = 0; i<width+2; i++){
printw("#");
}
printw("n");
}
You're not initialising your variables to 0
, so they could be anything! Rewrite it like this:
for (int j = 0; j < height + 2; j ++){
for (int i = 0; i<width+2; i++){
printw("#");
}
printw("n");
}
answered Aug 16 '18 at 6:05
wizzwizz4wizzwizz4
3,44011635
3,44011635
Thanks! That worked!
– Deejpake
Aug 16 '18 at 16:16
add a comment |
Thanks! That worked!
– Deejpake
Aug 16 '18 at 16:16
Thanks! That worked!
– Deejpake
Aug 16 '18 at 16:16
Thanks! That worked!
– Deejpake
Aug 16 '18 at 16:16
add a comment |
In Python, a for
loop is used for iterating over a sequence (that is either a list, a tuple or a string). This is less like the for
keyword in other programming language, and works more like an iterator
method as found in other object-orientated programming languages.
The traditional for
loop in C++ is different from Python's. (There is a version of for
which is called range-based for
introduced in C++11 which is similar to Python's.)
In C++, you have to initialize the variables declared in the for
loop before using them, otherwise the behaviour would be unexpected.
Thank you! Was just a syntax error after all.
– Deejpake
Aug 16 '18 at 16:16
add a comment |
In Python, a for
loop is used for iterating over a sequence (that is either a list, a tuple or a string). This is less like the for
keyword in other programming language, and works more like an iterator
method as found in other object-orientated programming languages.
The traditional for
loop in C++ is different from Python's. (There is a version of for
which is called range-based for
introduced in C++11 which is similar to Python's.)
In C++, you have to initialize the variables declared in the for
loop before using them, otherwise the behaviour would be unexpected.
Thank you! Was just a syntax error after all.
– Deejpake
Aug 16 '18 at 16:16
add a comment |
In Python, a for
loop is used for iterating over a sequence (that is either a list, a tuple or a string). This is less like the for
keyword in other programming language, and works more like an iterator
method as found in other object-orientated programming languages.
The traditional for
loop in C++ is different from Python's. (There is a version of for
which is called range-based for
introduced in C++11 which is similar to Python's.)
In C++, you have to initialize the variables declared in the for
loop before using them, otherwise the behaviour would be unexpected.
In Python, a for
loop is used for iterating over a sequence (that is either a list, a tuple or a string). This is less like the for
keyword in other programming language, and works more like an iterator
method as found in other object-orientated programming languages.
The traditional for
loop in C++ is different from Python's. (There is a version of for
which is called range-based for
introduced in C++11 which is similar to Python's.)
In C++, you have to initialize the variables declared in the for
loop before using them, otherwise the behaviour would be unexpected.
answered Aug 16 '18 at 6:48
P.WP.W
12.6k3844
12.6k3844
Thank you! Was just a syntax error after all.
– Deejpake
Aug 16 '18 at 16:16
add a comment |
Thank you! Was just a syntax error after all.
– Deejpake
Aug 16 '18 at 16:16
Thank you! Was just a syntax error after all.
– Deejpake
Aug 16 '18 at 16:16
Thank you! Was just a syntax error after all.
– Deejpake
Aug 16 '18 at 16:16
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.
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%2f51870490%2fncurses-printw-bug-when-embedded-in-double-for-loop-c%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
On Stack Overflow, we generally don't edit "SOLVED" into questions; accepting answers automatically marks it as "solved" by changing the colour showed in question listings etc. It's fine -- that's not written in the tour -- but I'm just letting you know.
– wizzwizz4
Nov 13 '18 at 17:03
thanks. i didnt know that.
– Deejpake
Nov 13 '18 at 21:55