pseudorandom21 166 Practically a Posting Shark

It may have only been Visual C++ 6.0 or something, but I think I remember needing to add curly braces after a switch's case label to declare variables, is this still true with VS2010 and C++?

ex:

switch( 1 )
{
case 1:
{
  int a; //<-- ok
}
break;
case 2:
  int b; //<-- not ok?
break;
}

I only ask because I'm kind of under the weather and am in the middle of some updates to a project.

pseudorandom21 166 Practically a Posting Shark

woah woah woah, what's going on here?

If you want a function to modify a variable as an argument to the function you can either pass by pointer or by reference.

In this case you could also return the value.

pass by reference:

void GetChar(char &ch)
{
  cin >> ch;
}

Pass by pointer:

void GetChar( char *ch )
{
  cin >> *ch;
}

Returning the value:

char GetChar()
{
  char ch = 0;
  cin >> ch;
  return ch;
}

Also please note that the maximum index of an array of 20 elements is 19.

char pass[20];
cin.get(pass[20]);

thus:

cin.get(pass[19]);//<-- gets one character into the last element of the array
//I think you meant this, however:
cin.get(pass,20);//<-- gets an entire string of characters into the array.

http://www.cplusplus.com/reference/iostream/istream/get/

pseudorandom21 166 Practically a Posting Shark

make sure your braces match up. { }

pseudorandom21 166 Practically a Posting Shark

I'm a college student, about to have an associates degree and start working on my Bachelor's for CS. I have a project that I've been working on for a while now, and I'm thinking about making it open source, and hosting it on sourceforge. The reason I would do so, is to hopefully work with others.

My question is, will it be good or bad to put an open-source project on a resume?

Really, I couldn't find the right place to ask on daniweb. The project does use C++ and is small medium sized.

pseudorandom21 166 Practically a Posting Shark

I'm assuming the pointers point to dynamically allocated data, probably allocated with "new" ?

In that case, it is very important you do not lose track of the pointers that point to data allocated with "new". When you copy a pointer to the vector that has pointers in it, if you're over-writing a pointer pointing to data you may lose the only pointer to the location of the data in memory, and thus will leak memory.

It is my opinion that the proper way to do it is actually to use a smart pointer type, such as those provided by the boost lib. See boost::shared_ptr<>

Otherwise, if you must use raw pointers, delete the pointer you are going to over-write, set it to nullptr, then copy the new pointer over. If you wish the pointer to still be useful, do NOT call delete on the other copy of hte pointer in the other vector.

Instead, set it to nullptr.

Ex:

for(int i=0; i<m_vPopulation2.size(); i++)
{
	delete m_vPopulation2[i];
	m_vPopulation2[i] = m_vParentPop2[i];
        m_vParentPop2[i] = nullptr;//or NULL (0x0)
}
pseudorandom21 166 Practically a Posting Shark

I am familiar with neither, though I have used many a C++ library, and using only one or at the most two different libs will help maintain flexibility and minimize problems with the project. I would try to use only one, if possible.

pseudorandom21 166 Practically a Posting Shark

He's using a 20 year old compiler that has a header file called "graphics.h", the compiler he's using probably produces 16 bit executables and supports non-standard C++.

Ahh the amazing Turbo C++, it's still used in universities, typically in India. Though, this poster's profile says Pakistan.

I would recommend OP use a newer compiler, and not pay for courses that use that compiler. Wasting money, IMO.

pseudorandom21 166 Practically a Posting Shark

What are you trying to sort? The characters in each string, or the strings themselves?

I'm guessing the strings, right? The example you posted doesn't really make it obvious to me how you want the strings sorted.

You may wish to use a lexicographical less-than: http://www.cplusplus.com/reference/algorithm/lexicographical_compare/
combined with some character functions from <cctype> like toupper() or tolower()
http://www.cplusplus.com/reference/clibrary/cctype/

pseudorandom21 166 Practically a Posting Shark

