mrnutty 761 Senior Poster

Here is what people usually do when creating a bullet class. Because bullets get
created and destroyed so fast, and because there are so many of them or can be. You
are better of using some raw arrays instead. Put a maximum cap on the number of bullets created. For example you can do something like this :

struct Bullet{
  vector3f startPosition;
  vector3f velocity;
};

template<int SIZE>
class BulletArray {
 private:
  boost::array<Bullet,SIZE> bulletList;
  int currentSize;
 public:
 BulletArray(): currentSize(){}
 bool addBullet(const Bullet& b){ 
    if(isBulletFull()) return false;
    bulletList[currentSize++] = b;
    return true;
 }
 //...more methods
};
mrnutty 761 Senior Poster

Do you know how to use polymorphism? Virtual inheritance? Pure virtual functions?
Problem with inline virtual functions? Try projecteuler.net. They have challenging
math problems for practice. Also research design patterns. There are plenty of stuff to
learn. Here read this. That should keep you busy.
Also how learning to implement different data structures? List, trees, TRIE ...

mrnutty 761 Senior Poster

>>Does it tell you what I know about C++?

Not really. Although you *solved the problem fast, it could be better. How far of C++
do you know? Any knowledge about meta-programming? Usually, the best way to get better at programming is to design project. That way you get a chance to practice algorithms and design patterns. But if you just want some problems to solve, then
google online. There are a lot of sites dedicated to practice problems.

-----
*solved: haven't really tested the program, just taking your word.

mrnutty 761 Senior Poster

First tell me what do you know about C++? And check out my sig.

mrnutty 761 Senior Poster

When you compare your answer, make sure its in correct units, radians or degrees?

>>so my first question, when i use the first fourmla, then how can i use the second one?


The first and second formula has nothing to do in this case. The first formula
shows one definition on cos and sin, while the second shows another definition of
cos and sin. The second definition is a recursive definition.

mrnutty 761 Senior Poster

Example:

class list{
public:
  class Node{
  };
};

list::Node n; //declare a Node object
mrnutty 761 Senior Poster

a better solution would be to define an operator>>. For example :

std::istream& operator>>(istream& is, Client& c){
 return is >> c.acccount >> firstName >> lastName >> balance;
}

//...in main somewhere
Client c;
while( file >> c) vectorVariable.push_back(c);
mrnutty 761 Senior Poster

You should call it like so:

sort(m,10); //call your sort method
//print the results:

for(int row = 0; row != 10; ++row){
  cout << "(" << m[0][0] << "," << m[0][1] << ")" << endl;
}
mrnutty 761 Senior Poster

Ok, I guess the easiest way to do this is to make your own data structure and insert it into a vector and sort it, then copy it back. So first create a struct to hold the elements.

struct Elem{
	int row;
	int col;
	Elem(int r, int c): row(r), col(c){}	
};

then create a compare function for the Elem struct:

bool elemComp(const Elem& lhs, const Elem& rhs){
	if(lhs.row < rhs.row) return true;
	else if(lhs.row > rhs.row) return false;
	//else both row are equal
	//so compare the column
	else if(lhs.col < rhs.col) return true;
	else return false;
}

now this will be your sort function:

void sortIt(int Array[][2], const int rowSize){
 //...
}

Now inside you are going to create a vector of elems like so :

std::vector< Elem > vecArray;

Now copy the array passed in, into the vecArray like so:

for(int i = 0; i < rowSize; ++i){
	vecArray.push_back( Elem(Array[i][0],Array[i][1]) );
}

Now all you got to do is call the std::sort function on it like so:

std::sort(vecArray.begin(),vecArray.end(),elemComp);

Now the vecArray is sorted the way you wanted it to be sorted. All thats left
is to copy it back into the original array like so :

for(int i = 0; i < rowSize; ++i){
	Array[i][0] = vecArray[i].row;
	Array[i][1] = vecArray[i].col;
}

So now the passed in Array is sorted by its x-axis, and secondary to its y-axis.

mrnutty 761 Senior Poster

Can you the library sort method?

mrnutty 761 Senior Poster

wait are you supposed to sort the rows first, and if there is any collision, like
2 values are the same in the rows, then you use the columns to resolve the conflict if
possible?

mrnutty 761 Senior Poster

Instead of making things global, why not make your function take in a _aline as a parameter?
For example ReadConfig(const _aline* line){ /* work with line variable */ }

And all you have to do is make sure that you can call ReadConfig from your file.

StuXYZ commented: I forgot that! +3
mrnutty 761 Senior Poster
Lusiphur commented: Thanks for the reference link :) +1
mrnutty 761 Senior Poster

I wrote this DateClass some time ago, see if you find a use for it. Study carefully and
you might find what you wanted.

#pragma once

#ifndef DATE_TIME_H_
#define DATE_TIME_H_

