CoolGamer48 65 Posting Pro in Training

What exactly is mouse handling? Do you mean reading mouse input? DirectInput is an expensive solution - there may be some simpler methods in the Win32 API.

CoolGamer48 65 Posting Pro in Training

This: module = new MyClass(true); line should not work in either case. module is of type MyClass, but new MyClass(true) is of type MyClass*. That assignment shouldn't work - are you sure the latter example compiles?

Also, use [code]

[/code] tags, not [quote][/quote] tags.

CoolGamer48 65 Posting Pro in Training

Thanks for the help, but could you please expand on point number 2. ArkM. What is my text database file?

A text database file is just your text file.

CoolGamer48 65 Posting Pro in Training

( x || y ) is an expression of type bool (at least I think so - even though you can't declare variables of type bool it still exists as a type) true is of type int. I believe this comparisan of int to bool is causing the issue (though the error you gave would suggest otherwise, since there is no assignment in the example above)

simply remove the == true. It's unnecessary, not just in this case but in all cases. (x || y) is already of type bool. there is no need to add the == to make it a bool.

CoolGamer48 65 Posting Pro in Training

For reading data from text files: ifstream

CoolGamer48 65 Posting Pro in Training

It is considered one of the "harder" languages, but as AncientDragon said, hard is a relative term. C++ was really the first language I learned, and I didn't have any real experience with other languages. So if you want to learn it, don't hold back because it's "hard".

CoolGamer48 65 Posting Pro in Training

I meant in a job - not in school.

CoolGamer48 65 Posting Pro in Training

As others have said - there is no change in syntax when using an API. You need to learn about C++ before you learn how to perform specific output with it (ie, a GUI). However, its very difficult to learn C++ when you have no means of outputting data. That's why you do console apps. It's very easy to perform simple I/O with the cin and cout objects.

CoolGamer48 65 Posting Pro in Training

Why do you use constant? Does it improve performance?

CoolGamer48 65 Posting Pro in Training

#2:

if you don't know how to do that, use ifstream to read from files and ofstream to write to them (or just fstream for both).

Quick example:

#include <fstream>
using namespace std;

int main()
{
    int data = 5;
    ofstream fout("myfile.txt");
    fout << data;
    fout.close();
    
    int moreData;
    ifstream fin("myotherfile.txt");
    fin >> moreData;
    fin.close()

    return 0;
}
CoolGamer48 65 Posting Pro in Training

I was looking for something a bit more programmer-oriented.

edit: well, I guess aspiring programmer oriented

CoolGamer48 65 Posting Pro in Training

@OP

Post some code. And use [code]

[/code] tags when you do so.

CoolGamer48 65 Posting Pro in Training

That's more of a math question than a C++ question.

At any rate, I believe it has something to do with the Pythagorean theorem. The 2D distance formula is sqrt((x2-x1)^2 + (y2-y1)^2) = D. I'm unsure what the 3D equation is.

CoolGamer48 65 Posting Pro in Training

Hey.

I've never worked in industry, and I was just curious as to how exactly things work in the real world (as a C++ programmer). To be a bit more specific, what kind of things do you do in a day? If you're working on a program, how many people are working with you? How exactly does working with other people on a single program work? What kind of "assignments" (sorry if that's the wrong word, I'm still in school) do you get? Around how much work do you get done in a day?

I'm not necessarily looking for concrete answers to all those questions - just some input from people who do work in industry.

CoolGamer48 65 Posting Pro in Training

*sigh*

Okay, you want to know where to start. I've never written an AVI loader - but here are a couple of places to start (and I don't think where you start is going to be language specific).

  • Learn how to make a GUI. I gave you a link for a starting tutorial for the windows API. If you've got access to MFC (which I believe is just a wrapper for the API), then use that. Don't want to use the API? How about DirectX? A bit more complicated but you can make the GUI look like whatever you like. Rather use something a little more open source? OpenGL (same scenario as DirectX). You're not on Windows? What OS are you on? OpenGL still applies, but there may be other options. So, go out to Google, and try to make a GUI. If you run into specific problems, come back and tell us. Some examples: Can you guys show me the basic structure of a Windows application using the Windows API? better yet: I wrote this GUI (post the code) and it won't compile. Here are the errors: <errors here>. How can I fix it?
  • Learn to interface with SQL. Just Google it. You should get some APIs and tutorials for whatever language you want. Those should help you. If you then have specific questions about that, post them.
  • Learn to load an AVI file. This is a bit more difficult. I gave you link in my previous …
