CoolGamer48 65 Posting Pro in Training

Interesting, thank you for the advice, also i have another question, whenever you make a program in c++ visual 2008 does the program always appear in command prompt?

No. Absolutely not. However (not 100% sure about this), I believe any time you have a main() function, a console will open in VC++. I ran this program:

int main()
{
}

and a console window still came up. I'm not sure if there's a way to prevent that. If you use WinMain() and create a window with the Windows API:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine,int nShowCmd)
{
}

a console won't come up.

CoolGamer48 65 Posting Pro in Training

They can also be passed as pointers, if you want to use them. Thought I believe it's considered better to use references over pointers when possible.

class MyClass
{
  // blabla
};

void foo(MyClass* obj)
{
    // class passed by reference
}

int main()
{
    MyClass myclass; // create an instance of the class
    foo(&myclass); // pass it to another function

    //or....
   MyClass* myclass = new MyClass;//create a pointer to the class and allocate an object at its location
   foo(myclass);//call the function
   delete myclass;//deallocate the object
}
CoolGamer48 65 Posting Pro in Training

Why are you doing this->Close(); ? You can just say Close(); if you're within the scope of the function, can't you? (unless you're trying to specify that you're not referring to some external Close() function, but I don't see how the compiler would think you mean that over a member of the class).

CoolGamer48 65 Posting Pro in Training

If you're on windows, a common way to do it is system("PAUSE"); . It gives a prompt like "Press any key to continue..."

CoolGamer48 65 Posting Pro in Training

Couldn't he also use pointers?

if( TTF_Init() < 0 )
   {
	   return 1;
   }

     Font* thing = new Font;
     delete thing;

   TTF_Quit();
CoolGamer48 65 Posting Pro in Training

Trying using surfaces instead of textures. link I believe that surfaces are supposed to be used for backgrounds, like the one you're using, and textures should be used for sprites, which often do have widths and heights that are powers of two.

Nick Evan commented: Good advice! +8
CoolGamer48 65 Posting Pro in Training

Oh, I see what you mean about powers of two (I thought you meant that screen sizes could only be powers of two). I believe I've loaded images whose width/height weren't powers of two, but I don't remember off hand exactly how I did it. I'll see if I can dig up the code can get back to you. You could also research this a bit: D3DX_DEFAULT_NONPOW2. That might help you out.

CoolGamer48 65 Posting Pro in Training

It's always a good idea to post the specific errors you get from the compiler.

I think you may be initializing your array incorrectly. Listing all the elements as you have done may not be the proper way to populate a multidimensional array.

void display_draw(int mega[12][6], int draw_index);

This is wrong (I think). You can't have an array as a parameter of a function (at least not the way you're doing it here). It's possible with pointers. Change the prototype (and the implementation) to:

void display_draw(int* mega, int draw_index);

Change the call to the function from display_draw(val,draw_index); to display_draw(&val,draw_index); You're also trying to refer to an element in a two dimensional array in display_draw with only one index, like mega[12] and mega[i] . I'm not sure exactly what you're trying to do, but I don't think that's the way to do it.

CoolGamer48 65 Posting Pro in Training

You may get better responses to DirectX specific questions in the Game Development forum, or in the XNA forums (it's mostly centered on using the XNA tools with C#, but there are some forums on DirectX, and I do believe it's supposed to be the official DirectX forum).

As to your problem, screen sizes are not perfect squares. The only real rule is that the ratio of width to height should be 4:3, or 16:9 for widescreen (and that the numbers are integers). So, if 1024 is your width, using algebra:

4:3 = 1024:height

1024*3 = 4*height

1024*0.75 = height = 768.

I'm not sure exactly what you're looking for, if that wasn't it. Some code would help.

CoolGamer48 65 Posting Pro in Training

Yes - std::strings are good. char* and char arrays are bad.

In case you're wondering why you can't use the == operator with char*, think about it. buff is a pointer to a char. In other words, it contains the address of a single char, and so should be compared to other addresses of chars. The line if(buff=="Asto.cfg") is seeing if "Asto.cfg" is the address of the char 'A' that begins your string. It isn't.

CoolGamer48 65 Posting Pro in Training

I'm assuming the problem is that I'm deleting the temp variables at the top of the loop, which is unnecessary, since they're being deleted at the bottom, and they're null pointers at the first iteration. Since this loop is actually within another loop, I'm guessing it'd also be a good idea to set the variables to NULL before the loop. Would this work? (I'm away from my home computer right now, so I can't try it).

CoolGamer48 65 Posting Pro in Training

