+ 1

Help troubleshooting code that prints a square

I am trying to make a program that outputs a square sized via user input. This is the code I have: --------------------------------------------- #include <iostream> #include <string> using namespace std; string square(int x, int y){ char unit = '#'; string row(x, unit); for(int i;i<=y;i++){ cout << row << "\n"; } } int main(){ int xx; int yy; cin >> xx; cin >> yy; cout << square(xx,yy); return 0; } --------------------------------------------- The output is something like: ------------------------ ### ├ÉÉÉÉÉ■    ╘    ■    ë◄∟vÉÉÉÉÉΦ=π  ├R¢ ------------------------ So obviously it's returning crap. I just don't know why. Thanks in advance for any help. Also, I'd like to try and keep the code as similar to what I already wrote as possible, i.e., not rewrite the code altogether, but fix the code.

27th Jan 2017, 8:00 PM
neon_cabbage
neon_cabbage - avatar
4 Antworten
+ 4
...pulled away, sorry. You need to initialize i because it could be set to anything. for(int i=1;i<=y;i++){ # here if you say you have a function return type of string you'd better return a string: return(""); # here } or when C++ pops the stack it will give you junk from crazyland (like not initializing i)...that is, from somewhere in memory. SoloLearn never completes...probably because you're corrupting either the stack or instruction pointers. I think I know the security ramifications better than I'm explaining the code issue so if you need me to rephrase (or flip from speculation to tracing) let me know.
27th Jan 2017, 9:12 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
You don't seem to have this in CodePlayground...? While I look at C++ behavior you might want to look at character encoding as a shot in the dark Wrong lang but what I mean: https://code.sololearn.com/c6NQFsmNHz82/?ref=app
27th Jan 2017, 6:26 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Here is how i fixed you problem: //You can remove the getch() and conio.h //The first problem was that you had a cout that couts, (cout << cout << endl;) type //But calling the function alone without a cout, still crashed, but without printing "├ÉÉÉÉÉ■ ╘ ■ ë◄∟vÉÉÉÉÉΦ=π ├R¢" //So i just removed the function and put it all in the main() //Hope this makes sense #include <iostream> #include <string> #include <conio.h> using namespace std; int main(){ int x; int y; cin >> x; cin >> y; char unit = '#'; string row(x, unit); for (int i = 0; i < y; i++){ cout << row << endl; } getch(); return 0; }
27th Jan 2017, 8:55 PM
Dawzy
Dawzy - avatar
0
@Kirk Schafer I'm not sure that's it. This seems to be something similar to another question I asked, where when I ran my code (to add up numbers in an array,) it would keep outputting a very large negative or positive number, when that obviously wasn't the sum. So I think I'm using data types wrong somehow and some way. I'm also suspicious about the "string row(x, unit)" part, like perhaps I did it wrong. I'll keep tinkering and looking, but if you find anything, let me know, please.
27th Jan 2017, 7:59 PM
neon_cabbage
neon_cabbage - avatar