ncurses printw bug when embedded in double for loop, c++












-2















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!










share|improve this question

























  • 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
















-2















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!










share|improve this question

























  • 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














-2












-2








-2








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:03









wizzwizz4

3,43011535




3,43011535










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



















  • 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












2 Answers
2






active

oldest

votes


















1














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");
}





share|improve this answer
























  • Thanks! That worked!

    – Deejpake
    Aug 16 '18 at 16:16



















1














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.






share|improve this answer
























  • Thank you! Was just a syntax error after all.

    – Deejpake
    Aug 16 '18 at 16:16











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
});


}
});














draft saved

draft discarded


















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









1














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");
}





share|improve this answer
























  • Thanks! That worked!

    – Deejpake
    Aug 16 '18 at 16:16
















1














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");
}





share|improve this answer
























  • Thanks! That worked!

    – Deejpake
    Aug 16 '18 at 16:16














1












1








1







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");
}





share|improve this answer













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");
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 16 '18 at 6:05









wizzwizz4wizzwizz4

3,43011535




3,43011535













  • 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





Thanks! That worked!

– Deejpake
Aug 16 '18 at 16:16













1














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.






share|improve this answer
























  • Thank you! Was just a syntax error after all.

    – Deejpake
    Aug 16 '18 at 16:16
















1














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.






share|improve this answer
























  • Thank you! Was just a syntax error after all.

    – Deejpake
    Aug 16 '18 at 16:16














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 16 '18 at 6:48









P.WP.W

12.2k3843




12.2k3843













  • 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





Thank you! Was just a syntax error after all.

– Deejpake
Aug 16 '18 at 16:16


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues