C++ 2Dimensional array - c++

I'm currently learning c++,and the code went so far,until
i made array variables to call a function instead of string,but something went wrong,and i can't figure out what it is.Problem is,it couts correctly only first 2 letters,then it couts the rest as └└└└ symbols.
Here's the code:
#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
using namespace std;
int land(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
char H = 72;
cout<<H;
}
int player(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
char X = 88;
cout<<X;
}
/*previously,i declared a string inside worldgen,and generated 2d array inside the for loop,but when i changed variables to call a function,first letters were X and H,but then it went └└└└└└└└└└└└└└└└└└└└└└└ for all the remaining characters.What's wrong here? */
int worldgen(int dimX,int dimY,int x,int y){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
system("TITLE MyTitleText");
int H = land();
int X = player();
string world[dimX][dimY];
for(int c = 0;c<dimY;c++){
for(int count = 0;count<dimX;count++){
world[count][c] = H;
world[x][y] = X;
cout<<world[count][c];
}
cout<<endl;
}
}
int main(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
worldgen(70,15,10,10);
cin.get();
}

Neither land() nor player() return anything so neither H nor X are getting any meaningful data are are just garbage data that was uninitialized.
I'm surprised this is even compiling since you have two functions that are supposed to return ints but aren't set to return anything.

Related

C++ no suitable conversion function from INPUT to LPINPUT exists

i'm trying to get the coordinates of the mouse, then move the mouse relative to those coordinates.
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
int main() {
SHORT f5;
POINT pt{};
MOUSEINPUT _mi{};
INPUT mm{};
mm.type = 0;
for (;;) {
f5 = GetAsyncKeyState(0x74);
if (f5 > 0) {
GetCursorPos(&pt);
_mi.dx = pt.x + 100;
_mi.dy = pt.y + 100;
mm.mi = _mi;
SendInput(1, mm, sizeof(mm));
}
Sleep(50);
}
return 0;
}
I'm getting an error saying that no suitable conversion from INPUT to LPINPUT exists, but I also have no idea how to create a "LPINPUT" struct.
SendInput() expects a pointer to an array of INPUT structures. Since you are passing only 1 INPUT, give it the address of your INPUT as a pointer by prefixing the structure with the & operator, just as you did with GetCursorPos():
SendInput(1, &mm, sizeof(mm));

exception thrown with random numbers in c++

I'm trying to make a game which requires pseudo random numbers to set the enemies to random spots. So I tried including the stdlib header file and used srand before getting random numbers for the enemies using rand, my code until the first srand looks like this:
#include "pch.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
int screenWidth = 70, screenHeight = 80;
class Enemy {
public:
int column, lives;
Enemy() {};
Enemy(int nColumn, int nLives) {
column = nColumn;
lives = nLives;
}
};
int main()
{
wchar_t *screen = new wchar_t[screenWidth * screenHeight];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD bytesWritten = 0;
int x = 0, y = 0, width = 10, height = 10;
std::wstring canvas = L"";
wchar_t *title = new wchar_t[8];
wsprintf(title, L"retro shooter");
SetConsoleTitle(title);
int enemiesLength = screenWidth / width;
Enemy* enemies = new Enemy[enemiesLength];
srand(time(NULL)); // It throws the error at this line
for (int i = 0; i < enemiesLength; i++) {
if (rand() % 2) enemies[i] = Enemy(i, 1);
}
// the code doesn't end here that's why I didn't put out the closing curly bracket
The code above gives me the error/exception:
Exception thrown at 0x76EDE496 (ntdll.dll) in retroShooter.exe: 0xC0000005: Access violation reading location 0x183A0FA2.
I have tried using vectors but the exception is the same.
I have also tried include-ing cstdlib like: #include <cstdlib> but the exception is the exact same.
This exception is thrown after it has been compiled and it isn't marked as an error in visual studio
This is robably the issue:
wchar_t *title = new wchar_t[8]; // 8 is a lot shorter
wsprintf(title, L"retro shooter"); // than 14

C++ How to remove vertical scrollbar?

I am trying to make a fun program where it display random numbers, but I need to remove the scrollbar so it looks more convincing. I managed to make the program full screen but I can't remove the vertical scrollbar. Screenshot
Code:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
There are many ways, one of them is manipulating the size of the internal buffer of the console to have the same size of the window and then using ShowScrollBar function to remove the scrolls.
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hstdout, &csbi);
csbi.dwSize.X = csbi.dwMaximumWindowSize.X;
csbi.dwSize.Y = csbi.dwMaximumWindowSize.Y;
SetConsoleScreenBufferSize(hstdout, csbi.dwSize);
HWND x = GetConsoleWindow();
ShowScrollBar(x, SB_BOTH, FALSE);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
Another way is to rely on conio.h or another C/C++ header/library which implements user interface functions.

C++ Background problems

