mrnutty 761 Senior Poster

a program in c++ which take some string from user and generate a unique code against it

Thats called hashing. You could do something like this :

string str = "abc";
unsigned long long hashCode =  str[0]*1 + str[1]*2 + str[2]*3;

Basically, you are multiplying the value at the string's position by its index plus 1.

If you want something better, google the term hash function.

mrnutty 761 Senior Poster

and why can't you try this in a C++ compiler?

mrnutty 761 Senior Poster

"num-65" : it subtracts 65 from num.

mrnutty 761 Senior Poster

Here is an example of your problem :

//Player .h
#include "Monsters.h"
//...
//Monster.h
#include "Player.h"
//...

Your problem is called Circular Dependency
See if you can solve it on your own.

mrnutty 761 Senior Poster

First some design problem. This function is pretty bad :

void attackplayer(Player& hero, bool checkup, bool checkdown, bool checkleft, bool checkright){
 if (checkup == true || checkdown == true || checkleft == true || checkright == true){
	hero.damage(damage);
 }
}

First an obvious way to make this better is to use the boolean as boolean, no need
for conversions, thus you should just do this

void attackplayer(Player& hero, bool checkup, bool checkdown, bool checkleft, bool checkright)
{
	if (checkup || checkdown  || checkleft || checkright ){
		hero.damage(damage);
	}
}

But its still bad. Specifically, you have a variable for each direction. What you
should do is abstract the direction. So the param is just 1 Direction and in that
Direction it has left,right,up,and down states. So do Something like this :

struct Directions{
public:
	enum Dir{EAST,WEST,NORTH,SOUTH,SIZE};
private:
	bool dir[SIZE];
	Directions(){
		std::fill(dir,dir+SIZE,false);
	}
public:
	void east(bool r){ dir[EAST] = r; }
	void west(bool l){ dir[WEST] = l; }	
	void south(bool s){ dir[SOUTH] = s; }
	void north(bool n){ dir[NORTH] = n; }

	bool get(const Dir direction)const{
		return dir[direction];
	}
};

class Player{
private:
	Directions playerDirection;
public:
	void attackplayer(Player& hero,const Directions& dir){
		if(dir.get(dir.EAST) || dir.get(dir.WEST) || dir.get(dir.NORTH) || dir.get(dir.SOUTH)){
			/* Do stuff */
		}
	}
};

No that might look like a little more work, but end the end it will save you more trouble.

mrnutty 761 Senior Poster

firstPerson - what is this supposed to do? Convert a string to an int? Shouldn't he just use a std::stringstream?

sorry misread his post.

mrnutty 761 Senior Poster

If its only 1-3 length, then just hard code it like so :

int toInt(const string& str){
 int result = 0;
 switch(str.length()){
   case 1: result = str[0] - '0'; break;
   case 2: result = (str[0]-'0')*10 + (str[1] - '0'); break;
   case 3: result = (str[0]-'0')*100 + (str[1] - '0')*10 + (str[2] - '0'); break;
   default: /* not supported */ break;
 }
  return result;
}
mrnutty 761 Senior Poster

to convert to a string use this function :

#include <string>
#include <sstream>
#include <iostream>

using namespace std;


template<typename T>
string toString(const T& arg){
 std::streamstream ss;
 ss << arg;
 return ss.str();
}

int main(){
 cout << toString(123) << endl;
 cout << toString(12.345) << endl;
}
mrnutty 761 Senior Poster

The way you did it is fine, you can also use aggregation if you want. Each have their
flexibility. Having a do nothing function is fine.

mrnutty 761 Senior Poster

>>Don't have a bird perform a movement that it can't perform

Can you explain this a little. Does he want a compile error? Does he wants to return
error, exceptions? Display error message?

mrnutty 761 Senior Poster

Seems like a homework problem to me. But you need to use the following :
1) Polymorphism
2) Classes
3) std::vector
4) virtual functions
5) Inheritance

mrnutty 761 Senior Poster

Why do you have 2 isDead function? From your code, it seems like isDead function needs
to be just a function like so :

struct Particle{
 float posX, posY;
};

bool isParticleDead(const Particle& p){ return isOutOfScreen(p);}

