C++ :: Calculate Something More Than A Var Can Handle?
Jan 1, 2013Let's say I want to calculate something like PI using some algorithm, but with the number of digits I want to.
View 1 RepliesLet's say I want to calculate something like PI using some algorithm, but with the number of digits I want to.
View 1 RepliesConsider the following code which closes an overlapped I/O serial handle during application shutdown.
Code:
Win32Com.CancelIo(hPort);
Win32Com.CloseHandle(hPort);
It works fine under .NET 2.0 but after switching to .NET 4.0 it crashes on the CloseHandle. Removing CancelIO doesn't work.
What the correct way is to close an overlapped I/O handle? And why is there the difference between NET2.0 and 4.0?
How to handle cmd line argument in c.
View 5 Replies View RelatedI've been thinking over this for long time... For example, when a button is clicked, print a string "clicked" after 5 seconds. (the button won't be blocked)
Thread should be the only way to do this:
btn.on_click = []() {
thread thrd([]() { sleep_5_seconds(); cout << "clicked" << endl; });
thrd.start();
};
the local variable thrd is destructed right after it starts, which may cause a crash, so use new operator:
btn.on_click = []() {
thread* thrd = new thread([]() { sleep_5_seconds << "clicked" << endl; });
thrd->start();
};
The thrd never get deleted
How would you solve problem like this?
If a class A contains an array of objects of another class B.. And in the constructor of A, while constructing objects of B, any one object throws an exception, then how can we guarantee to release all the memory acquired.. so that there is no memory leak..
class B{};
class A{
public:
B Array[100];
...
};
In the above code, if in constructor of A, suppose 99 objects of B are constructed successfully, but 100th object throws exception, then how can we guarantee to release all the memory acquired by the other 99 objects?
I have a game that I'm trying to create but I don't really know how to handle mouse motions yet. I have read the LazyFoo.net lesson 9. And I have too searched around on Google but haven't find any good article for what I want to do
But what I want is to check if the mouse is OVER the START button of the game and if the user has pressed the LEFT mouse button the game should START.
This works when I am hovering over the area there I want the mouse to do something:
Code:
else if (event.type == SDL_MOUSEMOTION)
{
if (event.motion.x == 0)
{
quit = true;
}
}
I do not know how to check this. The START and QUIT button is in the middle of the screen and I don't know how to position the mouse there either.
I am using SDL 2.0. But I don't know how to handle mouse input. Do you think you can explain of how to do that? Or give me a link that explains that really great?
I know how to handle a mouse button that presses down for a sec.
Code:
if (event.type == SDL_MOUSEBUTTONDOWN){
if (event.button.button == SDL_BUTTON_LEFT){
// Do something . . .
}
}
But not how to handle the mouse input of what the mouse is hovering over and that.
Like in a start menu of a game. I want a button for example to appear blue when I hover over it and when I click on it. It should start the game for example.
I do not know any of the functions to do this, and when I looked on the internet all the functions I saw were mainly non-standard.
View 12 Replies View RelatedI have an application that uses an array of threads to call a method along with thread.join(). I was just wondering what would be the best way to handle the thread in case if one of the thread fails? Should I put a try catch block on the method that is being called or should I put the try catch block on the array of threads, or is there any other proper way to handle failed threads?
View 3 Replies View RelatedAnd it is not running successfully... abnormal termination
View 3 Replies View RelatedI write a small application and using the following API to open the file:
hHandle = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Then I pass the hHandle to a DLL exported function, in that function, I will call
DWORD dwFlags = 0;
ASSERT(GetHandleInformation(hFile,&dwFlags) != 0)
to check if the handle is valid. However, the assert fails. And I use GetLastError to get the error code 6, which means invalid handle.
What is the problem with my codes?
Visual C++ 2010
I'm using SFML with Visual C++ and need to pass a handle to a function. I've tried to find this on the web with no luck.
The handle happens to be a sprite defined as: sf::Sprite Numbers(MyNumbers);
Now I want to pass "Numbers" to a function.
-
-
getFrame(Numbers);
-
-
???? getFrame(??????) {
Numbers.SetSubRect(sf::IntRect(0,0,63,63));
return ????
}
How do I do this?
Here is an example,
Code:
int main()
{
try{
int y = 0;
int x = 2/y;
}
catch(...)
{
cout<<"catch it"<<endl;
}
return 0;
}
If you try, you will find out that catch(...) couldn't catch exception divided by zero. How'd we catch such exceptions?
I have an MFC program created from the app wizard. It is an MDI program, reading/writing text files using Serialize. I can read the document and know that the entire document was read into my buffer without any errors. This was verified by compaing the number of bytes read with the file length.
How do I get the document to display in my main/child window?
How do I read the document from the window so I can save it back to the file?
As we know in C there is no checking if values passed to a function that takes enum are correct, that is if they have been defined in this enum. Example from Enums in C | Occasionally sane ([code] tags don't work on my fx 18.0.1 this is why I put in on pb): [URL] ......
Here c - How to check if an enum variable is valid? - Stack Overflow they say that common convention is add check whether value passed as the parameter is not bigger than the maximum value in enum. But how about situations when enum is composed of numbers from 1-20 and then from 500-510?
I programming currently something with OpenGL. Now I have written some wrapper classes, like Shader, Program .... All works fine, but I don't like to call Shader.GetHandle() every time I want to call a OpenGL function manually where I need the object handle/id. (GetHandle() returns the OpenGL ID of the object)
So now I wonder, is it possible to program it in C++ so, that I can put my objects to gl methods and c++ automatically pass the handle/id member to that function ? Is there maybe a operator that I can use for that?
For example:
The way I do it now:
Shader vertexShader();
glCompileShader(vertexShader.GetHandle());
The way I want to:
Shader vertexShader;
glCompileShader(vertexShader);
How can I handle splitter position changing?
View 2 Replies View RelatedHere is the code,
Code:
void foo() {
Acquiring lock
do something...
Func();
Releasing Lock
}
If the function Func throws an exception, there is potential deadlock issue. Then I handle exception like this,
Code:
void foo() {
Acquiring lock
do something...
try{
Func();
[Code] ....
Is this a good practice? I wonder how I can apply RAII in handling exception here.
My code is currently reads in a string of lower case letters, identifying the occurrence of each letter.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
[Code]....
My issue is that I want my code to read uppercase and special symbols. showing the occurrence of both.
Code:
else if(str[x] >= 'a' && str[x] <= 'z');
else if(str[x] >= '0' && str[x] <= '9');
However I struggle to implement it
In 2D games, what's the best way to handle the order of drawing objects? Because most games have a background, tiles to be drawn behind the player, perhaps tiles to be drawn covering up the player, etc. My point is, with my current setup of simply looping through all objects and drawing, I have no control over what objects are drawn on top of or behind the others. My best idea so far is to hold a vector of object pointers, each vector representing a different "visibility level", like so:
class Level{
//...
std::vector<Object> allObjectInstances;
std::vector<Object*> visibilityOne; //background objects
std::vector<Object*> visibilityTwo; //objects in front of background but not necessarily all the way in front
//and so on for more objects
};
If I go through with this, I'm wondering how I could loop through all my objects and add them to each vector, then shorten whatever I have to loop through for subsequent visibility vectors. handling the order of drawing objects?
How to create .cpp file (new class) that will handle all the random number generators (<random> library) that main could possibly access. When I looked for a way how to implement random generators the examples were always in the main function or in a class in the main.cpp (with no header file and the generator was not seeded as I imagine it should be = only once in a program).
Questions follows after this malfunction code.
main.cpp
#include "randomizer.h"
int main() {
Randomizer randObject(0, 10, 125); //Set bounds and seed generator
int mainVar = randObject.getRandInt(); //Store newly generated random number for future work
[Code] .....
1) So I think that I should somehow declare these generators and distribution in the header file. Is it like declaring int a;?
I tried writing std::default_random_engine defGen; into the header file which only silenced compiler's errors but defGen wasn't seeded.
I have seen some examples using auto a = randInt(defGen); (or maybe with the use of auto a = std::bind.... But what does auto represent? I can't use it in the header file.
2) Can I ensure that the randObject is seeded only once? If for examle I need randObject2 with different boundMax, can I still use the same defGen which was seeded the first time? That should ensure better "randomness" through the program, shouldn't it?
3) Is there better way (and easy too) to "interface" random number generators? The class in the end should provide "get" function to acces int, double, bool, signed/unsigned... or some other specialities like random prime numbers, etc.
If it is anyhow related I am using Code::Blocks IDE with GCC compiler (4.7.1)
Need a free image library that can handle png xresolution and yresolution. I need it to be compatible with visual studio 6.0
View 1 Replies View RelatedI'm stuck on the last part of my program. The directions are the following~
Expand the program to add an overloaded function to handle floating point numbers (i.e., doubles). Include output for one list of integers and one list of doubles. Use this function prototype: double avgx(double&, double&, int, ...);
Compile and run. You should have one function named avg, one named davg, and two functions named avgx
My code does not compile and I think I'm not declaring my function prototype correctly?
#include <iostream>
using std::cout;
using std::endl;
#include <cstdarg>
// function prototype(s)
int avg(int, ...);
[Code] ....
I'm trying to write a program to calculate an approximate value of e using the formula e = 1 + 1/1! + 1/2! + ....... 1/n!...However, I'm always getting nearly 2.291481 as answer.Here is my code :
Code:
//Approximate the value of e
#include <stdio.h>
int main(void) {
int n, j;
printf("Please enter value of n : ");
scanf("%d", &n);
[code]....
I know I'm not supposed to use system("PAUSE"), but this is self-study.
I'm trying to successfully run a program that calculates your BMI but, although it runs, it is not giving the the correct math. Instead, it gives me the number i've submitted as my weight as an answer. I'm using Visual Studio 2008 and the formula: weight / (height/100)*2
Here is my code
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int weight;
int height;
double BMI;
[Code] ....
I want calculate sum of every digit in my zip code so I wrote implementation function like that but, it shows "0" as answers, then I realize it should be call first, I add correctionDigitOf() in to constructor "Zipcode::Zipcode" then My zipcode are all "0' after I do this .....
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;
class Zipcode {
[Code] ....