>pnA = new int;
>*pnA = 5;
>delete pnA;//okay? still a null pointer? a bad pointer?
Fine here, but the only thing you can do with pnA at this point is reseat it to another address.

So, at this point, pnA still contains the old address where there is now no data?

>delete pnA;//is it a bad pointer now?
Yes, now you've done some damage.

Okay, so now, we've attempted to delete data at an address, but no data exists. pnA is now bad?

So if we did a third delete, we would get an error, since this is the first time we tried to delete a bad pointer? Or was it already a bad pointer after the first delete, in which case the second delete would cause an issue?

Can we avoid this problem by adding a <pointer name> = NULL statement after every delete? Is there any harm/risk by doing this (other than potentially being unnecessary)?

CoolGamer48 65 Posting Pro in Training

"Bad pointers" happen when you fail to initialize a pointer,

So:

int* pnNull = NULL;//null pointer
int* pnBad;//bad pointer?

manage to corrupt your memory,

How would that happen?

or try to free a pointer more than once.

Okay wait, so:

int* pnA = NULL;
delete pnA;//okay? still a null pointer? a bad pointer?
delete pnA;//is it a bad pointer now?
delete pnA;//will this cause the error I got?

//will it change if I throw a new in the mix?
pnA = new int;
*pnA = 5;
delete pnA;//okay? still a null pointer? a bad pointer?
delete pnA;//is it a bad pointer now?
delete pnA;//will this cause the error I got?
CoolGamer48 65 Posting Pro in Training

Hey,

I'm running into some issues with delete[]. I have these lines:

delete[] temp1;
delete[] temp2;

in a loop. temp1 and temp2 are pointers to std::strings. In the first iteration of the loop, temp1 and temp2 are both NULL pointers. The statements work fine. In the second iteration, the lines both still execute fine, but the the code immediately following them:

if(numAttributes > 1)
{
	temp1 = new std::string[numAttributes-1];
	temp2 = new std::string[numAttributes-1];
}

allocates arrays at temp1 and temp2.