#include <ctime>
#include <string>
#include "CommonFunction.h"

using std::string;

typedef unsigned long ulong;

struct DateInfo{			
	ulong seconds;//current seconds
	ulong minutes;//current minutes
	ulong hours;//current hours
	
	ulong year; //current year
	ulong dayOfMonth; //current day of month [1-31]
	ulong dayOfYear; //current day of year since januaray 1st [0-365]
	ulong monthOfYear; //current month of the year [ 0 - 11 ]

	string day; //current day [mon-sun]
	string month; //current month [jan-dec ]
	

	string meridiem()const{ return (_amFlag? "AM" : "PM" ); }	

	DateInfo(bool isAm = false) : month(), day(), year(), hours(), minutes(), seconds(), _amFlag(isAm) {
	}
private: 
	bool _amFlag;
};

class DateTime{
public:
	typedef time_t TimeType;	
	typedef tm TimeInfo;
private:
	DateInfo dateInfo;	
public:
	DateTime(){ _initDate(); }
	const DateInfo& info(){ 
		return dateInfo; 
	}	
private:
	//helpers
	void _initDate(){
		TimeType rawTime = TimeType();
		TimeInfo info = TimeInfo();
		time( &rawTime );
		info = *(localtime(&rawTime) );		
		
		dateInfo = DateInfo(info.tm_hour < 12 ); //check if its am or pm

		dateInfo.seconds = info.tm_sec;
		dateInfo.minutes = info.tm_min;
		dateInfo.hours = _converToRegularTime( info.tm_hour );

		dateInfo.dayOfMonth = info.tm_mday;
		dateInfo.dayOfYear = info.tm_yday;
		dateInfo.monthOfYear = info.tm_mon + 1; //offset so jan = 1, feb = 2 ...

		dateInfo.year = _startYear() + info.tm_year;	

		dateInfo.day = _convertToDay( info.tm_wday );
		dateInfo.month = _convertToMonth( info.tm_mon );
	}
	const int _startYear()const{
		return 1900;
	}
	string _convertToDay(int dayNum)const{
		string day = "INVALID";
		switch( dayNum ){
			case 0 : day =  "SUNDAY"; break;				
			case …
mrnutty 761 Senior Poster

You should be fine, but you can always separate your logic :

if(condition1 && condition2){
  /*logic here */
}
if(condition1){
  if(condition2){
     /* logic here */
  }
}

But,you don't even need isOccupied variable, if you default initialize player to
null, in the class, then you can just check if seat[x].player == NULL.

But I see you are using raw arrays, just use vectors instead.

Also, maybe some context would help so we can help better the design. For example,
I question why inRound and requireAction as a member variable. Those name suggest
to me that, thats the responsibility of some other class. Remember to minimize
the things the classes needs to do, if possible.

mrnutty 761 Senior Poster

Thank you, I actually figured that out about a minute after posting that hehe... but another question along those same lines if thats okay.

How can I use an if statement to decide whether the input is a character or an integer?

if(x == char)
//code here...
if(x == int)
//code here...

I know that code is totally wrong, but it gets the point across. Anyone know how to make that happen?

First, why would you want to do that? Second you cant do that in C++, because no
matter what, you need to define the variable's type before getting an input from the user.

mrnutty 761 Senior Poster

No I want take one letter and convert that to integer. If that is not possible I have to use array or vector and size would be big.

char n1 = '1';
int num = n1 - '0'; //num equals 1
mrnutty 761 Senior Poster

So you want to convert it into an int array?

mrnutty 761 Senior Poster

A int is not going to hold a number that big. Use __int64 instead.

__int64 n = atoi("11111111111111");
mrnutty 761 Senior Poster

>>Strings used to be an array of char in C-style coding. Now they're objects in C++.
Forget about what it used to be is C. std::string is a class. An instance of a class
is called an object, so std::string name , here name is an object, because
it is an instance of a class.

>>Does this mean that int and char are not objects?
As pointed out, int and char are primitive data types. The are P.O.D

mrnutty 761 Senior Poster

Make a simple timer class to simplify stuff. For example :

#include <iostream>
#include <ctime>

using namespace std;

class ClockTimer{
public:
	typedef time_t TimeType;
private:
 TimeType startTime;
public:
 ClockTimer(): startTime(clock()){
 }
 TimeType elapsed(){
    return clock() - startTime;
 }
 TimeType elapsedInSec(){
   return  TimeType(elapsed()/float(CLOCKS_PER_SEC));
 }
 void start(){
    startTime = clock();
 }
 void reset(){
    startTime = clock();
 }
};

void wait(unsigned long sec){
	ClockTimer timer;
	timer.start();
	while(timer.elapsedInSec() < sec) continue;	
}
int main(){
	cout << "Loading...\n";
	wait(5); //seconds
	cout << "Done\n";
	
	ClockTimer timer;
	timer.start();
	while(true){
		if(timer.elapsedInSec() > 1 ){			
			cout << "moving character\n";
			timer.reset();
		}
	}
}
mrnutty 761 Senior Poster

What part of C++ troubles you, my young lad?

mrnutty 761 Senior Poster

Although you created no object, the point is that, the compiler might create
a static instance of the base destructor, thats why there might be a linking error.
Whether the compiler does this or not, is depended on the compiler, but recent compiler
will not do this, but older ones might.

mrnutty 761 Senior Poster

when getting a name, into a string variable, or any input from into a string variable
if you use getline(cin,someStringVariable) , then it reads the whole line
into the someStringVariable. The problem with yours was that cin stops reading when
it reaches a whitespace. because of that, there could be some input left in the buffer,
and in your case it was, which causes the input to fail. So when getting a name into
a string a variable use getline(cin,name).

mrnutty 761 Senior Poster

You should only have 1 main, 1 entry point! I don't know if you have 2 int main() , but if you do, then delete one of them or transfer on of them to the other.

usafsatwide commented: Thanks for all your help +1
mrnutty 761 Senior Poster

Post full error

mrnutty 761 Senior Poster

Ahh, you define the variable "info" twice, one in line 13 and the other in line
28. Just delete line 13.

mrnutty 761 Senior Poster

Can you show ur current code.

mrnutty 761 Senior Poster

in your employee.h, after the #include <string>
insert using std::string;

mrnutty 761 Senior Poster

char is short for character, which means only 1 letter, not multiple.
So doing this char a = "huh" generates an error because a char variable
can only point to one letter.

To solve your problem replace char with std::string, in the return type as well as
the type of nameShift.

mrnutty 761 Senior Poster

Let me try to take a crack at this, after googling it. Its gonna be a long post, so
I hope you are ready to read it.

So first things first you know what inline functions are right? Just to recap, when
you inline a function, you suggest to the compiler that it inline or substitute the
definition where the function call was.

So the next thing, you probably know what virtual function does. Virtual functions
enable polymorphism. When you call a derived class from a base class pointer/ref, it
calls the derived class function.

Ok so thats cleared up. Now lets get started towards the problem.

Here is the first question that needs to be answered, and the main problem
that underlies with inline virtual function, consider the following code :

struct B{
 inline void show(){ _showIt(); } //explicitly tell compiler to hint of inlining it
 void tell(){ _tellIt(); } //implicitly tell the compiler to hint of inlining this func
};

int main(){
 B b;
 b.show();
 b.tell();
}

So suppose a the compiler accepts your inline hint, and inline those function, then
that means b.show() and b.tell() would be substituted with its definition.

Ok now consider this code :

struct B{
 virtual int id(){ return 1; }
};
struct D: B{
 int id(){ return 2; }
};

int main(){
 B base;
 D derv;
 
 base.id();
 derv.id();
}

Now look at the above code. The type of …

StuXYZ commented: Excellent deep answer +3
Nick Evan commented: Good job +15
mrnutty 761 Senior Poster

>>string word2("Over");
>>string word3(3, '!');

The string class, has the proper constructor that allows those syntax. So it calls
the string class constructor. Other that that, i'm not so sure about your question, maybe its because i'm tired.

mrnutty 761 Senior Poster

You should try looking into operator overloading:

int* operator >> (int *RHS)
{
   RHS = this->array;
   return RHS; //For chaining
}

Your operator overloading would allow this obscure code :

int * a = {1,2,3,4,5};
Integer num = Integer(a,5);
num >>(a)

which to me dosen't make sense that much. It would be hard to read.

@OP: Don't provide a way to return the internal array. Instead make the class
an array, by overloading either[] or () operator.

mrnutty 761 Senior Poster

Here is what I got :

#include <iostream>

using namespace std;

template<typename T>
void printN(T arg, int n){
 while(n--)cout << arg;
}

int main(){
 const int ROW = 9;

 for(int r = 0; r != ROW; ++r){
      printN(ROW, r);		
      printN('*',ROW - r);
      cout << endl;
 }

  return 0;
}
mrnutty 761 Senior Poster

>>And what will happen if the first character is white space
Then it just skips it because I used cin and not getline. Here is a test.

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <sstream>

using namespace std;


int main(){
	string input = " hello this is a test";
	std::stringbuf inStream = std::stringbuf(input);
	cin.rdbuf(&inStream);
	string in;
	while(cin >> in){
		in[0] = toupper(in[0]);
		cout << in << endl;
	}
}
mrnutty 761 Senior Poster

whats with the c-style syntax? Use proper C++ syntax here, or else jump towards the C
forum, where your kind are welcomed lol.

string input;
while(cin >> input){
 input[0] = toupper(input[0]);
 cout << input << endl;
}
intills commented: thanks and sorry for not using Proper C++ syntax im new to this cite and to both c and c++, thanks a lot +0
mrnutty 761 Senior Poster

You can use an online compiler if you want. That way you can compile your code and such.

mrnutty 761 Senior Poster

Also this code, remember to use brackets to declare a block :

if(AdventureGame.keypressed[DIK_6]){
 /* code goes here */
}

Also I'm not sure if ur trying to optimize by this statement :

if (AdventureGame.keypressed[DIK_F] && (ignition == false))
		ignition = true;
 
if (AdventureGame.keypressed[DIK_G] && (ignition == true))
		ignition = false;

but you can just do this :

if(AdventureGame.keypressed[DIK_F] ) ignition = true;
else if(AdventureGame.keypressed[DIK_G]) ignition = false;
mrnutty 761 Senior Poster

You don't need while loop. IMO the forloop looks better and easier to read( most of the time).

mrnutty 761 Senior Poster

Remember, when defining a class, you should make that class objective minimal as possible. So don't dump all functions and variable into the class if not necessary.

mrnutty 761 Senior Poster

>>But another problem I have is that is has to accept an int type and also a double type

There are couple of ways to do this. It depends on what you know. Have you
heard of function overloading? Here is an example :

void print(int a){ cout << a << endl; }
void print(double b){ cout << b << endl; }

int main(){
 int a = 5;
 double b = 3.14;
 print(a); //call the print(int);
 print(b); //call the print(double);
}

>>have to use references for this

Using reference is not that hard. You just need to understand what it means to use reference.
Here is an example :

void nonRef(int a){ a = 0; }
//use the '&' to mean pass-by-reference
void ref(int& b){ b = 0; }

int main(){
 int a = 123;
 nonRef(a); //a = 123 still because nonRef makes a copy of the variable a
 cout << a << endl;
 ref( a ); //a = 0 because ref does NOT use a copy of the variable a, instead it uses the actual variable a.
  cout << a << endl;
}
mrnutty 761 Senior Poster

>>I'm not sure how to create a function that accepts multiple arguments.

Its easy, its very same as a single argument function. Compare these 2 functions.

void one(int a){ /* do something with a */}
void two(int a, int b){ /* do something with a and b */ }

int main(){
  one(5); //call the one parameter function
  two(1,4); //call the two parameter function
}

That should get you started.

mrnutty 761 Senior Poster

>>g. your line result=pre_result; will actually call the copy constructor and not the assignment operator (operator=)

Really? I thought it would call the assignment operator and could call the copy constructor depending on the implementation.

mrnutty 761 Senior Poster

Here are my guess:

1) Dont get the question, what does "name" mean? DataType?
2) false;
3) C: Increment operators
4) confusing
5) false;
6) c: *
7) false: runtime variable?
8) false;
9) b: 12,81
10) d: shallow
11) true;
12) false, it can;
13) false;
14) true;
15) false;
16) b: deep;
17) false; incremental and subtraction and addition are some counter examples
18) d: pointer-to-int
19) false;
20) d: 46
mrnutty 761 Senior Poster

