twomers 408 Posting Virtuoso

I doubt you'll get any code for free here. So here's an outline of how you might go about it.
Grab the input from the user and compare strcmp() it against references, i.e. "January", etc (might want to consider how you'd work with upper and lower case letters).
Then grab the year and see if it's a leap year (only necessary if the month is Feb though.

Have a go at trying this and get back to us with problems.

twomers 408 Posting Virtuoso

As I said in post #4 :rolleyes:

twomers 408 Posting Virtuoso

Does that work?
I think the limits of the loops need work, and that you need to consider the input=1 case, but it looks fine. ANd yer missing a ; in places. And you don't need the else. And you have a ; where you don't need one (end of the first for line).

twomers 408 Posting Virtuoso

Sorta. But you don't want to nest the loops. printf() after each for loop.
for ( counting up )
printf();

for ( counting down )
printf()
The second loop will be going from the upper limit to 1, remember. So limits of count=n; count>=1; count-- would make more sense.

twomers 408 Posting Virtuoso

I added the cin arguments you mentioned and still nothing. When I say, 'nothing', no error comes up, just the 'Form1' box comes up with nothing in it. I know it must be something basic that I'm missing. I just want to see the output of my basic program just to know the program's working. I'm using Windows7 and Borland C++ with the latest updates. If it would be easier to help me, I've also got Visual Studio 2008 installed too.

Try it with VS '08 so. Same code. Though get rid of the .h from the include files.

twomers 408 Posting Virtuoso

Ok. Consider using two for loops. One to count up to a number and one to count down. Print the looping variable.

twomers 408 Posting Virtuoso

That's curious.
Try doing \r\n instead. Not sure if that'll help, to be honest, but worth a try.
Can you get a hex editor and look at the hex of the file and see if the \n character is there? It could just be your editor.

twomers 408 Posting Virtuoso

Well, what to you have so far? Code wise?

twomers 408 Posting Virtuoso

You're probably using an old compiler. Might do good to update it.
Try putting a cin.get() and cin.ignore() before the return 0 in main(). Does the console window disappear, or does nothing turn up?

twomers 408 Posting Virtuoso
twomers 408 Posting Virtuoso

ANd the problem is?

twomers 408 Posting Virtuoso

Code?

twomers 408 Posting Virtuoso

>> My question is how can I use a member function of C at D and at D's child classes?
I don't understand that sentence. Can you elaborate or show what you mean with a simple example that shows what you want to do even if it doesn't work?

twomers 408 Posting Virtuoso

What do you need help with? Specifically?

twomers 408 Posting Virtuoso

Someone asked something similar before in another forum and you could do something like this.

class thing {
private:
  typedef int (*_fptr)(int,int); // Typedef this for readibility of functino pointer
  std::map<std::string, _fptr> mfp; // Associate a string to a function pointer

public:
  void add( std::string id, _fptr f ) {
    mfp[id] = f; // Add a function pointer (shuld check if the function already exists)
  }

  int call(std::string func, int l, int r){
    return mfp[func]( l, r ); // Call the function (should check if function already exists)
  }

};

int add( int l, int r ) {
  return l+r;
}
int sub( int l, int r ) {
  return l-r;
}
// Simple functions

int main( void ) {
  thing t;
  t.add("add",add);
  t.add("sub",sub);// Add the functions to the class

  int l(5), r(3);
  std::string func;
  std::cout<< "Enter function (add,sub): "; // Ask for the function that's to be called
  std::cin >> func;
  std::cout<< t.call(func,l,r) << "\n"; // Call it
  
  return 0;
}

Maybe it's too much, maybe not... but I think it does what you want.

twomers 408 Posting Virtuoso

You could have it so the constructor requires a flag to ID the allocated item... so something like...

class base {
public: 
  enum derrivableIDs {
    idBase,
    idOne,
    idTwo
  };

protected:
  derrivableIDs id;

public:
  base() : id(idBase) {
  }



  derrivableIDs getID( void ) {
    return id;
  }
};

class one : public base {
public:
  one( void ) {
    id = base::idOne;
  }
};

class two : public base {
public:
  two( void ) {
    id = base::idTwo;
  }
};

int main( void )
{
  base *o = new one;
  base *t = new two;

  std::cout<< o->getID() << "\n";
  std::cout<< t->getID() << "\n";

  delete o;
  delete t;
  
  return 0;
}

And then test against getID() for the type of monster.

twomers 408 Posting Virtuoso

Hmm. Hard to say without seeing code, really. You could always using namespace namespaceOne::namespaceTwo; if you wanted to hide the thing.

twomers 408 Posting Virtuoso

Curious. You should use std::string s really...

std::string strItoA(int number) {
   std::ostringstream sin;
   sin << number;
   return sin.str();
}

Though Narue may tell you to generalise (which is a good idea, even though she spells it wrong ;)) which is where templates become your friend.

twomers 408 Posting Virtuoso