Do you mean sort the characters of each string, or sort the array of strings?

std::sort() http://www.cplusplus.com/reference/algorithm/sort/

pseudorandom21 166 Practically a Posting Shark

It would almost be funnier if you used some turbo C++ headers.

jonsca commented: I have a special place in my heart for dos.h :) +6
pseudorandom21 166 Practically a Posting Shark

That's a good idea with the std::next_permutation(), I should have caught on to that myself. I thought that function was absolutely awesome when I went to do some permutation stuff.

Also, you may consider deciding upon the best container for your code, I would probably use a vector like Zjarek said. But the std::list<> can probably swap some stuff around faster.

Also, depending on how much data you have to permute, it may take a LOT of processing power or a LOT of time. It is my guess you will be considering performance improvements, so I'll add that to take advantage of multi-core processors you will need to use multiple threads of execution.

pseudorandom21 166 Practically a Posting Shark

http://www.cplusplus.com/reference/stl/set/ //container info
http://www.cplusplus.com/reference/std/iterator/BidirectionalIterator/ //iterator type info

ex:

for( set<char>::iterator it = myset.begin(); it != myset.end(); ++it){
//use iterator
}
pseudorandom21 166 Practically a Posting Shark

If your compiler has C++0x TR1 support, you can use the auto keyword for the type, otherwise you can use the iterator type.

I'm not exactly sure what you want, but here is an example of iterating over a set with iterators.

If iterators are new to you, you may wish to read about them before jumping in to using the STL.

set< int > myset;
	myset.insert(100);
	myset.insert(200);
	myset.insert(300);
	for( set<int>::iterator it = myset.begin(); it != myset.end(); ++it)
	{
		//Use iterator.
		cout << (*it) << endl;
	}
pseudorandom21 166 Practically a Posting Shark

My suggestion (while maybe not the best, but is simple), is:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  const string ping_command = "PING ";
  string input;
  cout << "Enter IP: ";
  getline(cin, input);//<-- gets a line of input into "input"
  string command = ping_command + input;
  //use the command.c_str().
}

.c_str() will return the C style string data the string has. It is required to use the system() function.

pseudorandom21 166 Practically a Posting Shark

I'm relieved to see daniweb has a sense of humor :)

pseudorandom21 166 Practically a Posting Shark

Random suggestion: could typedef your array so as to help ensure consistency.

typedef char matrix_type[100][50];
int foo(matrix_type m);
pseudorandom21 166 Practically a Posting Shark

Yes it does waste at most (N/2)-1 units of memory, I'm not an expert at memory management, I'm a college student--that's just something I picked up on reading a random website a year or two ago. The website was basically some guy ranting about how crappy his memory management was, then the perfect solution was to allocate N*2--like it was a magical formula made by god. Kind of funny, actually.

and yes you do delete it, my bad.

pseudorandom21 166 Practically a Posting Shark

You could try implementing the sieve of eratosthenes using a vector<bool> or a bitset, the wikipedia page on the sieve is decent; the vector<bool> is for marking it as not prime.

The indices would be the numbers.

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

You also have other options, of course. It is my belief that Era's sieve will be the fastest.
http://en.wikipedia.org/wiki/Sieve_of_Sundaram

Here, this according to wikipedia, is a modern version of Eratosthenes' sieve:
http://en.wikipedia.org/wiki/Sieve_of_Atkin

see also: http://en.wikipedia.org/wiki/Wheel_factorization
And, it's up to you as the master and commander of your project to determine the one that will suit your needs of both difficulty in implementation/time used and the required performance.

pseudorandom21 166 Practically a Posting Shark
void Add(DataType p_item)
{
int m_newSize = m_size+1;
DataType* tempArr = new DataType[m_newSize];
for(int i=0;i<m_size;i++)
tempArr[i] = m_array[i];
tempArr[m_size] = p_item;
m_size = m_newSize;
if(m_array!=0)
delete[] m_array;
m_array = tempArr;
tempArr = 0;
}

