Sorry for my newbie-ness
Does anyone have problems getting std::cout to output when using SDL?
I can't seem to get anything shown in the output even when I comment away the SDL codes.
#include <iostream>
//#include <SDL.h>
int main(int argc, char **argv){
//if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
// std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
// return 1;
//}
//SDL_Quit();
std::cout << "Testing" << std::endl;
return 0;
}
Edited: The window was closed too fast to see anything, so I added SDL_Delay(2000); after my std::cout, and I saw my output :)
Posting #Jonathan comment as the answer.
One thought might be that the window disappears too quick to see something. Try putting sleep(5); after your cout statement
Related
This question already has an answer here:
c++ - Doesn't name a type
(1 answer)
Closed 6 months ago.
I edit the code to clarify the actual code :
#include <fstream>
#include <iostream>
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
#include <nav_msgs/Odometry.h>
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
runtimeFile.open();
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
runtimeFile.close ();
std::cout << "Runtime data stored." << std::endl;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "main");
ros::NodeHandle nh;
ros::Subscriber Listener = nh.subscribe<nav_msgs::Odometry>("/odom", 100, callhandler);
ros::spin();
return 0;
}
error: `‘runtimeFile’ does not name a type
9 | runtimeFile.open ("cmg_operations_runtime.txt")
The error is the same, I hope someone to help me in this issue?`
In C++ all code must be inside a function. Additionally all C++ programs must have a function called main.
Further your code opens the file twice, once when you declare the runtimeFile variable and once when you call open. Did you not think it strange that you have the file name twice in your code? Don't open files twice. Finally, although it's not an error, there is no need to close the file, that will happen automatically.
Put all that together and you have a legal C++ program.
#include <fstream>
int main()
{
std::fstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
runtimeFile << "tempVector[j]" << ";\t";
}
EDIT
Some real code has been posted. Based on that I would remove the global runtimeFile variable and make it local to callHandler like the following
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
std::cout << "Runtime data stored." << std::endl;
}
However I can't really see how the latest posted code causes the error described.
No compiler issues, No linker issues, all that happens when this program is ran the PlaySound function returns FALSE and doesn't play any sound at all,
https://media.discordapp.net/attachments/670195970226651140/852503334505676820/unknown.png
As seen in the figure image above the directory is perfectly fine, I've ran this program on both Release, and Debug, Any help would be very much appreciated, the code is below,
#include <iostream>
#include <Windows.h>
#include <fstream>
#pragma comment(lib, "winmm.lib")
int main()
{
std::ifstream ThisFile;
ThisFile.open("S:\\Visual Studio Projects\\KB\\A\\Test.wav");
if (ThisFile.is_open())
{
std::cout << "std::ifstream Success!" << std::endl;
}
else
{
std::cout << "std::ifstream Failure!" << std::endl;
}
std::cout << "Begin Sound" << std::endl;
BOOL PlaySoundReturn = PlaySound(L"S:\\Visual Studio Projects\\KB\\A\\Test.wav", NULL, SND_SYNC | SND_FILENAME | SND_NODEFAULT);
if (PlaySoundReturn == 0)
{
std::cout << "Failed!" << std::endl;
}
else
{
std::cout << "Success!" << std::endl;
}
std::cin.get();
return 0;
}
Sorry! It's come to my attention all the WAVE files I was testing were for some reason invalid and unreadable, even though I could listen to them with other media software,
I'm developing an app that should freeze all input, both keyboard and mouse, for a period of time. I've tried using XGrabKeyboard, but I cannot revert its effect using XUngrabKeyboard, it does nothing.
Here's a minimal example you can easily compile:
#include <iostream>
#include <thread>
#include <chrono>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/cursorfont.h>
int main(int argc, char *argv[])
{
Display * dpy = nullptr;
dpy = XOpenDisplay(0);
if(!dpy)
{
std::cerr << "Error" << std::endl;
return 1;
}
std::cerr << "Grabbing..." << std::endl;
XGrabKeyboard(dpy, DefaultRootWindow(dpy), false, GrabModeAsync, GrabModeAsync, CurrentTime);
std::cerr << "Waiting 2 secs, you shouldn't be able to type anything" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cerr << "Ungrabbing..." << std::endl;
XUngrabKeyboard(dpy, CurrentTime);
std::cerr << "Try to type now" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
You can see that you cannot write anything anymore. I've tried clicking on the terminal, in case the focus is lost or anything, to no avail. Once the program finishes the keyboard is released.
Not sure if it has something to do with the parameters in the XGrabKeyboard call, I've tried modifying them (Sync vs Async, etc). But there's no difference.
Adding an XSync(dpy, true); (*) after XUngrabKeyboard makes the code behave in the way you expected. So possibly you have to process all the events you grabbed before the event queue resumes?
(*): don't actually do this, this is just to demonstrate that the problem is with the queued events
Also works:
XUngrabKeyboard(dpy, CurrentTime);
XEvent foo;
while (XPending(dpy)) XNextEvent(dpy, &foo);
Update - also works:
XFlush(dpy);
So... the problem is that the ungrab was not actually sent?
I've been trying to get into C++ and OpenGL development and such, but I've run into an issue I can't seem to figure out with the following code:
int main() {
std::cout << "Attempting to load" << std::endl;
if (!glfwInit()) {
std::cout << "Error loading GLFW" << std::endl;
return 0;
}
else {
std::cout << "Loaded GLFW" << std::endl;
}
using namespace kreezyEngine;
using namespace graphics;
Window window("Kreezy Engine", 800, 600);
while (!window.isClosed()) {
window.update();
}
return 0;
}
Now, the code works fine, it's just that I noticed it took 30 seconds to even create the window. After some debugging, I noticed that "Attempting to load" is being printed, but it takes around 30 seconds for "Loaded GLFW" to be printed in the console. I feel like thats really slow to initialize glfw, as the tutorial im watching takes no more than a second.
Any help?
Thanks :)
I'm doing a C++ assingment for a class and I haven't used C++ in a decade so this might be something I'm missing that is simple; however ,I can't seem to figure it out.
I have a class I defined with a function that is producing no output; it looks like it's not even running and I don't have a clue why. Could someone point out my problem to me?
Issue: cout from the function getwords of the class readwords doesn't display any results.
Here is my class:
class readwords {
private:
char c;
//string aword;
public:
void getwords(std::istream& file) {
cout << "I got here" << std::flush;
/*while(file.good()) {
cout << "I got here\n";
c = file.get();
if(isspace(c)) cout << "\n"; //continue;
if(isalnum(c)) {
cout << c; //aword.insert(aword.end(),c);
}
}
*/
}
};
Which is being called from my main:
#include <fstream>
#include <stdlib.h>
#include "lab1.h"
using namespace std;
readwords wordsinfile;
words wordslist;
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) {
// Looks like we have no arguments and need do something about it
// Lets tell the user
cout << "Usage: " << argv[0] <<" <filename>\n";
} else {
// Yeah we have arguements so lets make sure the file exists and it is readable
ifstream ourfile(argv[1]);
if (!ourfile.is_open()) {
// Then we have a problem opening the file
// Lets tell the user and exit
cout << "Error: " << argv[0] << " could not open the file. Exiting\n";
exit (1);
}
// Do we have a ASCII file?
if (isasciifile(ourfile)) {
cout << "Error: " << argv[0] << " only can handle ASCII or non empty files. Exiting\n";
exit(1);
}
// Let ensure we are at the start of the file
ourfile.seekg (0, ios::beg);
// Now lets close it up
ourfile.close();
}
// Ok looks like we have past our tests
// Time to go to work on the file
ifstream ourfile2(argv[1]);
wordsinfile.getwords(ourfile2);
}
Thank you for any help you can provide.
Try to use a debugger. Most IDEs (NetBeans, Code::Blocks, etc) provide an interactive interface with gdb.
I just compiled and ran your code, but nothing wrong with the code itself,
except that I needed to include to use the 'cout' method.
"I got here" has been successfully displayed in my ubuntu machine.
What is your execution environment? You should check it first.
The problem appears to be redefining my own class. When actually coding the function I needed to use:
in readwords::countwords(std::istream& file) {
....
}
Once doing this output produced fine.