Why is Vector3::operator* returning a const Vector3 and not just a regular Vector3?

Why does your Quanternion class contain a "x,y,z" variable, when you have Vector3 for
that exact purpose?

Unless, your using dynamic memory in Vector3, which I doubt, then I am doubting your
debugging skills. What happens if you just return pre_result?

mrnutty 761 Senior Poster

OMG, just use vectors :

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class StringArray{
private:
 std::vector<string>  strArray;
public:
 StringArray(const std::vector<string>& vecStr): strArray(vecStr){};
 void print(){ /* print vector here */;
};

int main(){
 std::vector<string> vec(5,"hello"); //contains 5 of "hello"
 StringArray strArray(vec);
 vec.print();
}
mrnutty 761 Senior Poster

umm...couple of ways, first is the easiest.

//option #1
string path = "AMBER/data/runx/amhsk_run"; 
string runNumber;
cin >> runNumber;
validate(runNumber);
path += runNumber + ".root";
//option # 2
string path = "AMBER/data/runx/amhsk_run*.root";
string runNumber;
cin >> runNumber;
path.replace( path.find('*'),1,runNumber) ;
mrnutty 761 Senior Poster
mrnutty 761 Senior Poster

Given two numbers, you can use std::max(a,b) to return the largest out of a and b.
Thus using the same logic you can just nest std::max for 3 inputs, so std::max( std::max(a,b) , c) returns the largest out of the 3 inputs. There is a very similar patterns for 4 inputs and so on.

mrnutty 761 Senior Poster

Given two numbers, you can use std::max(a,b) to return the largest out of a and b.
Thus using the same logic you can just nest std::max for 3 inputs, so std::max( std::max(a,b) , c) returns the largest out of the 3 inputs. There is a very similar patterns for 4 inputs and so on.