Well there are some serious problems with this. I expected your class to be something similar to a vector, meaning it will allocate a large block of memory and then allow access to a certain part of it. Reallocation can be time-consuming and the best way to avoid frequent re-allocation is to allocate N*2 elements (usually).

So if you have 512 bytes reserved for use by the container and it needs more memory, one would allocate 1024 bytes, and the next realloc would be 2048.

My question is, why are you allocating a new array and copying every time you add a new element? Wouldn't it be better to allocate a chunk of memory, and allow access to it as needed?

pseudorandom21 166 Practically a Posting Shark

Yes that is indeed a pointer to a function, a named one.

typedef void (*funcPtrType)(int someParam);
funcPtrType pSomeFunc = nullptr;
pSomeFunc = &someFuncWithTheRightParamsAndReturnType;
pSomeFunc(1000);
pseudorandom21 166 Practically a Posting Shark

Sometimes data is left in the buffer of cin.

check cin for !good(), cin.good() is not the exact opposite of cin.bad(). good() will return true if no error bits are set, false otherwise. Afterward you will want to clear() the errors bit, and sync() the buffer associated with the stream to its controlled input sequence.

int number = 0;
cin >> number;
if( cin.bad() )
{
  //badbit is set
  //stream is totally f'd up now.
  return 1;
}
else if( !cin.good() )
{
  cin.sync();//sync
  cin.clear();//clear error bits.
  //stream can now be used again.
}

Note that sync() can return a value.

http://www.cplusplus.com/reference/iostream/istream/


cin is an object of type istream.
http://www.cplusplus.com/reference/iostream/cin/

pseudorandom21 166 Practically a Posting Shark

I would also like to add that the input stream is not checked for errors in the example you have been given, so if you type a letter when it expects a number it will probably cough & die or cough & infinite loop.

pseudorandom21 166 Practically a Posting Shark

http://www.johndcook.com/cpp_TR1_random.html#seed

I meant to include this link in my post.

pseudorandom21 166 Practically a Posting Shark

I think the tr1 random facilities may help. If you're using Visual Studio 2010, this may be worth looking into:

#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
	std::tr1::uniform_real<double> dist( 0.0, 1.0);
	std::tr1::mt19937 engine(time(nullptr));
	for(int i = 0; i < 20; i++) //print a few random numbers.
	std::cout << dist(engine) << std::endl;
	std::cin.get();
}
pseudorandom21 166 Practically a Posting Shark

Nice to know, see I learned something. :D

Typically I will exploit every useful part of the standard library to make the application more robust. Note being restricted to <iostream> and <cmath>.

pseudorandom21 166 Practically a Posting Shark

Oh, and for mike_2000_17's implementation, the values returned by your operator-/+ overload would usually be constant, to avoid weird crap like this:

((obj - obj2) = obj3);

and my Triangle needs a copy constructor to do a deep copy because of the array.

pseudorandom21 166 Practically a Posting Shark

I do agree it turned out to be a piece of crap. I suppose I should give it another whirl, after all I'm here to learn.

So, I forgot OP doesn't even know how to use arrays yet, but here's a pretty one (in my opinion):

#include <iostream>
#include <cmath>

struct Point
{
	Point() : x(0.0f), y(0.0f) {}
	Point(float tx, float ty) : x(tx), y(ty) {}
	Point& operator+=(const Point &rhs)
	{
		this->x+=rhs.x;
		this->y+=rhs.y;
		return *this;
	}
	Point& operator/=(const int &rhs)
	{
		this->x /= rhs;
		this->y /= rhs;
		return *this;
	}
	const Point operator-(const Point &rhs) const
	{
		return Point(rhs.x-this->x, rhs.y-this->y);
	}
	friend std::ostream& operator<<(std::ostream &out, const Point &rhs)
	{
		out << "(" << rhs.x << "," << rhs.y << ")";
		return out;
	}
	//data members
	float x;
	float y;
};