Consider what happens if all the numbers in the array are negative. Then the if(a[i]>max) will never return true. There are two ways you can do this. Either set max to INT_MIN or set max to a[0] and loop from 1 to n then.

Salem commented: Beaten :) +36
twomers 408 Posting Virtuoso

I said take it out of the loop. In your code it's in the loop in the function random and it's in the loop in main. It shouldn't be in either. Take it out from both of those and seed rand once in main.

Seeding it with a time value that's a number in seconds, I think, will not give you a good random number as each time it's being seeded by the same value so it'll give the same output.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

void random(int array[], int max);  // No repeats :D

int main()
{
	int a[6];

		srand(time(NULL));

            for (int i = 0; i < 10; i++)
	{
		random(a,40);

		for (int i = 0; i < 6; i++)
		{
			cout << a[i] << " ";
		}

		cout << endl;
	}

	return 0;
}

void random(int array[], int max) {

	int n;
	int match = 0;     // check if any numbers are repeated

	for (int i = 0; i < 6; i++)
	{
		n = (rand()%max)+1;
		for (int j=0; j<6; j++) {
			if (n == array[j]) {
				match++;
			}
		}
		if (match==0) {
			array[i] = n;
		}
		else { array[i] = (n+rand()%max); }

		match = 0;
	}
}
twomers 408 Posting Virtuoso
twomers 408 Posting Virtuoso

I go for a grazing cammel
double playerLifeForce;

twomers 408 Posting Virtuoso

http://www.sqlite.org/ Never used it, but have heard people speak good about it.

twomers 408 Posting Virtuoso

No.

Everyone thinks the one they use is the best. Do something that makes sense. Nobody will complain too much so long as it's sensible.

Dave Sinkula commented: Of course mine is best. :D +24
twomers 408 Posting Virtuoso

Haven't done template programming in a while, but as far as I remember you only need to specialise a function rather than the entire class. I think this should work, if not it should give you a good idea how to do what you want...

template <typename ty>
class thing {
public:
  void one();

};

template<typename ty>
void thing<ty>::one() {
  std::cout<< "thing<ty>::one() called...\n";
}

template<>
void thing<bool>::one() {
  std::cout<< "thing<bool>::one() called...\n";
}

int main( void ) {
  thing<bool> boolThing;
  thing<int> intThing;

  boolThing.one();
  intThing.one();

  return 0;
}
twomers 408 Posting Virtuoso

I was thinking that too, but figured he wanted to ... transpose it for his own reasons.

twomers 408 Posting Virtuoso

First consider not globalising all the variables and consider passing them by pointer or something from main().

Second, the line should be stringarray[j][0] = string[j]; That should be it, methinks. Work on indentation too, perhaps.

twomers 408 Posting Virtuoso

I'd imagine if you were to wait in the for loop for about a second per iteration it'd be different.

The problem is that you're reseeding the rand function so quickly it's being reseeded off the same value. You only need to seed it once, so do it at the start of main.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>


using namespace std;

int random(int max)
{

	return ((rand()%max)+1);
}

int main() 
{

	srand(time(NULL));
for (int i=0; i<6; i++) {
		used_nos[i] = random(40);
		cout << used_nos[i] << endl;
	}

return 0;
}

For further clarification... srand takes time(null) in your code, which updates every second, I believe. So if srand was in the function and if the loop executed for over a second you'd get different numbers out every different second.

twomers 408 Posting Virtuoso

http://www.codeguru.com/forum/showthread.php?t=302806
First hit on google.
Basically, it extracts segments from a varaible, i.e. the higher or lower 16 bits.

twomers 408 Posting Virtuoso

You should read about getline here, http://www.cplusplus.com/reference/iostream/istream/getline/

Personally, I'd use std::string-s and getline...

#include <string>
#include <fstream>
#include <iostream>

int main() {
  std::ifstream inFileStream( "filename.txt" );
  std::string line, file;

  while( std::getline(inFileStream,line) )
    file += line + "\n";

  std::cout<< file;

  return 0;
}
tux4life commented: Agreed :) +16
twomers 408 Posting Virtuoso

unsigned int val = (unsigned int)array[0]; // For the value 1

twomers 408 Posting Virtuoso

Should try the typical FAQs.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044844545&id=1043284392
You don't need conio for gotoxy, if it's the gotoxy I was thinking of.

twomers 408 Posting Virtuoso

MessageBox(0,"Message body", "Message title", MB_OK ); might be what you're looking for. #include <windows.h> to use it.

twomers 408 Posting Virtuoso
twomers 408 Posting Virtuoso

Read here http://www.adrianxw.dk/SoftwareSite/FindFirstFile/FindFirstFile1.html (and part 2 and 3), and in the retrieving loop you could simply have a counter that's returned by the function.

Creator07 commented: Thanks +1
twomers 408 Posting Virtuoso

No. You'll have to write a cyclic shift macros or function. ie, something like

