I've been wanting to make a very simple console based game in C++ and I was working on the map generation function but my for loops were not outputting the number of characters I wanted. Here is my code:
#include <iostream>
using namespace std;
int height = 40;
int width = 40;
void mapgeneration()
{
for (int i; i = width; i++)
{
cout << "-";
}
}
int main()
{
mapgeneration();
return 0;
}
You would think this would output:
----------------------------------------
but it outputs dashes infinitely for some reason I can't seem to put my finger on. Can anyone help?
Thank you so much for your reply! Your method did help. The reason my code may seem a bit sloppy is because I have only 2 months of C++ experience and I am 11 years old. But, thanks again for the help.
No problem, and don’t apologize; it is a common mistake, even for people plenty older than you. C and C++ are notorious for these kinds of syntax issues.
Going forward, try to find a reference or tutorial where you can see examples of using loops and functions and stuff, and try to stick with the syntactical conventions you see being used. Once you gain more experience about how the grammar is actually defined you can freely play with it more without getting surprised by odd behavior.
It is also worth your time to tell your IDE or compiler to complain about everything. Your compiler should have caught the uninitialized variable error for you and complained about it.