SDL ignores std::cin - c++

I started learing SDL using visual studio 2015, and I am following a video tutorial.
I already have everything setup.
This tutorial is all about drawing a blank window. After drawing the window the author adds a std::cin to take in an integer so that the window does not quit immediately.
The problem is that I have done the same exact thing but my window quits instantly.
This is my main.cpp:
#include"SDL.h"
#include<iostream>
#include"MainGame.h"
int main(int argc, char *args[]) {
using namespace std;
MainGame maingame;
maingame.run();
cout<<"Enter any key: "
int a;
cin>>a;
return 0;
}
This same thing works in the video. What am I doing wrong here?

It seems like visual studio isn't told to pay attention to the console, so it just throws away std::cin.
Try replacing it with:
SDL_Delay(1000); // do nothing for 1 second

Related

How to disable input method in C++ console?

I'm making a text-based console game.
The Chinese input method is annoying as it blocks normal key input.
I tried to send NULL to ImmAssociateContext but it doesn't work.
#include<Windows.h>
#include <imm.h>
#include <atlstr.h>
#include<handleapi.h>
#include<iostream>
#pragma comment ( lib,"imm32.lib" )
using namespace std;
int main(int argc, char* argv[])
{
SetConsoleTitle(TEXT("test"));
char ch;
ImmAssociateContext(FindWindow(NULL, TEXT("test")), NULL);
cin >> ch;
cout << ch;
return 0;
}
Edit:
I don't need to input Chinese in this game.
About Chinese input:
When you input a letter in console, Chinese input methods would receive the input as pinyin, and turn it into Chinese characters.
(In the screenshot, aabbcc with dashline below is pinyin)
The same thing happened to Japanese input method.
This is not what I need. All I want is, when I press A, the console receives A.
About the code:
I'm using PDCurses in my project to draw a text-based gui and get key inputs.
Everything looks fine when the input method is turned off.
The code above shows ImmAssociateContext (google says it can turn off input method) doesn't work to me.

OpenCV imread could not open or find image

I'm doing a project with OpenCV. I am trying to run a simple code:
#include<opencv2/core.hpp>
#include<opencv2/videoio.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
cv::Mat img2 = cv::imread("test.jpg", 1);
if (!img2.data) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
cv::waitKey(5000);
return -1;
}
else {
cout << "Working" << endl;
cv::waitKey(5000);
}
return 0;
}
My project is a lot more bigger then this, however something simple like this isn't working for me. I have tried many things such as full path names // and \\, even tried an IplImage and convert it into Mat still no luck.
I have tried many different file types as well. If it helps I have coded it to stop the application after hitting any button, that does not work as it isn't taking any input from my keyboard.
How can I determine what is wrong?
I had this exact issue including the part about it only working in release mode. I found out I was using the release libraries for both release and debug. Because I had followed the tutorial on the OpenCV website for visual studio (incorrectly albeit), I changed the .lib folder to the correct d.lib and that fixed it.

SDL not printing to console

I'm trying to use SDL with Visual Studio 2019 but my programs are only showing an empty console. At the moment I just want to be able to compile my program with the SDL libraries.
#include <iostream>
#include <SDL.h>
int main(int argc, char** argv)
{
std::cout << "yee haw!" << std::endl;
return 0;
}
This code is just giving me a console with the text:
(process 32) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
Where I would want 'yee haw!' preceding it.
It works fine when I take out the #include <SDL.h> (but I want the SDL.h)
I've heard that SDL now redirects to a stdout.txt file but I couldn't find that anywhere. I've also tried displaying a window with code from a tutorial I found, but that also gives me the empty console.
I'm using Visual Studio 2019 on Windows and SDL 2.0.9
Thanks!
By default, SDL uses a macro hack to replace the main function. The user defined main function must be in the following format:
int main(int argc, char** argv)
{
// whatever
return 0;
}
Alternatively, if you don't want this behaviour you can use SDL_SetMainReady.
#define SDL_MAIN_HANDLED
#include <SDL.h>
int main()
{
SDL_SetMainReady();
// whatever
return 0;
}

Visual Studio in debug mode restarts automatically after an exception is thrown (C++)

I'm working on a big C++ project which I haven't created, but the following short code fully represents a problem which I'm faced with:
#include <string>
#include <iostream>
int main(int argc, char **argv){
using namespace std;
try{
string str;
str = "r";
double d = stod(str);
cout << "'stod' worked! d = " << d << endl;
}
catch (invalid_argument){
cout << "I'm in 'catch'\n";
}
cout << "I'm after 'try-catch'\n";
cin.get();
cin.get();
return 0;
}
If I launch the code without debugger, everything works fine and as expected it gets into the catch-scope and then main() proceeds.
If I use the debugger, Visual Studio under 32-bit configuration restarts immediately or under 64-bit configuration I get a message: "The debugger's worker process (msvsmon.exe) unexpectedly exited. Debugging will be aborted."
I'm using VS2013 Ultimate on Windows 8.1 Professional.
Due to this post:
msvsmon.exe crashed when debugging
I have already installed Update 4. I have no breakpoints. In DEBUG->EXCEPTIONS the reset was done.
I also do not understand, what msvsmon.exe is. The Google-search combines it with remote debugger which confuses me a little bit, because I'm using the local debugger, at least I press each time the 'Local Windows Debugger' button.
Could anybody help me out?

OpenCV: Reading the frames of a video sequence

Anyone help me ,I am trying to run code to read frames from video in folder its success in building but when debugging there isn't any output
* I am using Visual studio 2012 ,opencv 2.4.11 version
the code is :
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int main()
{
// Open the video file
cv::VideoCapture capture("C:/Users/asus/Desktop/A.mp4");
// check if video successfully opened
if (!capture.isOpened())
return 1;
// Get the frame rate
int rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame in ms
// corresponds to video frame rate
int delay= 1000/rate;
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file.
// Not required since called by destructor
capture.release();
}
Your main() function is never executed. The only thing, that gets executed is _tmain(), which does nothing and returns immediately.
I haven't done much Windows programming in a while, but if I remember correctly this is how it works:
When Unicode is enabled for your compiler
int _tmain(int argc, _TCHAR* argv[])
gets compiled as
int wmain(int argc, wchar * argv[])
which is then used as the program entry point.
Since you seem not to be using any Windows-APIs in your code I would ignore the Microsoft specific way of doing multibyte character strings, which is non-portable, and simply use plain ASCII strings as you did in the main() function, that you intended to use.
So to solve your problem simply throw out the _tmain() function. Maybe you also need to disable Unicode in your project settings if you get linker errors.