Jump to content


Check out our Community Blogs

Flying Dutchman

Member Since 10 Feb 2010
Offline Last Active Mar 24 2016 03:21 AM
*****

#642812 Destructor Exception

Posted by Flying Dutchman on 24 October 2012 - 04:47 AM

Don't never ever throw from a destructor. Extremely bad magic will happen. This section explains why.
  • 1


#642167 c - follow the black line

Posted by Flying Dutchman on 16 October 2012 - 06:53 PM

This is very hardware specific stuff, and we'd need detailed description's of what those functions (GetAnalong, SetMotor, TimeStruct) do.
  • 1


#642038 vector and function template arguments

Posted by Flying Dutchman on 15 October 2012 - 10:57 PM

I'm not sure if this is legal:
vector<Type[2]> Obj ;

You can try this:
std::vector<int*> v;

v.push_back(new int[2]{1,2});
v.push_back(new int[2]{3,4});
v.push_back(new int[2]{5,6});
v.push_back(new int[2]{7,8});

for (auto i : v)
    std::cout << i[0] << ", " << i[1] << std::endl;

Or better yet, why don you create structure/class.
  • 1


#629625 Connect Python On A Site

Posted by Flying Dutchman on 11 May 2012 - 09:31 PM

You have to know at what URL the button points, or find out if you can download a file by direct access.
  • 1


#628040 Mysqldb

Posted by Flying Dutchman on 25 April 2012 - 03:20 AM

Guess var is of type long. Try this:
db.execute("Select * From test_2 Where key_test_1=%d" % var[0])
It would also be helpful if next time you could provide data types of your columns.
  • 1


#627934 Mysqldb

Posted by Flying Dutchman on 24 April 2012 - 03:19 AM

You're missing % and before (name.strip()...)

You also have to call, if I'm not mistaken, commit method to actually insert into DB.
  • 1


#627746 Files In Python

Posted by Flying Dutchman on 22 April 2012 - 07:00 PM

import re

vectors = []

with open("file.txt") as f:
  for line in f.readlines():
    # no space between \ and t
	line = re.sub("[\ t\n ]+", " ", line.strip())
	if line:
	  vectors.append(map(int, line.split(" ")))

print vectors
No error checking and assuming only numbers are file.
  • 1


#626524 Code Advise: counting how many times something appears in a string

Posted by Flying Dutchman on 12 April 2012 - 08:50 AM

You can with regular expressions or with find method. find returns the index of the match and you can also specify offset.
  • 1


#626369 Putting Two funtions in one .cpp file

Posted by Flying Dutchman on 11 April 2012 - 11:54 AM

Only error I got was that main2 was not defined (no header like the rest of functions have). I've moved main's code to main2 and renamed it to main and it seems to work just fine:
0
1
2

Target distance
In feet: 100

The launcher angle
Enter degrees: 40

The Projectiles velocity
Enter Projectile velocity: 20

***********************************************************
Shot Number: (1) Looks like your running out!
Target Distance: 100
Launcher Angle: 40
Projectile Velocity: 20
Projectile Distance: 12.2336

You missed by: 87.7664 feet, Better luck next time!
***********************************************************