int main(){
 list<Particle> p;
 p.push_back( Particle() );
 p.push_back( Particle() );
 p.remove_if(isParticleDead);
}
mrnutty 761 Senior Poster

Maybe some code would help :

struct Shape{
 virtual void draw()const{}
};
struct Rectangle : Shape{
 void draw()const{ rect.draw(); /*some low level command*/ }
};
struct Circle : Shape{
 void draw()const{ circle.draw(); /*some low level command*/ }
}
int main(){
 Shape allShapes[3] = { Shape(), Rectangle(), Circle() };
 for(int i = 0; i < 3; ++i){
  allShapes[i].draw(); //draw circle, rectangle or whatever it points to
 }
}
mrnutty 761 Senior Poster

The derived object is not the main focus, its the base object. By the base object
being able to point to the derived object, it calls Derived::print function, if it in fact points to a derived object else it calls its own. Of course this assumes print is
a virtual function. So what exactly is the problem you are having?

mrnutty 761 Senior Poster

Oops, there is a major bug in my program. Say the call was :

std::vector<string> words;
std::string text = "This, is a string, to be dealt with;
words = split(text,',');

The problem is that it will split by both, space and comma. So disregard all my code and use this :

vector<string> split(const string& text,const char sub ){
	stringstream ss(text);
	vector<string> words;	
	string tmp;
	while(ss && getline(ss,tmp,sub)){ words.push_back(tmp);}
	return words;
}
mrnutty 761 Senior Poster

Idk, maybe its just me, but personally, I like returning the result, instead of it
being an argument is better. It feels like C++ is more return result, and C is more write result
into the destination provided.

mrnutty 761 Senior Poster

Here is C++ version just because I'm feeling nice today.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <sstream>

using namespace std;

template< typename T>
struct EqualCheck{
	T rhs;
	EqualCheck(): rhs(){};
	EqualCheck(const T& Rhs) : rhs(Rhs){}
	bool operator()(const T& arg){ return arg == rhs; }
};
vector<string> split(const string& text,const char sub ){
	string textCopy(text);	
	replace_if(textCopy.begin(),textCopy.end(),EqualCheck<char>(sub),' ');	
	stringstream ss(textCopy);
	vector<string> words;	
	std::copy(istream_iterator<string>(ss),
			  istream_iterator<string>(),
			  back_insert_iterator<vector<string>>(words)
			  );
	return words;
}

template<typename T>
void println(const T& arg){ 
	cout << arg << endl; 
};
int main(){
	std::vector<string> words;
	std::string text = "This@Is@My@String@";	
	words = split(text,'@');
	std::for_each( words.begin(),words.end(),println<string>);

	return 0;
}
mrnutty 761 Senior Poster

Its pointless doing this since, you are using string, unless you are doing this as an
exercise.

string text = "This is the text I want to search in";
string wordToFind = "want";
bool isFound = text.find(wordToFind) != string::npos;
mrnutty 761 Senior Poster

I have a string that includes numbers and letters and I want to be able to seperate the string into parts. An example of the string looks like this FAP834E. I want to seperate it so that the first letter is seperate (F), the second and third are together (AP), the fourth and fifth are together (83), the sixth is seperate (4) and the last is seperate (E).

Please letme know if you can help me

Look into substr.

mrnutty 761 Senior Poster

Here is an example :

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

using namespace std;

int main(){
 vector< string > names;
 const string END = "!";
 cout << "Enter many names... <enter '" << END << "' to quit >\n";
 while(true){
  string temp;
  cin >> temp;
  if(temp != END)
    names.push_back(temp); //add names
  else break;
 }

  for(int i = 0; i < names.size(); ++i){
   cout << names[i] << "\n";
  }

  return 0;
}
mrnutty 761 Senior Poster

Whats wrong with this :

vector< vector<Point> > possible_neurons
vector< vector<Point> > possible_neurons_second_copy;
possible_neurons_second_copy = possible_neurons;
mrnutty 761 Senior Poster

> Arrays are not pointers.
Except when they are. ;)

oh I see what you mean. Thanks for that clarification.

@OP, this code

if(arg == "test"){...}

is not comparing the content. Its comparing addresses. Use strcmp like so :

if( strcmp(arg,"test") == 0) { ... }
mrnutty 761 Senior Poster