CoolGamer48 65 Posting Pro in Training

What does friend do when your outside of a class? Also, isn't only necessary to pass MyClass by reference when you're doing input?

CoolGamer48 65 Posting Pro in Training

All you're doing is defined a method of MyClass called Cout. To, use it, you would have to call it:

MyClass obj; 
obj.Cout();

the cout you use to do things like cout << x; is not a function, it's an object. Look in to operator overloading, if you haven't already.

To allow you to use the insertion (shift-left) operator (<<) with left operand of ostream (the class that cout is an object of) and right operand of MyClass, do something like this (I haven't actually run this, so it may not work):

ostream& operator<<(ostream& cout,MyClass obj)
{
     cout << "Output members here.";

    return cout;
}
CoolGamer48 65 Posting Pro in Training

I don't really think it would make sense to learn one without sort of learning the other. I mean they're mostly the same. When you begin to learn either language, you're going to cover basic flow control - if statements, loops, functions, etc. It's only when you get to classes that the differences start to come up, and I would just go ahead and learn classes. C structs are just classes sine methods. The only other major differences (according to Wikipedia) are the addition of polymorphism (tied in to classes and methods), operator overloading, multiple inheritance, templates, and exception handling.

CoolGamer48 65 Posting Pro in Training

Do you need us to lay out the steps of what you need to do? I thought you had already planned that? You want to know how to make a Windows looking GUI? Windows API or MFC, if you've bought it. Here's a quick tut on the API, don't know much about MFC. Do you want to know how to how to load an AVI file? A site I was referred to on this forum. Want to know how to interface with SQL databses? Google is your friend.

If thats all your forum has for me then I'll leave, disappointed.

This isn't a substitute for a search engine. You've given us a complex program, and are asking us what you should do. You've laid out the basics of what you want - decide to work on one of the components of your program and go look some stuff up. If you have more specific questions then, ask them. But unless someone has recently worked on an AVI loader that does the things you want and doesn't mind sharing, no one is going to go out and make one just to give you an idea of where to start.

CoolGamer48 65 Posting Pro in Training

Simple - because I must.

You must? Was it assigned to you for your job or something? I don't have any industry experience, but I'm pretty sure a decent boss wouldn't give a huge project like this to a single person with limited programming experience.

VernonDozier's suggestion of breaking up the program is smart - but you're going to have to do so much learning each step it might even be faster to just put the whole idea of your current project aside now, and work on getting the basics down.

CoolGamer48 65 Posting Pro in Training

I got it. I was pretty much on the right track with my theory. The program executes fine for all 11 tests now. If anyone cares, here is the condensed and more efficient program:

#include <fstream>
#include <string>
#include <vector>
using namespace std;

int password_length;
int num_chars;
vector<string> possible_combos;
char* chars;
bool limit_reached = false;


bool IsVowel(char c)
{
	switch(c)
	{
	case 97:
	case 101:
	case 105:
	case 111:
	case 117:
		return true;
	}

	return false;
}

bool HasVowel(string str)
{
	int length = str.length();
	for(int i = 0;i < length;i++)
	{
		if(IsVowel(str[i]))
			return true;
	}

	return false;
}

bool HasTwoConsonants(string str)
{
	int numConsonants = 0;
	
	int length = str.length();
	for(int i = 0;i < length;i++)
	{
		if(!IsVowel(str[i]))
			numConsonants++;
	}

	if(numConsonants >= 2)
		return true;

	return false;
}

void FindAll(string current,int last_char_id)
{
	if(limit_reached)
		return;

	if(current.length() == password_length)
	{
		if(!HasVowel(current))
			return;
		if(!HasTwoConsonants(current))
			return;
		
		possible_combos.push_back(current);
		if(possible_combos.size() == 25000)
			limit_reached = true;
		return;
	}

	for(int i = last_char_id+1; i < num_chars;i++)//check first part of the for
	{
		FindAll(current + chars[i],i);
	}
}

int main()
{
	ifstream fin("passwd.in");
	fin >> password_length >> num_chars;
	chars = new char[num_chars];
	for(int i = 0;i < num_chars;i++)
		fin >> chars[i];

	fin.close();

	for(int i = 1;i < num_chars;i++)
	{
		if(int(chars[i-1]) > int(chars[i]))
		{
			char temp = chars[i-1];
			chars[i-1] = chars[i];
			chars[i] = temp;
			for(int j = i - 1;j > 0;j--)
			{
				if(int(chars[j-1]) > int(chars[j]))
				{
					temp = chars[j-1];
					chars[j-1] = chars[j];
					chars[j] = temp;
				}
				else
					break;
			}
		}
	}

	for(int i = 0;i < …
CoolGamer48 65 Posting Pro in Training

Question: Can you have more than one of the same character or must all characters be unique? If you can have multiple characters (i.e. aaert), that's a harder program, I would imagine. Or must all characters be unique (i.e. aert)?

I thought of that too (while I was out), and I thought of a possible solution. I just checked, and yes, the chars given in the input must be unique. It gave me an idea:

Right after you get the input chars, alphabetize them. Then, go to the first char, and cycle through all the possible combos, but stop if you find that any of the chars you are adding to the string make the string not be in alphabetical order. That will make each output be in alphabetical order, and the outputs themselves will be organized alphabetically.

That logic might be slightly flawed, I just spurred it out. As I'm implementing it I might see any flaws in it, but I think I'm on the right track.

Was this what you were thinking of?

CoolGamer48 65 Posting Pro in Training

Alright, I added the validations before I store the combo in a vector, and that did get rid of the bad_alloc. However, now I'm having issues with runtime. All of the tests that failed before still all fail, it's just that now they fail due their runtime passing one second. I'll try to make the program more efficient as far as runtime, but I still think that there's something up with the brute-force logic I have in place. I can't imagine that one of the tests would take 40+ mins. while others would take fractions of seconds. Though perhaps that 's because I'm not used to these types of problems. I'll look into both improving my logic and seeing if there are issues with it as it is currently implemented.

CoolGamer48 65 Posting Pro in Training

I got home recently, and I'll try to make the program more memory efficient, but I just let the program run for 40 mins with one of the failed sets of test data, and it didn't get past the portion where it finds all the possible combinations you can have, so I'm thinking there may be some other issue, but I'll try to make it less of a brute-force and see what happens.

Here's the code, if anyone cares to read it:

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

int password_length;
int num_chars;
vector<string> possible_combos;
char* chars;

void FindAll(string current,vector<int> char_ids_used)
{
	if(current.length() == password_length)
	{
		possible_combos.push_back(current);
		return;
	}

	for(int i = 0; i < num_chars;i++)
	{
		bool used = false;
		for(int j = 0;j < char_ids_used.size();j++)
		{
			if(i == char_ids_used[j])
				used = true;
		}
		if(!used)
		{
			vector<int> my_used = char_ids_used;
			my_used.push_back(i);
			FindAll(current + chars[i],my_used);
		}
	}
}

bool IsVowel(char c)
{
	switch(c)
	{
	case 97:
	case 101:
	case 105:
	case 111:
	case 117:
		return true;
	}

	return false;
}

bool HasVowel(string str)
{
	int length = str.length();
	for(int i = 0;i < length;i++)
	{
		if(IsVowel(str[i]))
			return true;
	}

	return false;
}

bool HasTwoConsonants(string str)
{
	int numConsonants = 0;
	
	int length = str.length();
	for(int i = 0;i < length;i++)
	{
		if(!IsVowel(str[i]))
			numConsonants++;
	}

	if(numConsonants >= 2)
		return true;

	return false;
}

bool InAlphaOrder(string str)
{
	for(int i = 1;i < str.length();i++)
	{
		char prev = str[i-1];
		char current = str[i];
		if(int(prev) > int(current)) …
CoolGamer48 65 Posting Pro in Training

I would surmise you're using a brute force solution when perhaps a dynamic solution exists.

This is quite common on online contests such as topcoder etc.

My solution may be a brute force - I'm not entirety sure. I would have preferred to think of the algorithm on my own - but this is the basic structure of the problem/program, is it a brute force?

The problem:
You're given x number of lowercase characters and a password length. You need to find all the valid passwords you can construct from the given letters.
valid passwords:
- have at least one vowel
- have at least two consonants
- have characters in alphabetical order, ie: abc is ok, bac, or cab, is not

You need to then alphabetize the results you get and print up to 25000 of them. Here's how I go about it (I'm away from home so I don't have the code with me).

Find all the possible combos given the letters.

Weed out the incorrect ones

Alphabetize what's left.

Output up to 25000

Is this a brute force method? If so, what would be a possible dynamic method (if you can sort of hint at the answer or put me on the right track without giving me the answer, that'd be nice).

CoolGamer48 65 Posting Pro in Training

Just because IBM says something is wrong doesn't mean VC++ agrees. Heck, VC++ could make billybob a primitive data type and still call itself a C++ compiler (though few others would agree - and that's really what matters). Whatever is in the ISO standard for C++ is what is allowed. Implementations of C++ can bend the rules if they wish, no one can stop them.

As to whether the ISO standard says references to pointers to references are allowed, I don't know.

Also, use [code] code here [/code] tags.

CoolGamer48 65 Posting Pro in Training

I'm writing a program for a contest (it's a demo, not the actual thing - so I'm not cheating by asking for help - and my question isn't directly related to the algorithm they want anyway). To submit a program, you send them the .cpp file, and they execute it on their server (automatically) for a number of test trials (around 10), and show you the results. This problem has 11 trials. For the first five trials, the program runs fine. However, the sixth run of the program fails, and it throws std::bad_alloc (I'm guessing its caused by vector::push_back - the only other things I'm using from std are file streams and strings). Then, trial 7 works fine. Trials 8 and 9 failed, same reason. Trial 10's runtime was too long (it was stopped at around 1.5 secs, shouldn't go over 1 second), and trial 11 had the bad_alloc again.

I wanted to see exactly where the bad_alloc was occurring, so I copied the test input data for trial 6 into the test input file on my machine, and ran the program. I didn't get bad_alloc, but the program didn't end (for the time I ran it, maybe 10 seconds - I'm going to try leaving it for longer while outputting debug info).

Anyway - this is my assumption of what's going on, tell me if I'm right:
On the virtual machine on the competition's servers, the program is only given the amount of memory that they …

Salem commented: Yes, your analysis seems correct. +20
CoolGamer48 65 Posting Pro in Training

Storing values before allocating memory for 'em? I will be surprised if it works.

Oh, right. Sorry.

records** myarray;
myarray = new records*[numRecords];
for(int i = 0;i < numRecords;i++)
    myarray[i] = new records;
(*myarray[2]).title = "w/e";//same as...
myarray[2]->title = "w/e";
CoolGamer48 65 Posting Pro in Training
records* myarray;
myarray = new records[numRecords];
myarray[i].title = "w/e";

or, you you mean:

records** myarray;
myarray = new records*[numRecords];
(*myarray[i]).title = "w/e";//same as...
myarray[i]->title = "w/e";

x[y] is equivalent to *(x + y) . If that's confusing, read "Pointers and Arrays" in this article

Basically, in the first example, [] is already dereferencing the pointer, so you can't dereference it again with * or ->.

CoolGamer48 65 Posting Pro in Training

link, if you're on windows

CoolGamer48 65 Posting Pro in Training

Do you mean manually deleting data at an address you chose? Or are you just asking what delete does?

int* x;//declare a pointer to an it
x = new int;//create an int somewhere in memory and store its address in x
*x;//access the actual int stored at the location that x has
delete x;//delete the int stored at the location that x has
CoolGamer48 65 Posting Pro in Training

As was mentioned in one of the links, you can #define something with no value. The only real use for this is to use the symbol in preprocessor if-statements. One example is the #ifndef code blocks that sparked this discussion.

file1.h

#ifndef FILE_1
#define FILE_1
//the symbol FILE_1 now has a status of being defined, though it has no value

//class and function declarations, etc.
#endif

file2.cpp

//FILE_1 not defined
#include "file1.h"//code executed
//FILE_1 defined
#include "file1.h"//code not executed twice
CoolGamer48 65 Posting Pro in Training

Look here

I once read an article that said to use cin.get(). What's wrong with that?

CoolGamer48 65 Posting Pro in Training

Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears. What are #ifndef and #define, and what's #endif? It works, thanks. But what are these three lines of code?

Why should it (stay open)? Unless you ask for input, the program will do all the code you ask it to, outputting everything, and once it's done (either when it reachers return 0; or the end-bracket of main) it will close. #define 'new symbol' 'previously known symbol' will make 'new symbol' and 'previously known symbol' look the same to the compiler. #ifndef 'symbol' checks if 'symbol' is #defined. If so, it will execute all the code after it until it reached something that tells it to stop, like #endif

CoolGamer48 65 Posting Pro in Training

Another quick question though, the Node myNode; does this contain a reference (in the same way Java would) to the object?

No. myNode is a Node, just like with int x; : x is an int.

CoolGamer48 65 Posting Pro in Training

Don't confuse Java new with C++ new. I don't know Java too well, but new in Java does something else. In Java, variables with primitive types (int, boolean, float, etc.) can be simply declared: int x; boolean b; . Same for C++. However, in Java, when you declare a variable with a non-primitive data type, you aren't actually creating an object of that class, you're creating a reference to it (similar in certain aspects of it's behavior, but not the same as, a pointer). This is the only way to declare objects of classes. The reference must be filled with an actual object using new. Similarly, primitives can't be declared as references (or so I believe). So this: x = new int; will never happen.

In C++, variables of any type, primitive or not, can be directly declared: int x; myclass y; , and you can declare a pointer to any type: int* a; myclass* b; . Now, a and b don't hold objects (nor will they ever). They hold the memory address of objects.

One thing you can do is this:

a = new int;
b = new myclass;

Here, new creates a variable somewhere in memory and returns its address. Now, when you create a variable with C++'s new, you have control of when that object is deleted. In Java (again, I believe), all variables are deleted at the end of scope, regardless of whether they were created by new. In C++, variables created by …

CoolGamer48 65 Posting Pro in Training

One very evident difference between the two myNodes is that the second one will be deleted at the end of scope, while the pointer will continue to exist until you manually delete it. Well, that wasn't exactly true. The pointer myNode will be deleted at the end of scope, but pointer myNode doesn't actually hold a node, it holds the address of one. The node at which the pointer myNode points at is what will continue to exist until you delete it.

There are many other differences as well, though whether or not you care about them will depend on your program's intent. For example, myNode can contain the address of another node:

Node x;
Node* y;
y = &x;

//these two calls refer to the same method of the same object
//not two separate objects of the same class
y->Member();
x.Member();
CoolGamer48 65 Posting Pro in Training

I think what he's referring to in #1 is a NULL pointer:

int* x = NULL;
//or
int* x = 0;

I'm guessing this is what is meant by #3:

int x[10] = {0};
x[3] = 5;
x[4] = 13;
int* pInt = (&x[3])+1;

*pInt would be 13

CoolGamer48 65 Posting Pro in Training

@VernonDozier
I was talking to the OP about the curly bracket thing and everything following that, not you (sorry if that was unclear).

As I said, "this method works in this situation". I was just giving him some tips for condensing if-else chains in other, hypothetical situations where an array wouldn't solve his problem.

Also, I didn't see your second post while I was posting, so sorry for bringing up the out-of-bounds thing again.

CoolGamer48 65 Posting Pro in Training

>>numInstances[7] would be the number of times 7 showed up
shouldn't numInstances[6] be the number of times 7 was rolled? Like n1337 said, you should do:

numInstances[sum-1]++;

otherwise, you'd be wasting numInstances[0], and when you roll a 12, you'd have nowhere to put it (since the highest index of the array is 11)

Also - this method works in this situation, but just some pointers (not int* x; pointers):

when an if statement has only one line of code, you don't need curly brackets. So you can do this:

if(sum==1)
    ones+=1;

Also, you could use a switch statement:

switch(sum)
{
case 1:
    ones+=1;
    break;
case 2:
    twos+=1;
    break;
case 3:
   //...
}
CoolGamer48 65 Posting Pro in Training

@second error
you wrote itsVectorOfNotebook in one or more places (one of which is line 135 of CTesterClass.cpp) where you should have written itsVectorOfNotebooks .

Could you specify which line is 68 in CTestClass.cpp? The line numbers are different since you bundled .h and .cpp files.

CoolGamer48 65 Posting Pro in Training

The only insert function of the vector template I found takes two parameters - one of type size_t, and the other of type Item_type. When you call it, you're only specifying the Item_type parameter.

Also, I believe the entire purpose of the VectorOfNotebooks class is to not have to use the template directly. There is no added functionality. Use typdefs instead.

typedef CVectorTemplate<CNotebook> VectorOfNotebooks;
CoolGamer48 65 Posting Pro in Training

I guess you could classify DirectX as that. OpenGL, on the other hand, is multi-platform, so it can't always be using the Windows API. I'm not sure how OpenGL handles being multi-platform. Perhaps the different versions of OpenGL for different platforms are coded slightly differently internally. I can't say for sure.

CoolGamer48 65 Posting Pro in Training

To my knowledge, both DirectX and OpenGL are written with C or C++. You could write an API like them yourself using C++. On Windows, you can use SetPixel() to set a pixel on a screen. I believe that's one of the most basic graphical functions you can use (in the Windows API). Then it's up to you to write bitmap loaders and the like.

CoolGamer48 65 Posting Pro in Training

Thanks. That did it. :)

CoolGamer48 65 Posting Pro in Training

Thanks, though I'm still having some problems. Does land now need to be a double pointer? I finally got the code to compile, but then I ran into a write error.

LandCondition** land;

//...

*land = new LandCondition[IMG_WIDTH];//runtime error

//...

for(int i = 0;i < IMG_WIDTH;i++)
	land[i] = new LandCondition[IMG_HEIGHT];

It's complaining that it can't write to location 0x0000000, which makes sense I guess, but I don't know how to resolve this.

CoolGamer48 65 Posting Pro in Training

I'm having problems creating an array with a dynamic size using new.

Here is the relevant code:

enum LandCondition
{
	Bad,
	Good
};

//...

int IMG_WIDTH, IMG_HEIGHT;
LandCondition* land;

//...

land = new LandCondition[IMG_WIDTH][IMG_HEIGHT];//causes errors

The errors VC++ gives me are:
error C2540: non-constant expression as array bound
error C2440: '=' : cannot convert from 'LandCondition (*)[1]' to 'LandCondition *'

What's causing this? I've done things similar to this before without issues.

CoolGamer48 65 Posting Pro in Training

i don't want to use OpenGL in dev !

Ancient Dragon said to look at the source for OpenGL (one of the nice things about having an open source graphics API) - not to actually use it. But I think that would be a bit complicated. I certainly wouldn't feel confident looking through the source for OpenGL trying to find code for a bitmap loader. Then again, I'm not sure how easy it would be to find a tutorial for a bitmap loader.

CoolGamer48 65 Posting Pro in Training

Hmm I guess I was just trying to continue as I had been before i.e int* a; a=&b; Then using b unless I want to send it into a function or something but I guess this doesn't carry over in this case.

I'm not following exactly...

a contains the address of b. modifying b is the same as modifying *a, until a is resigned to another address, or b goes out of scope (or a goes out of scope)

Just to check I have got the right end of the stick, you are saying it is best to just define a pointer and continually dereference it every time? There is no "normal" variable defined at the memory address of the pointer.

I'm not sure what you mean by "normal variable". Do you mean a primitive data type (i.e., int, float, double, char, short, long, etc.)? If so, there can definitely be a normal variable at the address of a pointer:

int* x = new int;//there is an int at the memory address of int
*x = 5;
//work with x, even past ends of scope
delete x;//you need to manually free the int, C++ won't do it for you when you use new

the value that x itself contains is a a memory address. you should only use x without the dereference operator if you're dealing with another pointer

int y = new int;
*y = 6;
delete x;//assuming x wasn't already deleted
x = y;//no …
CoolGamer48 65 Posting Pro in Training

Think of the Harry Potter Games. They make a new one almost every year, but it is also made by one of the biggest game producing companies in the world, with almost unlimited resources, programmers, level designers, artists, sound technitions, etc.

A good game would take even longer ;)

An alternative to OpenGL (if for whatever reason you don't want to use it): DirectX. I believe Ancient Dragon mentioned them earlier...