The launcher angle
Enter degrees:
Code:
#include <iostream>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int GenerateID()
{
	static int s_nID = 0;
	return s_nID++;
}
float GetTargetDistance(); // Target's Dist
float CalcWinningRange( float ); // Range to Win
float GetLauncherAngle(); // Angle of Projectile
float DegreesToRadians( float ); // Radian Conversion
float GetProjectileVelocity(); // Projectile Velocity Formula
float CalcProjectileDistance( float, float ); // Projectile Dist Calc
void  PrintSummary( int, float, float, float, float); // User Output
int main()
{
std::cout << GenerateID() << std::endl;
std::cout << GenerateID() << std::endl;
std::cout << GenerateID() << std::endl;

	float targetDistance = 0.0;
	float winningRange = 0.0;
	float launcherAngle = 0.0;
	float ProjectileVelocity = 0.0;
	float launcherRadians = 0.0;
	float ProjectileDistance = 0.0;
	bool  hit = false;

	targetDistance = GetTargetDistance();
	  
	winningRange = CalcWinningRange(targetDistance);
	for (int shotNumber = 1; shotNumber <= 5; shotNumber++)
	{
	  launcherAngle = GetLauncherAngle();
	  ProjectileVelocity = GetProjectileVelocity();
	  launcherRadians = DegreesToRadians(launcherAngle);
	  ProjectileDistance = CalcProjectileDistance(launcherRadians, ProjectileVelocity);
	  PrintSummary(shotNumber, targetDistance, launcherAngle,
	  ProjectileVelocity, ProjectileDistance);
	  if ( fabs(ProjectileDistance - targetDistance) <= winningRange )
	  {
	   hit = true;
	   break;
	  }
	}
	  
	if ( hit == true )
	  cout << "Congrats You Win!\n";
	else  
	  cout << "To Bad Better Luck Next Time!\n";
	return 1;
}
//************************************************************************
// Funstion to Get Target Dist
float GetTargetDistance()
{
	float distance = 0.0;
	while ( distance <= 0.0 )
	{
	  cout << "\nTarget distance" << "\nIn feet: ";
	  cin >> distance;
	}
	return distance;
}
//************************************************************************
// Function to get launch Angle
float GetLauncherAngle()
{
	float angle = 0.0;
	while ( angle <= 0.0 || angle >= 90.0 )
	{
	  cout << "\nThe launcher angle" << "\nEnter degrees: ";
	  cin >> angle;
	}
	return angle;
}
//************************************************************************
// Function to get Velocity
float GetProjectileVelocity()
{
	float velocity = 0.0;
	while ( velocity <= 0.0 )
	{
	  cout << "\nThe Projectiles velocity " << "\nEnter Projectile velocity: ";
	  cin >> velocity;
	}
	return velocity;
}
//************************************************************************
// Function to Calc Winning Range
float CalcWinningRange( float targetDistance )
{
	return (targetDistance * 0.001);
}
//************************************************************************
// Radian Converter
float DegreesToRadians( float launcherAngle )
{
	return (launcherAngle * 3.14159265 / 180.0);
}
//************************************************************************
// Distance Calc
float CalcProjectileDistance( float radians, float velocity )
{
	return ((velocity * velocity)  * sin(2 * radians) / 32.2);
}
//************************************************************************
// Primary User Output
void  PrintSummary( int shotNumber, float targetDistance, float launcherAngle, float ProjectileVelocity, float ProjectileDistance)
{
	cout << "\n***********************************************************\n"
         << "Shot Number: (" << shotNumber << ") Looks like your running out!" << endl
         << "Target Distance: " << targetDistance << endl
         << "Launcher Angle: " << launcherAngle << endl
         << "Projectile Velocity: " << ProjectileVelocity << endl
         << "Projectile Distance: " << ProjectileDistance << endl;
  
	if ( ProjectileDistance <= targetDistance )
	  cout << "\nYou missed by: "
               << targetDistance - ProjectileDistance
               << " feet, Better luck next time!\n";
	else
	  cout << "\nYou over shot by ( "
               << ProjectileDistance - targetDistance
               << " ) feet, Try Again!\n";
	cout << "***********************************************************\n";
}

  • 1


#623492 C++ To C Help

Posted by Flying Dutchman on 07 March 2012 - 01:10 PM

You don't need 3 variables, it's easier with 1 because you're doing math on just 1 variable.
[COLOR=#333333]scanf("%d\n", &n);
[/COLOR]              ^
              +-- this says pass the address of n

  • -1


#619256 2D array game going wrong.

Posted by Flying Dutchman on 11 January 2012 - 12:27 AM

First, is there a reason why all code is jammed up like this?

Next, use more functions! Also, read on classes (for example, create Player, AIPlayer, Matrix or Grid).

Main problem is that you're not checking for bounds; if you go out of bounds, strange things will happen (like getting garbage values, crashing the program, ...).

Next, your loop should be like this (game loop):
while running
    get input
    do logic
    draw
With console games this usually doesn't show (make) difference, but when you start with 2D/3D, you will see huge difference. You can read more about game loops here (lazyfoo.net) or do a google search.


Overall, game looks pretty sweet and could easily be upgraded to full 2D (maybe even 3D?).
  • -1


#614479 documentation comment

Posted by Flying Dutchman on 17 November 2011 - 05:32 PM

Yes, you are right. Any kind of comments are skipped by compiler.
  • -1


#611959 How to display text using SDL?

Posted by Flying Dutchman on 21 October 2011 - 12:05 PM

If it's possible, please post some code. At least the relevant parts.
  • -1


#611724 How to fix Project Euler in C++?

Posted by Flying Dutchman on 18 October 2011 - 04:48 PM

I remember this one, had a (probably similar) problem myself. I don't want to spoil it for you, but maybe rereading the question again might give you an idea.

EDIT: Removed the hint, since WP posted not so obvious one.
  • 1


#600739 How to properly use trig functions in C++

Posted by Flying Dutchman on 22 May 2011 - 01:35 PM

Here's a list of those functions and some more and each comes with a provided sample code.
  • -1




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download