I have a c++ program that generates random files filled with gibberish, but for it to work it needs to run in the background. The method I am using generates a null window. I have made other programs using this background method, but it doesn't work in this program:
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <fstream>
#include <windows.h>
using namespace std;
string random(int len)
{
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string r;
srand(time(NULL));
for(int i = 0; i < len; i++) r.push_back(a.at(size_t(rand() % 62)));
return r;
}
int main(){
restart:
/*This is the background code*/
HWND window;
AllocConsole();
window - FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(window, 0);
std::string file=random(1);
std::ofstream o(file.c_str());
o << random(999) << std::endl;
goto restart;
return 0;
}
I am using the dev C++ compiler
I just realized my problem, the goto statement needed to not include the null window rendering part, so the window wasn't re-rendered and de-rendered every time. Also there was a - that needed to be an =.

dispose is not a member of GDI+ Image class

So I have a program that pulls random images from a folder and creates a collage out of them, and set it to the windows wallpaper. Which seems to work fine. So I thought I would put in a sleep timer and let it automatically update itself without me having to run it every half hour or what ever. I did that and it works great, but I ran into a problem of a memory leak that wasn't noticed before I started looping it. I am attempting to dispose of the GDI+objects, but I keep getting the error that dispose is not a member of GDIplus::Image
I am loading an picture into an Image object, then resizing it and putting it into an array of Images, then I would like to dispose of the first Image. I would then like to dispose of the array after I finish working with the images in it.
This is being done with an old copy of VS2005.
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <string>
#include <iostream>
#include <vector>
#include <dirent.h>
#include <time.h>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "cwp05rnd.h"
using namespace Gdiplus;
using namespace std;
#pragma comment (lib,"Gdiplus.lib")
#pragma comment (lib, "user32.lib")
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
HDC hdc;
Graphics graphics(hdc);
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID jpegClsid;
GetEncoderClsid(L"image/jpeg", &jpegClsid);
SetCurrentDirectoryA("E:\\Photos");
ofstream outfile;
outfile.open ("outimgs.txt");
ofstream outfile2;
outfile2.open("imgpos.txt");
srand(time(NULL));
init_genrand(time(NULL));
vector<string> dirlist;
DIR *d;
struct dirent *dir;
int i=0;
d=opendir(".");
if (d)
{
while ((dir=readdir(d)) != NULL)
{
i++;
dirlist.push_back(dir->d_name);
}
closedir(d);
}
Image wp(L"E:\\Dropbox\\Photos\\wallpaper.jpg");
Graphics* wpimage = Graphics::FromImage(&wp);
int r;
int rvsize=100;
int rv[100]={0};
string img;
std::wstring wimg;
const wchar_t* rimg;
double cwidth;
double cheight;
double ratio;
int nheight;
int counter=0;
int full = 0;
int tries = 0;
int hfull = 0;
int imgnum =0;
int last=0;
Image* newpic[10];
while ( tries <10)
{
redo:
tries++;
int newrv=0;
while (newrv ==0)
{
r=genrand_int32()%i;
for (int k=0; k < rvsize; k++)
{
if (rv[k] > 0 && rv[k]==r )
{
break;
}
if (rv[k]==0 && r < i)
{
newrv =1;
rv[k]=r;
last=k;
break;
}
if (rv[k] ==0)
{
break;
}
}
}
img = dirlist[r];
if (img[0]=='.')
{
newrv=0;
goto redo;
}
wimg = std::wstring(img.begin(),img.end());
rimg = wimg.c_str();
Image pic(rimg);
cwidth = pic.GetWidth();
cheight = pic.GetHeight();
if (cheight ==0)
{
outfile2 << "error" << img << endl;
rv[last]=0;
system("pause");
goto redo;
}
ratio = cwidth/cheight;
nheight = nwidth/ratio;
pic.RotateFlip(Rotate180FlipNone);
pic.RotateFlip(Rotate180FlipNone);
newpic[imgnum] = pic.GetThumbnailImage(nwidth,nheight,NULL,NULL);
delete pic[0];
imgnum = imgnum + 1;
}
then there is a long section of flips and rotates on the images in newpic, according to various random values.
wpimage->DrawImage(newpic[k],(j*nwidth),(((k+1)*whitespace)+htot),nwidth,nh[k]);
wp.Save(L"C:\\Temp\\wallpaper\\nwallpaper.jpg", &jpegClsid, NULL);
delete newpic;
setWall();
delete wpimage;
delete wp;
return 0;
}
When I attempt to delete the Image objects, I get an error that says either it can not delete objects that are not pointers, or it cannot convert from GDIplus::Image to void*
Any advice would be appreciated.
I noticed you have Image pic(rimg);
But you are doing delete pic[0];
pic is not a pointer.. not dynamically allocated or something... nor is a array (or maybe it is.. but intuitively I think no..)
* Add *
Oh yeah, if you already solved this, suggests you close the question or at least mention it...