struct Triangle
{
	static const int NUM_TRIANGLE_POINTS = 3;
	Point vertices[NUM_TRIANGLE_POINTS];
};

Point CalculateCentroid(/* IN */ Triangle &triangle_in)
{
	Point r;
	for(int i = 0; i < triangle_in.NUM_TRIANGLE_POINTS; i++)
	{
		r+=triangle_in.vertices[i];
	}
	return (r /= triangle_in.NUM_TRIANGLE_POINTS);
}

float CalculateDistance(const Point &centroid, const Point &fromPoint)
{
	Point t = fromPoint - centroid;
	return t.x * t.x + t.y * t.y;
}

bool GetInput(/* IN */ Triangle &triangle_in)
{
	for(int i = 0; i < triangle_in.NUM_TRIANGLE_POINTS; i++)
	{
		std::cout << "Enter triangle point " << i+1 << " as a float" << std::endl;
		std::cout << "In the form: x y" << std::endl;
		std::cin >> triangle_in.vertices[i].x >> triangle_in.vertices[i].y;
		if( !std::cin.good() )
		{
			std::cout << "Invalid entry; retry." << std::endl;
			std::cin.sync();
			std::cin.clear(); …
pseudorandom21 166 Practically a Posting Shark

Personally I started programming in C++ while I was in middle school, it was a beast but after a while (i would guess a few weeks) studying at home after school (programming didn't suck like my classes), I was able to put together some decent programming logic with it. The program used labels and "goto" a lot, but it was still inspiration. Maybe I'm just actually a programming fool, but I'm quite certain if you waste enough time messing with a language and have a good book handy you will master it--with time.

By the way I (gasp) downloaded my first programming book and a copy of Visual C++ 6.0. Best decision ever.

Today, (I'm 20) I do notice programming skill does aid in my understanding of Computer Science subjects. The only real limitation software engineers/programmers have (in my opinion) is limited intelligence, and a crappy memory. If you're decently bright, meaning when you try to think your head isn't all cloudy, then you should do fine if you're willing to learn a programming language or two. Creativity is an extraordinary bonus too.

pseudorandom21 166 Practically a Posting Shark

This is not the appropriate place to beg for code.

Fbody commented: I prefer to give +rep today :) +5
pseudorandom21 166 Practically a Posting Shark

This was an experiment for me, using enums to index arrays, and for the size:

#include <iostream>// for cout and cin
#include <cmath> // for sqrt () and pow (x,y)
 

using namespace std;

enum //<-- anonymous enum
{
  DISTANCE_ONE = 0,
  DISTANCE_TWO,
  DISTANCE_THREE,
  DISTANCE_BUFFER_SIZE
};

struct Triangle
{
	Triangle() : x(0.0f), y(0.0f) {}
	float x;
	float y;
};

enum
{
	TRIANGLE_ONE = 0,
	TRIANGLE_TWO,
	TRIANGLE_THREE,
	TRIANGLE_BUFFER_SIZE
};

//Prototypes
float ComputeDistance( const Triangle &centroid, const Triangle &point );
Triangle ComputeCentroid( const Triangle points[], const int size );
bool GetInput( Triangle points[], const int size );

int main () 
{
	cout << " Triangle Centroid Calculator!" << endl; // welcome user
	float distanceBuffer[DISTANCE_BUFFER_SIZE] = {0.0f};//  assign number of distance 1-3
	Triangle triangleBuffer[TRIANGLE_BUFFER_SIZE];// Points.
	Triangle centroid;

	while( !GetInput(triangleBuffer,TRIANGLE_BUFFER_SIZE) )
	{
		cin.sync();
		cin.clear();
	}
	
	centroid = ComputeCentroid(triangleBuffer, TRIANGLE_BUFFER_SIZE);

	cout << "The centroid is located at (" << centroid.x << ',' << centroid.y << ") " << endl; // distplay centroid coordinate to user
	cout << endl;
	
	distanceBuffer[DISTANCE_ONE] = ComputeDistance(centroid,triangleBuffer[TRIANGLE_ONE]);
	distanceBuffer[DISTANCE_TWO] = ComputeDistance(centroid,triangleBuffer[TRIANGLE_TWO]);
	distanceBuffer[DISTANCE_THREE] = ComputeDistance(centroid,triangleBuffer[TRIANGLE_THREE]);

	
	//Print results
	for(int i=0; i < DISTANCE_BUFFER_SIZE; i++)
	{
		cout << "Distance from centroid to point " << i << ": " << distanceBuffer[i] << endl; // prompt user of the distance1
	}
	cout << endl;
	cout<< "Thank you for your business." << endl;
	
	cout << "Press ENTER to exit." << endl;
	cin.sync();
	cin.clear();
	cin.get();
    return 0;
}

bool GetInput( Triangle points[], const int size )
{
	cout << endl;
	for(int i = 0; i < size; i++) …
pseudorandom21 166 Practically a Posting Shark

That should do it? I think OP is using native C++. Where is 'i' created anyway?

pseudorandom21 166 Practically a Posting Shark

I think the stl container template, std::vector<T> will help you with the dynamic allocation, and the STL algorithm std::random_shuffle() will help you obtain new matrices--as for the random number generation, if you have C++0x TR1 support on your compiler, you can use the new random number generation facilities which includes distributions. Otherwise, the C function rand() will generate random numbers, and you will have to make do with them, probably by dividing by 10 until you get a decimal (or something more creative).

Of course you may opt to not use the vector or standard library algorithms, it is just a possibility and will make it much easier to do the assignment.

pseudorandom21 166 Practically a Posting Shark

The parameter is a pointer, so send it either a pointer to a valid RECT or the address of an automatic variable.

pseudorandom21 166 Practically a Posting Shark

The reference for the DrawText function says it's a pointer to a RECT structure,
http://msdn.microsoft.com/en-us/library/dd162498%28v=vs.85%29.aspx

The reference for the RECT structure says it's four LONG variables,
http://msdn.microsoft.com/en-us/library/dd162897%28v=vs.85%29.aspx

So you can make a RECT like this:

RECT drawingArea;
memset(&drawingArea, 0, sizeof(RECT));
drawingArea.top = 0;
drawingArea.left = 0;
drawingArea.right = 150;
drawingArea.bottom = 150;

then use a rectangle with the function like this:

DrawText( hdc, (LPCWSTR) szTitle, -1, &drawingArea, DT_WORDBREAK );
pseudorandom21 166 Practically a Posting Shark

It's also good to avoid magic numbers.

In mike_2000_17's code snippet magic numbers are used for the array size, and in the for loop.

magic number = unnamed numerical constant.

I've been turned down a job because I used a couple o' magic numbers.

Also in your code exist magic numbers.

pseudorandom21 166 Practically a Posting Shark

to make more readable: Functional decomposition, which involves breaking the program into segments and using functions for them. For instance, getting your input could be in it's own function.

Personally I would probably use an array of floats and enum values for indexing it.
ex:

enum //<-- anonymous enum
{
  DISTANCE_ONE = 0,
  DISTANCE_TWO,
  DISTANCE_THREE,
  BUFFER_SIZE
};
int main()
{
  float buffer[BUFFER_SIZE];
  buffer[DISTANCE_ONE] = 0.0f;
  buffer[DISTANCE_TWO] = 0.1f;
  //etc.
}

You certainly don't have to do it that way, it was just a random thought of mine.

pseudorandom21 166 Practically a Posting Shark

The problem is here:

class PaintHandler{
};

That shouldn't be in your source file.

pseudorandom21 166 Practically a Posting Shark

Not unless you pay me for it.

Problem Solving skills are valuable, and this isn't a place to beg people to write code for you.

pseudorandom21 166 Practically a Posting Shark

Then you have to decide how you want to check to see if it's running. There is more than one way to do it, and like I said before if this is supposed to be reliable in any way it will require more work.

Take a look at the examples for C++ on this page: http://msdn.microsoft.com/en-us/library/x8b2hzk8.aspx

pseudorandom21 166 Practically a Posting Shark

Hey I made some progress, but I think the scrolling part will be somewhat of a chore--but maybe not.

pseudorandom21 166 Practically a Posting Shark

It's not managed C++ anymore, it's C++/CLI--there is a difference.

managed C++ had the horrible syntax, C++/CLI is actually kind of decent.

Ancient Dragon commented: good point :) +36
pseudorandom21 166 Practically a Posting Shark

Okay, so the problem is:
I need a way to display a graphic with a few lines of text over the picture. An amount determined at run-time of these will be created.
They also needs to be contained in a scrollable container, and scroll properly. It sounds simple but I'm not very experienced with C++/CLI or the forms designer.

so my question is, is there a way to attach labels to a picturebox, and is there a control in which I can place pictureboxes and scroll them?

pseudorandom21 166 Practically a Posting Shark

Yeah you could make a notes application, or a scheduler which would alert the user at a certain time, etc.

and when you say "third year" does that mean you're on year 3/4 for a bachelor's degree?

I would make something useful to yourself, I have a few of them.

pseudorandom21 166 Practically a Posting Shark

If a U.S. government agency were to infect a PC with a virus, would it be possible to detect it with personal anti-virus software? If so, what's the best bet software wise?

pseudorandom21 166 Practically a Posting Shark

How are iterators used in C# ?

suppose I have say:

List<int> someList = new List<int>();
/* assume some items are added here */

/* and now I wonder if C++ style iterators are available? */

for( var i = /* iterator begin of list */ ; i != /* iterator to end of list */; ++i )
{
   //use iterator.
}

I understand it's common to use the foreach with C# arrays/containers, but I prefer the normal for( ; ; ) { }.

pseudorandom21 166 Practically a Posting Shark

a struct prototype/forward declaration may solve your woes, or a different design that avoids circular dependencies.

struct a;

pseudorandom21 166 Practically a Posting Shark
pseudorandom21 166 Practically a Posting Shark

Oh yeah, I had a problem with that when I was first learning C++ too.

= assigns to whatever is on the left side of it.

pseudorandom21 166 Practically a Posting Shark

Truthfully, my best advice to you is not make performance your #1 priority for every application.

Professional programmers, (and sometimes software engineers) will use the right tool for the job. That's what programming languages are, tools with which to solve problems. Native C++ (the ISO kind, not C++/CLI) is often used in performance-critical applications, and also is somewhat vendor independent. Many computer games are written with C++. Now having said that, it does have it's limitations, such as not having a standard way to create a Graphical User Interface. To create a GUI with native C++ will be different for Windows and for Linux. If you want to know more about C++ you can read the FAQ on Bjarne Stroustrup's (the creator of C++) website.

While C++ can be used for just about everything under the sun, and I do recommend you learn it if you intend to be a software engineer/developer/programmer--I myself would opt to use the RIGHT tool for the job.

Automating HTTP stuff is a real pain with C++, but the beauty of the beast is that there are so many libraries to assist with doing things like that (it's quite commonly used) it's almost a guarantee to find a library for doing many things from a quick google search.

Now I'm done ranting, but I want to say it is of utmost performance that you learn more than one programming language. .NET languages (like C# and C++/CLI) are Microsoft dependent, but allow …

jonsca commented: Good observations +6
pseudorandom21 166 Practically a Posting Shark

The most likely reason they don't like .NET is because of vendor dependence, namely .NET code only working on Windows.

The majority of the world uses Windows, and if you fear that may change, or you like Linux oh so much then you should steer clear of it.