No that you have hardcoded the work, I would use stl to lessen the work I have to do. That means, more efficient code, safer, readability, and shorter code.

For example, your listPropertiesSorted could be something like this :

bool propertiesCompare(const Property& lhs, const Property& rhs){
 /* compare properties here */
}
void printProperty(const Property& p){
 /* print property code here */
}
void listPropertiesSorted(Property properties[],const int items){
 std::sort(properties,properties + items, propertiesCompare);
 std::for_each(properties,properties + items, printProperty);
}
mrnutty 761 Senior Poster

void* is usually used to simulate generics in C. In C++, template were made to remove
this bad method. Since you are using C++, definitely, ditch void* and use templates.

mrnutty 761 Senior Poster

I hope there is no naming conflict here , since there is a std::list. But this code:

listArray = new list[hashTableSize];

will THROW and exception, instead of setting listArray to null. So there is no
reason to check if listArray is null, because you won't get a chance to check it, unless you use nothrow.


EDIT, from your previous post, you need a good constructor :

struct hashType{
	listType *head;
	listType *tail;
	int numValue;
 //use a initializer list, to initialize head,tail,numValue using its default constructor.
 hashType() : head(), tail(), numValue(){} 
};
mrnutty 761 Senior Poster

The array size in a function parameter is ignored because array notation in the parameter list is a syntactic convenience. The array is really just a pointer.

int main(int argc, char *argv[1])

// is the same as

int main(int argc, char **argv)

Misleading, maybe, but accessing argv[1] is not an out of bounds access provided argc is at least 2. The real problem here is accessing argv[1] *before* testing argc. ;)

I don't see how, char*argv[1] is the same as char**argv. Arrays are not pointers. Either
he's wrong for putting that "char*argv[1]" there, or I'm not seeing something here.

mrnutty 761 Senior Poster

Also subtle but still there, this part :

char* argv[1]

creates a array of char pointer, with size 1, thus only index 0 is valid. That means
that this code :

string arg = argv[1];

is a index out of bounds.

mrnutty 761 Senior Poster

Note there are explicit formulas for 2x2 and 3x3 matrix, see here

mrnutty 761 Senior Poster

Not compiled.

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
using namespace std;

int main(){
 std::ifstream fileReader("text.txt");
 std::string content;
 std::string temp;
 while( getline(fileReader,temp) ){ content += temp; }
 
 char letterToFind = 'A';
 int count = std::count(content.begin(),content.end(),letterToFind);
 cout << count << " occurrence of '" << letterToFind << "'\n";
}
mrnutty 761 Senior Poster

If you know how to convert from base 10 to base 2, then you know how to convert
from any base to any other base. So the question remains, do you know how to convert
from base 10 to base 2?

mrnutty 761 Senior Poster

>>I want to optimize the following small integer program in speed

May I ask why?

mrnutty 761 Senior Poster

>>is this correct or wrong? why?

what if you wanted to add more cities, say 100 more. How much code do you think you
would have to write ? Thus a good code, not only cares about its present status, it also cares about its future status as well.

mrnutty 761 Senior Poster

Which one?

Transpose, adjoint, inverse, addition, identity, multiplication ...

subtraction, LU factorization, elementary row operations, power, determinants ...

mrnutty 761 Senior Poster

>>typedef std::vector vector <class B> bVecArray

is that supposed to be :

typedef std::vector< vector <class B> >bVecArray
//or
typedef std::vector<class B> bVecArray
mrnutty 761 Senior Poster

opengl does not have anything for printing a text. If you are using glut. Then
you can use glutBitmapCharacter function. If not, then there are plenty of libs that
prints fancy texts for you. Go ahead and google it.

mrnutty 761 Senior Poster

why yes there is.

mrnutty 761 Senior Poster

>>A fancier way to reverse the string can then be used:

Another way is to use algorithms.

std::originalString = "1010101";
std::reversedString = std::reverse(originalString.begin(),originalString.end());
mrnutty 761 Senior Poster

what if your sentence is longer than 29 letters? Thus you need to change "char word[30],rev[30]" to accompany this. That is why you should use std::string.

std::string word;
std::string reverseWord;
cin >> word; //word can be of any length, not like the char word[30] array you declared

//now do your logic just as it word were an char array
mrnutty 761 Senior Poster