By the third iteration, the two delete[] statements fail, and a dialog comes up saying Debug assertion failed. In the debugger, I saw that in the third iteration, temp1 and temp2 were labeled as "Bat Ptr" (which I'm assuming is bad pointer), rather than showing that they were NULL pointers ("0x000000" comes up).

Here is the bulk of the relevant code:

while(readAttributes)
		{
			numAttributes++;
			if(!ReadAttribute(atr_name,atr_value,tagtype,readAttributes))
				return 0;
			
			delete[] temp1;
			delete[] temp2;
			if(numAttributes > 1)
			{
				temp1 = new std::string[numAttributes-1];
				temp2 = new std::string[numAttributes-1];
			}
			for(int i = 0;i < numAttributes-1;i++)
			{
				temp1[i] = attributes[i];
				temp2[i] = atrValues[i];
			}
			delete[] attributes;
			delete[] atrValues;
			attributes = new std::string[numAttributes];
			atrValues = new std::string[numAttributes];
			for(int i = 0;i < (numAttributes-1);i++)
			{
				attributes[i] = temp1[i];
				atrValues[i] = temp2[i];
			}
			delete[] temp1;
			delete[] temp2;
	
			attributes[numAttributes-1] = atr_name;
			atrValues[numAttributes-1] = atr_value;
		}

So, what is the difference between NULL pointers and bad pointers, and why do the delete statements fail with the bad pointers?

CoolGamer48 65 Posting Pro in Training

well..honestly I really don't know if my code is correct..thats why I'm asking for some advice on how to do it correctly.

the numbers in the text file are separated by a space and thats how it is supposed to look when I execute the program.

I would condense/change the program to this:

int myArray[10];
ifstream myfile("numbers.txt");
for(int i = 0; i < 10;i++)
{
    myfile >> myArray[i];
    cout << myArray[i];
}

This program assumes that there are 10 integers in the file separated by spaces or newlines (or some form of whitespace), which, from what you said, there are.

CoolGamer48 65 Posting Pro in Training

error C2511: 'Contributor::Contributor(std::string,double,Contributor::gender,int)' : overloaded member function not found in 'Contributor'
see declaration of 'Contributor'

Can't say I'm 100% sure - but after looking at it a bit I'm pretty sure:

You have two enumerations. The first is gender, which is declared and defined in global scope. The second is Contributor::gender, which is declared as a private member of the Contributor class. When you declare the constructor we're dealing with in the .h file, one of it's types is gender, and since we're in the scope of Contributor, it is assumed that gender refers to Contributor::gender. However, when you define the constructor in the .cpp file, you say gender once again, only this time, you're referring to the global gender enumeration (since we're in global scope), and so the compiler complains that you're defining an overloaded method with different parameter types than any of the ones in the class declaration.

I'm assuming you only wanted one enumeration. Take the line that first declares gender (outside of the class), and move it to the top of the .cpp file, and change it from enum gender ... to enum Contributor::gender...

CoolGamer48 65 Posting Pro in Training

Err, what exactly is the error you get? From what line?

CoolGamer48 65 Posting Pro in Training

it also doesn't work. I've tried it but my outputs are some strange symbols nothing else..

Show us your file. How are the numbers separated? I would use the >> operator, not the get() method.

Also, on further review, I seem some weird things in your loop. First, you read a char from the file (using the code in the OP). Then, you enter a while loop that assigns elements 1-10 of your array to that char, so if the first char you read was '1', the entire array would be filled with 1's. Then, you take the next char in your file, and assign that char to every element in your array, and so on until the file finishes. During all of this, you'll be outputting each char in the file 10 times.

This is a bit weird though, since the output you gave isn't consistent with this prediction. Am I not seeing something, or is something else wrong?

CoolGamer48 65 Posting Pro in Training

Thanks CoolGamer48 you are a genius

No problem. It's not that hard after you do it once...

CoolGamer48 65 Posting Pro in Training

I think you are confusing terms -- oops is not the opposite of a procedural language. An "event driven" language is the opposite of procedural.

procedural: the program executed from top down with loops to redirect it. Most console programs are procedural programs.

event driven: The program executes a piece of code when some event happens, such as when you type on a keyboard, click a button, or select a menu. All MS-Windows GUI programs are event driven programs.

Event driven programs are also loop-based, right? I.e., you enter a loop that continually checks if events are occurring, and does something based on those events, and that loop goes on for the duration of the program (excluding start-up and shut-down stuff when the user begins or ends the program).

Also, C++ can create both of these types of programs, right? So would we say its event-driven since it can support that type of program, or do we say it's both? Or neither?

CoolGamer48 65 Posting Pro in Training

well i tried to do something like that with the standard headers, and wouldn't compile. And when i changed the order, it worked. also is it the same for all OS's, i wanna port my apps to linux too, and include python.

What errors did you get? What was the wrong order and what was the order that worked?

CoolGamer48 65 Posting Pro in Training

If you'd like to link to a lib through the compiler's interface rather than using #pragma, right-click on your project's name in the panel to the left, go down to properties, expand linker, go to input, and put the libraries names in the additional dependencies field, separated by spaces.

guy40az commented: thanks again !!! +2
CoolGamer48 65 Posting Pro in Training

Sorry, but the original program has exactly the same effect: see do-while loop in the original post...

The do-while loop in the original post was being used to cycle through the letters of the string to see if any of them was an 'i'. The cin >> word; statement was executed before the loop began, and will only be executed once.

CoolGamer48 65 Posting Pro in Training

Do you want an example to explain classes in English, or in C++? Or perhaps both? Is there something specific about classes you don't understand?

CoolGamer48 65 Posting Pro in Training

Data is very, very important. To try and prove otherwise wouldn't make sense. If you mean create a program that ignores a piece of data:

#include <iostream>
using namespace std;

int main()
{
    int x;
    cin >> x;
    cout << "Hello\n\n";
    cout << "We do not care about your input\n";

    return 0;
}

There's a program where the data in the variable x is not important.

If this isn't what you mean, you need to try and clarify what you want.

CoolGamer48 65 Posting Pro in Training

You may also want to consider learning the Windows API:http://winprog.org/tutorial/

CoolGamer48 65 Posting Pro in Training

Actually it would be

#define TRUE 1 
#define FALSE 0

Oh ya. Got confused with the typedefs.

CoolGamer48 65 Posting Pro in Training

This works:

#include <iostream>
using namespace std;

int x;

int& GetRef()
{
     return x;
}

int GetInt()
{
    return x;
}

int main()
{
    x = 5;
    
    cout << GetInt() << endl << GetRef() << endl;
    system("PAUSE");
    return 0;
}
CoolGamer48 65 Posting Pro in Training

Is this what you mean?

#include <iostream>
using namespace std;

class Foo
{
public:
      Foo()
      {
           cout << "Foo constructor\n";
      }
};

class SubFoo : public Foo
{
public:
      SubFoo()
      {
           cout << "SubFoo constructor\n";
      }
};

int main()
{
    Foo* myfoo = new SubFoo();
    
    return 0;
}
CoolGamer48 65 Posting Pro in Training

x is technically a placeholder for an int, therefore it can be treated as a reference variable.

Also the statements provided above were supplying input for a non-lvalue (a copy of an int, not a reference).

For example--

#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;
using std::flush;

int x, y;

int &getRef(){
    return x;
}

int getInt(){
   return y;
}

int main(){

   x = 5;
   y = 2;

   cin >> getRef(); // doable because getRef is an lvalue

   cout << x << endl; // should print out the input

   cin >> getInt(); // illegal!, trying to assign a copy of an int which is NOT an lvalue

   return 0;
}

Oh right, I see. So this would work, correct?

cout << getInt();
CoolGamer48 65 Posting Pro in Training

I learned C++ from C++ for Dummies. I enjoyed it, but I've heard bad things about it because it has typos. I did notice a couple of typos when going through it, but it was nothing I couldn't bare or make sense of.

Still, there may be better books out there.

CoolGamer48 65 Posting Pro in Training

But wait, doesn't this work?

int x;
cin >> x;

x is an int, not a reference to one. Is it different when you're dealing with function return types and just the data type of a variable?

CoolGamer48 65 Posting Pro in Training

Hmm I wonder...

typedef void VOID

=P

I'm pretty sure that exists too.

I also love this one (not sure if its in Windows API or DirectX):

#define 1 TRUE
#define 0 FALSE
typedef int BOOL;

It's just a way to make types sort of case-insensitive. It may be a bit superfluous at times, but it's a neat thing to have.

CoolGamer48 65 Posting Pro in Training

Some explanations:

Stream operator >> (and << too) returns reference to stream (that's why we may couple expressions in

cin >> first >> second;
// (cin >> first) >> second - again stream >> target

Stream classes have conversion to bool (inherited from the common ancestor). It returns true if stream is OK otherwise false). Operator && is a simple C logical AND.

So the loop will been terminated when:
cin returns false (end of input stream or i/o error)
AND
find_first_of() returns std::string::npos value (no i's in the world).

The while stmt condition follows by the template:
Do_prompt , input_OK AND word_with_i

Do_prompt is the 1st arg of a comma operator (do this and forget then do the 2nd arg).

Some correction of my prev post:
1. Of course, the literal must "iI", not 'iI'
2. Discard string word in the tail (careless copy/paste op artifact;)

Sorry if this post is duplicated: I have some Inet troubles now...

Um, so if I got that right, the loop will terminate when the input stream goes bad, or when the user inputs a string without an 'i'. This isn't what the OP's program did. His program only asked for a single string, checked if it had an 'i', gave a warning if it did, and terminated.

CoolGamer48 65 Posting Pro in Training

Depending on how much of a beginner you are, you may want to stick with console apps for a while and get a handle around pointers, classes, inheritance, and polymorphism to some degree. But, you don't necessarily have to.

If you're on Windows, this is a very good place to begin. As Duoas said, there's a way to do a background with just the Windows API, though if you want to go into more graphical applications, you can try to delve on in to Direct3D.

Alex Edwards commented: Approved =) +1
CoolGamer48 65 Posting Pro in Training

If you're running a command prompt, there really isn't a way (to my knowledge) of setting a background.

CoolGamer48 65 Posting Pro in Training

What type of program are you dealing with? A window? A console window?

Most likely, the answer will lie somewhere in either the Direct3D (Microsoft), or OpenGL (open source) APIs. Look them up.

CoolGamer48 65 Posting Pro in Training

EDIT: Dave beat me to it, but read it anyway if you like (apparently I was missing a few things).

i didnt get you!!!!

Firstly, wrap your code in CODE tags, and specify a language (i.e. [ CODE = C++ ], w/o the spaces) so that there are numbers - makes it easier to point out errors.

void mtable(int ,int);

I'm pretty sure function prototypes should be outside of other functions, though I could be wrong about that.

mtable(num,n) ;

n was declared but not defined (i.e. it wasn't initialized). It has no value. Put that line after

cout<<"\n\n\tEnter the limit";
cin>>n;
void mtable(int num,int n)

You need an opening curly bracket after this to start a function.

I believe that's it.

CoolGamer48 65 Posting Pro in Training

If you're on Windows: system("PAUSE"); may be what you're looking for, though you can't get the character that was pressed, if that's also what you wanted (wasn't sure).

CoolGamer48 65 Posting Pro in Training

Sorry, more robust code (Ctrl-Z reaction added):

string word;

while (cout << "Enter a word which includes NO i's : ",
         cin >> word &&
         word.find_first_of('iI') != string::npos
        )
        cout << "I asked for no i's!" << endl;
cout << "Thank you. Bye..." << endl;string word;

Wait, when will the loop terminate? And what does the && operator do with left-operand istream and right-operand size_t? Is that even defined?

CoolGamer48 65 Posting Pro in Training

as for .exe's with java you don't need them, jar files serve the same purpose, double click on one and it runs. and DLL's again you don't need them, jar files serve that purpose too, make sure the compiler knows about them and you can use any class in any package that is in it,

a .jar files only runs if the computer it's running on has the JVM (which, granted, most do), but an .exe file can run without the JVM, all you need is the OS itself (assuming your running windows). DLLs can be used by other languages as well (I believe), so being able to create DLLs would still be a benefit.

CoolGamer48 65 Posting Pro in Training

Do you know about templates? You could create a Linked List template class to make things easier to understand. Though, on second thought, creating a linked list template class can be a hassle, and giving you one wouldn't teach you anything about linked lists.

CoolGamer48 65 Posting Pro in Training

@AncientDragon
Doesn't the loop you give continually ask for strings until you enter one that doesn't have an 'i'? The original program just asked for one, and check if there was an 'i' in it, then ended. You can reduce lines 14-25 to

if( strchr(word,'i') )
   {
      cout << endl << "I asked for no i's!";
   }

You can also remove line 9, the deceleration of the variable i, if you employ this method.

Also, the program terminates after return 0; , so there's no point in the two cin.get() statements after that. They can either be removed, or, you can put them before return 0; if you want them to be executed.

CoolGamer48 65 Posting Pro in Training

Well why do you have a return in the function, you shouldn't need one because you are passing back by reference.

Wait, I'm not sure if he edited the code in the first post, but the function just had return; , not return someVal; . It's not neccesary to include the return; statement at the end of a void function, but I sometimes put it just so I'm controlling when the function returns, not the end curly bracket. Is that considered "wrong" or weird?

CoolGamer48 65 Posting Pro in Training

> So if the code file1.h needs to be executed before the code in file2.h (for some reason)
But compiling is not executing. #include is all about declarations, not program execution order. In other words, this works.

#include "func1.h"
#include "func2.h"
int main ( ) {
  func2();
  func1();
}

Ya - I thought I had used the wrong word.

CoolGamer48 65 Posting Pro in Training

The code in header files is executed in the order you #include them. So if the code file1.h needs to be executed before the code in file2.h (for some reason), then they must be #included in that order.

CoolGamer48 65 Posting Pro in Training

Um, sorry, what exactly is a dir? A directory?

CoolGamer48 65 Posting Pro in Training

Hey,

I'm writing an XML parser in C++. Currently it works, but too much of what needs to be done is left up to the end user. I'm trying to figure out a way to have a clean, more encapsulated interface for the parser, but I can't seem to think of one that I like.

This is the deceleration of the parser:

class XMLParser
{
public:
	XMLParser();
	~XMLParser();

	int OpenFile(std::string filename);
	void CloseFile();

	int ReadTag(std::string* name,TagType* tag_type,bool* attributes);
	int ReadAtribute(std::string* name,std::string* value,TagType* tag_type,bool* moreAttributes);
	int ReadData(std::string* data);
private:
	std::string m_filename;
	std::ifstream m_fin;
};

Definition of TagType:

enum TagType
{
	Unknown,
	Open,
	Close,
	StandAlone
};

The parser works with three main methods. The first is ReadTag(). It returns 1 on success, and fills name with the name of the tag, tag_type with the type of the tag (if not known, its set to 0 (unknown)), and fill attributes with whether or not there are any attributes to be read (if attributes is true, tag_type is always unknown).

ReadAttribute() should only be called directly after a call to ReadTag() that sets attributes to true (or a call to ReadAttribute() that says there are more attributes). It will give you the name of the attribute, it's value, the type of tag (again, if known), and whether or not there are more attributes (again, if true, tag_type is always unknown).

ReadData() should be called only after the attribute flag (of either ReadTag() or ReadAttribute()) is false. It fills …

CoolGamer48 65 Posting Pro in Training

anything that falls out of scope and does not have some type of reference pointing to it in java is immediately finalized (or rather, its destructor is called, though in Java this is finalization). Note that you CANNOT finalize a java object manually unless NOTHING is pointing to it! This leads to serious memory-management restrictions, aside from the fact that you don't have any real access to the heap anyways.

Ahh, ok. This seems much more useful. So there's no way to keep an object in scope after it normally would've been finalized (or is it just easier to do than in C++).

CoolGamer48 65 Posting Pro in Training

I see. But is that really all the useful? I mean what's the benefit of that method over using an array and the [] operator?