#define cyclicShiftRight(val,shiftBy) ...code here...
#define cyclicShiftLeft(val,shiftBy) ...code here...
//...
char num = 0x80; //1000-0000
cyclicShiftRight/Left(num,3); //0000-0100
twomers 408 Posting Virtuoso

Do it more like this:

char fileName[30];
printf( "Enter filename: " );
scanf( "%s", fileName );
FILE *pFile;
pFile = fopen( fileName, "r" );
... etc
fclose( fFile );

Though technically this is C++ so you should use fstreams, really.

twomers 408 Posting Virtuoso

At a guess I'd say that fileHandle = fopen(Name,"w"); should be changed to "a" or something. If the function's being called more than once you'll want to append data to the file rather than write to a clean file, right?

twomers 408 Posting Virtuoso

Nah. You'll probably have to specialise the template, I'd say. Or overload a > operator, if you feel like it.

twomers 408 Posting Virtuoso

ShowWindow( GetDlgItem(hwnd,dialogID), SW_HIDE );

twomers 408 Posting Virtuoso

You can do what's called 'template specialisation' which will implement certain functions for a specific type, basically.

template<typename ty> 
class myClass {
private:
  ty var;

public:
  myClass( ty i );
  void print( void );
};

template<typename ty>
myClass<ty>::myClass( ty i ) 
: var(i) {
}

template<>
void myClass<char>::print() {
  std::cout<< "Printint char: " << var << "\n";
}
template<>
void myClass<int>::print() {
  std::cout<< "Printing int: " << var << "\n";
}
template<typename ty>
void myClass<ty>::print() {
  std::cout<< "Printing ty: " << var << "\n";
}


int main( void ) {
  myClass<char> mych(42);
  myClass<int>  myint(43);
  myClass<double> mydoub(44);

  mych.print();
  myint.print();
  mydoub.print();

  return 0;
}

You could always use templates again for the second solution...

template<int graphSize>
struct graph2d {
  int id;
  bool j[graphSize][graphSize];
};

int main( void ) {
  std::cout<< sizeof(graph2d<12>) << "\n";
  std::cout<< sizeof(graph2d<52>) << "\n";

  return 0;
}
twomers 408 Posting Virtuoso

Nope.

class thing {
private:
  std::string str;

public:
  thing(std::string _str) 
    : str(_str) {
  }
  std::string &getAttribute() {
    return str;
  }
};
int main(int argc, char* argv[]) {
  thing inst("one");
  std::cout<< inst.getAttribute() << "\n";

  inst.getAttribute() = "two";
  std::cout<< inst.getAttribute() << "\n";

  thing *ptr = &inst;
  ptr->getAttribute() = "three";
  std::cout<< inst.getAttribute() << "\n";

  return 0;
}

Because getAttribute() returns the address of str if means you can assign variables to it using the = operator, as above.

twomers 408 Posting Virtuoso

aa is a pointer so you have to either dynamically allocate an instance of a, or set it to the address of an already instantiated instance, and call aa->getAttribute(); . Alternatively do

aa a;
a.getAttribute();
twomers 408 Posting Virtuoso

I'd've thought if you were comparing binary data the memcmp function would have taken const void* 's rather than any kind of char* abstraction.

twomers 408 Posting Virtuoso

>> Not for long -- he's been banned again too And this time probably permanently.
Heh. What for this time? There's normally a (n amusing) good reason.

twomers 408 Posting Virtuoso

Hey Josh. Yeh back again?

twomers 408 Posting Virtuoso

Leave the computer idle until it's automatically put on.

Didn't look closely at this but it looks like it might be useful. http://www.dreamincode.net/forums/showtopic17214.htm

twomers 408 Posting Virtuoso

Why are you flushing so much? The problem is that you can't directly print an array. You have to print from both indices, so

for(k=0;k<3;k++)
{
  for(l=0;l<3;l++)
  {
    mat[k][l]=(mat1[k][l]+mat2[k][l]);
    cout<<" "<<(mat[k][l]);
  }
  cout<<"\n";
}

Also, iostream.h isn't standard. <iostream> is. Which compiler are you using?

twomers 408 Posting Virtuoso

You could do it (quite unstandardly) via

#include <iostream>
#include <string>

// Non-standard library
#include <conio.h>

int main() {
  std::string password;

  std::cout<< "Enter password: ";
  for( char ch=getch(); ch != 0x0D; ch=getch() ) {
    password += ch;
    std::cout<< "*";
  }
  std::cout<< "\nThe password is: " << password << "\n";

  return 0;
}

0x0D is hex for return, I think.

twomers 408 Posting Virtuoso

>> You could even pre-initialize the map in memory with default values before attempting to read the file.
Yeah. I set the values of the map members to "" before reading the values. So I don't have to check they exist. I also have something which specifies whether the variables are necessary or optional and call a verification function before proceeding.

>> Where does the settings.txt file end up?.
Same directory as the executable. You can write a function to save the elements too using iterators and the like.