Why would anyone do this? Ouch my eyes. Use either std::for_each, BOOST_FOR_EACH,
or even manually.

mrnutty 761 Senior Poster

>>But all temporary objects constructed will automatically be made const even though function is not of const type

Well if that was true then your example would have been a compile error wouldn't it?
Temporary object will adapt to the situation.

mrnutty 761 Senior Poster

No you are confusing yourself. This is what happens as pointed out :

A ob4 =  (ob1.operaotr+(ob2)).operator+(ob3);

But I think your problem is that this code :

A operator + (const  A & ob){
   return (num+ ob.num);
 }

This code does NOT return a const object. It returns a temporary object in which
its content could be modified. What it does is promise that the object ob passed
in does not get modified.

mrnutty 761 Senior Poster

When you change the array size, change the bounds on the for loop as well. Thats why
you should use integer constants :

const int MAX_SIZE = 10;
int inputArray[MAX_SIZE] = {0}; //initialize all elements to 0

 for(int i = 0; i < MAX_SIZE; ++i){
  cin >> inputArray[i];
 }

 //sort array using bubble sort
 for(int i = 0; i < MAX_SIZE; ++i){
   for(int j = 0; j < MAX_SIZE; ++j){
       if(inputArray[i] > inputArray[j]) std:::swap(Array[i],Array[j]);
    }
  }

  //print Array
 for(int i = 0; i < MAX_SIZE; ++i){
   cout << inputArray[i] << " ";
 }

Now if you want to change the array size, all you have to do is change only MAX_SIZE
to a different number.

mrnutty 761 Senior Poster

What are you trying to do? Copy a code and get more code from someone else and say
you did it? If you're not going to a programmer, then its ok, I guess. But if you
decide to become a programmer, then stop with this nonsense and start learning.

mrnutty 761 Senior Poster

>>but still, I wont be able to code a working and satisfying OOP design

Don't worry not many people do. It takes time and practice and more time and practice.
Until, then start reading and coding. Eventually, you will start to recognize some
type of pattern you can do to solve a problem.

mrnutty 761 Senior Poster

How are you creating the tile map? A 2d Array?

mrnutty 761 Senior Poster

Generally :

declare var v1
declare stringstream converter
declare var2 v2
converter << v1 //put variable 1 into stream, whether it be int,long,string...
converter >> v2 //convert var1, into int,long,float,string or whatever type var2 is.
mrnutty 761 Senior Poster

1) Write code.
2) Write more code.
3) Read up on design patterns, whether it be wiki or whatever
4) goto 1 until comfortable
5) Now every time you write code, you can either :
5a) Finish the code, and the project ( depending on its size) then take a step back
and see how you can better the system, decouple it, give each class its minimal
job.
5b) Or take a step back first, and think about how each class interacts with the
other and then how all class interacts to make the program. This is where UML
comes in. It provides a visual
6) Repeat all of this procedure for like your whole life, and eventually you'll get
to be a better programmer.

mrnutty 761 Senior Poster

>> why Turbo C++ is still the dominant compiler in universities that still teach C++

[citation?]

And, if you are using C++, then definitely use C++ containers. else if you are using C, then don't use C++ containers , lol.

mrnutty 761 Senior Poster

The problem is you separate the definition of printVector. Your compiler can't handle
defining a template function somewhere else than its declared file. So do this instead.

//myTools.h
template<class Iterator>
static void print(Iterator begin, Iterator end){
 while(begin != end){ cout << *begin++ << " "; };
}

Better yet, use your own namespace.

namespace MyTools{

template<class Iterator>
 void print(Iterator begin, Iterator end){
   while(begin != end){ cout << *begin++ << " "; };
 } 

 template<typename T>
 print(T arg){ cout << arg ; }
 
 template<class Iterator>
 println(T arg){ cout << arg << endl; }
 
 //more tools
}

Then you can use it like this :

#include "MyTools.h"
int main(){
 MyTools::print("hello");
 vector<int> vec(4,4);
 MyTools::print(vec.begin(),vec.end());
 
 using namespace MyTools;
 print("world");
 print(vec.begin(),vec.end());

 return 0;
}
mrnutty 761 Senior Poster

The sphere could probably be drawing over your polygons. But, i'll have to see your
code first.