JoBe 36 Posting Pro in Training

The idea is to help him create HIS own code, not to give a possible solution ! You're not helping him with this, on the contrary !

Salem commented: Quite so. +36
JoBe 36 Posting Pro in Training

Well, whatta you know, I have a Home Key aswell :D

Thanks Narue ;)

JoBe 36 Posting Pro in Training

Hi Danny,

Was wondering when you scrolled down all the way to read the thread you're interested in, there's no way to either go to the top of the page or push a button to go back to view the forum in which the thread was started ?

Wouldn't it be alot easier and faster then having to scroll back up every time.

JoBe 36 Posting Pro in Training

Great tutorial on pointers by Narue @ Eternally Confuzzled

JoBe 36 Posting Pro in Training

Still looking for programming, as well as organizing help!

Hi, allways fascinated with AI, can't help myself but maybe this place is a good place to ask for help: Gamedev_AI_Forum

Hope this helps :)

JoBe 36 Posting Pro in Training

- Which compiler are you using ?
- Have you created a console application ?

- Is this the code you entered:

#include <iostream>

using namespace std;

int main()
{
	int age = 0;
	cout <<"please enter your age :";
	cin >> age;
	age = 10 + age;

	cout <<"your age in ten years will be"<< age << endl ;

	return 0;
}
JoBe 36 Posting Pro in Training

Doesn't seem to be anything wrong with it !

Are you having a problem with the code ending immediatly after entering a variable ?

Do use

tags in the future, makes it easier to read your code.

JoBe 36 Posting Pro in Training

Yes i understand i need to use break; =P

So what if i make cho an integer? Then it would be 1 and not '1' correct?

>> - the labels must be literal characters.

I got a long way to go lol. Thanks in advance

Yep, like vmanes mentioned !

JoBe 36 Posting Pro in Training

- char cho ;

- After each case you have to use a break; , otherwise, the program will fall threw to each case that is presented.

- I guess you haven't shown all the code, seeing as you didn't use close braces for the do loop and main.

JoBe 36 Posting Pro in Training

1 is a number.

'1' is a character.

So, it depends on which of the two you are going to use.

JoBe 36 Posting Pro in Training

Great tutorial on pointers: Narue's Eternally Confuzzled (Click Here)

C++ dynamically allocating memory:

int *pMyInt = new int;

...

delete pMyInt;
JoBe 36 Posting Pro in Training

Darn, sorry Narue. As soon as I saw your correction to my code, I felt stupid :icon_redface:

JoBe 36 Posting Pro in Training

Hi Narue,

Well, the problemis that I'm getting these error messages:

[TEXT]Removed error messages[/TEXT]

JoBe 36 Posting Pro in Training

Hi all,


I have a class which contains a struct that holds some variables, string, int's.

Now, one of the variables (string) in the struct is used to compare it with a name, if the name is in the struct, it has to be returned from my function with it's own integer variables to be put into a separate list.

The thing is, how is the code written to do so ?

struct ArmorArray
{
	std::string armorName;
	int armorCheck;
	int maxDext;
	int price;

	ArmorArray *nxtArmor;
};

class Armor
{
	private:
	ArmorArray ArmorList[13];
	ArmorArray *p_List;

	int m_SIZEARRAY;

	std::string m_ArmorName;
	std::string m_String;

public:
	Armor();
	~Armor() {}

	p_List BuyArmor();
	std::string CreateString(std::string &myString);
	std::string ArmorBought(std::string &bought);
};
p_List Armor::BuyArmor()
{
	m_ArmorName = CreateString (m_String);

	for (int i = 0; i < m_SIZEARRAY; i++)
	{
		if(!m_ArmorName.compare(ArmorList[i].armorName))
		{
			return ArmorList[i];
		}
	}

	//return m_ArmorName = "Armor does not exist ! Try again or return to previous menu !";
}

Thanks for any assistance.

JoBe 36 Posting Pro in Training

TIP !

Show what code you have written, makes it easier to see what direction you want/have to take !

Working with a an STL vector ?
Working with an array ?
... etc.

JoBe 36 Posting Pro in Training

Ive run your program and it gave me an error, according to my limited knowledge it's related towards going out of bound with your array.

Try using for loops:

for (int i = 0; i < (N-1); i++)

and change your switching algo to this:

if (a[j] < a[j+1])
{
            int tmp = a[j];
            a[j] = a[j+1];
            a[j+1] = tmp;
}

Remember, you need two loops, you run the first for one time and the second will go threw the whole array, if that's okay, your first array will run for the second time and so on and on.

Watch out for overflowing your array.

JoBe 36 Posting Pro in Training

Ok, thanks for the explanation vmanes.

JoBe 36 Posting Pro in Training

Well, found a solution to solve the problem:

if (fin) // allready exists ?
	{
		cout << "Current file contents:\n";
		char ch;

		while (fin.get(ch))
			cout << ch;
		cout << "\n***End of file contents.***\n";
	}
	fin.close();
	fin.clear();

.......

The problem seems to be this:

fin returns 0 because "the base class iso (from which istream is inherited) provides an overloaded cast operator that converts a stream into a pointer of type void*. The value of the pointer is 0 if an error occurred while attempting to read a value or the end-of-file indicator was encoutered." (1)

So, If I understand this correctly, your input because of being casted to a type void pointer get's another address and therefore isn't recognised?

If so, why does the function clear() solve this?

JoBe 36 Posting Pro in Training

Good evening ladies and gents,

Had a question concerning this piece of code that I'm trying out from a book, it supposed to open a file, write text to it, close it, reopen it, append text to it and then write the text from the file to the console screen, it works fine up untill the text has to be written to the console screen, then I get the message that the file can't be opened for reading though the text is added to the file?

What am I doing wrong?

#include <fstream>
#include <iostream>

using namespace std;

int main()	// returns 1 on error
{
	char fileName[80];
	char buffer[255]; // for user input.
	cout << "Please reenter the file name: ";
	cin >> fileName;

	ifstream fin(fileName);

	if (fin) // allready exists ?
	{
		cout << "Current file contents:\n";
		char ch;

		while (fin.get(ch))
			cout << ch;
		cout << "\n***End of file contents.***\n";
	}
	fin.close();

	cout << "\nOpening " << fileName << " in append mode...\n";

	ofstream fout(fileName, ios::app);
	if (!fout)
	{
		cout << "Unable to open " << fileName << " for appending !\n";
		return 1;
	}

	cout << "\nEnter text for this file: ";
	cin.ignore(1, '\n');
	cin.getline(buffer, 255);
	fout << buffer << "\n";
	fout.close();

	fin.open(fileName); // reassign existing fin object.
	if (!fin) // <-- problem is here.
	{
		cout << "Unable to open " << fileName << " for reading !\n";
		return 1;
	}

	cout << "\nHere's the contents of the file: \n";
	char ch;
	while …
JoBe 36 Posting Pro in Training

Thank you for the explanation and examples gentlemen :)

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Got a question, is it wise to create a member of a class by using another class it's constructor and deleting it threw the use of that class it destructor?

For example:

// Testing code.

#include "first.h"

int main()
{
	first myMenu;

	bool gameLoop = true;

	while (gameLoop)
	{
		gameLoop = false;
	}

    return 0;
}
#include "second.h"

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

private:
	second *mySecond;
};
#include "first.h"

first::first()
{
	mySecond = new second;
}

first::~first()
{
	delete mySecond;
}
class second
{
public:
	second();
	~second();
};
#include "second.h"

second::second() {}

second::~second() {}

Is this something that is done or should it be avoided?

Thank you.

JoBe 36 Posting Pro in Training

Hi Narue,

Great to see you're stilll around here.

>It looks fine, why do you ask?
Because I wasn't sure Narue.

>It's just your debugger's way of saying that mName isn't ready to be used yet. If you get that after stepping over the line (where mName should be created and initialized), then you probably have an issue.
Ah, ok, theres no problem after stepping over the line.

>Why should you? std::string is an object that manages the actual string for you. The memory grows to fit the size of the string automagically.
Well, believe it or not, I thought it wasn't necessary, but, because the message of the <bad Ptr> I started to doubt this. I was thinking because I didn't add a sizeof, the <bad Ptr> was appearing.

>Yes, it's gimped now. Completely useless if you want to do any kind of advanced searching. But it seems Dani isn't done upgrading the search feature, so perhaps it will be fixed in the near future.
Well, I hope she fixes it, it was alot better and easier to find related posts/threads to what you where looking for !!!

Anyway, thanks again Narue, good to see you're still around :)

JoBe 36 Posting Pro in Training

Hello ladies and gents,

It's been ages since I posted here, have been doing some coding on and of and I was wondering about the following, beneath is a test I made in trying to use the new operator in combination with a string, first a string literal, second time a string input.

It's working, but, the thing that I'm seeing when debugging this is when I get to the point std::string mName = "John Malkovich"; the value of mName comes up as <bad Ptr>.

Questions:
A) is the way I used the string and new operator correct for this little program?
B) is this <bad Ptr> related to a bad Pointer and what does that mean? Probably not something good?
C) It seems that when I use std::string *pName = new std::string; , I don't have to determine the sizeof this string? Is this correct? Or is something going wrong which I'm not aware of?

// Testing code.

#include <iostream>
#include <string>

int main()
{
	std::string mName = "John Malkovich";

	std::string *pName = new std::string;
	*pName = mName;

	std::cout << "Name is: " << *pName << " !\n";

	delete pName;
	pName = 0;

	std::string secName = "";

	getline(std::cin, secName);
	pName = new std::string;
	*pName = secName;

	std::cout << "Second name is: " << *pName << " !\n";

	delete pName;
	pName = 0;

    return 0;
}

Thanks for any help you guys/girls can help.

Edit: oh yes, has the search function been …

JoBe 36 Posting Pro in Training

Forgive me, but without declarations and the intent of the operations, there is really not a question being asked here.

Hi Dave, I understand what you're saying, but the examples that I gave are how they are given in the exercise for the Bjarne Stroustrup The C++ Programming Language, there isn't anything mentioned about declarations or intent of operations.

I guess it's just an exercise to practise your knowledge of order of precedence for the various expressions.

@ Walt, thanks for that Walt, but, as said previous, don't think this exercise means much more then to see whether you know what expression has a higher precedence.

Thanks for the help guys ;)

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive got a few more expressions I needed to fully parenthesize:

1) *p++ becomes *(p++)

2) *--p becomes nothing
if *--p would have been --*p:
--*p OR--(*p) would have the same result.
Reason is, both would have the same precedence, so, it
goes from left to right in both cases.

3) ++a-- becomes ++(a--)

4) (int*)p->m becomes (int*)(p->m)

5) *p.m becomes *(p.m)

6) *a becomes *(a)


One more question, am I correct that the following expression:
(int*)p->m equals a pointer member which get's a type cast conversion?

JoBe 36 Posting Pro in Training

I'm not Dave, but hi anyway! :)

Hi Ravalon :cheesy:

Yes, that's it exactly. :)

Ok, that's what I needed to know.

New code should be written in either C95 or C99. Okay, maybe that wasn't so easy. :eek:

When you speak of "new code", you do mean "new code" written by those compiler developers right? If not, then yeah, you got me confused again :cheesy:

JoBe 36 Posting Pro in Training

[intervening distractions]

I also meant to link to this for a C90 list:
http://web.archive.org/web/20050207005628/http://dev.unicals.com/papers/c89-draft.html#A.6

Hi Dave,

You're speaking of the C90 list, but yet, the Draft shown is C89, am I missing something?

Also, what is actually C89, C90, etc... all about, are they like definitions of what C/C++ compiler writers have to keep in consideration?

JoBe 36 Posting Pro in Training

Thank you both for the help gentlemen :!:

JoBe 36 Posting Pro in Training

Nope, can't find it, opened parent directory, searched for anex J, doesn't seem to be in the list.

JoBe 36 Posting Pro in Training

Hi Salem,

Thanks for the link, but, to me, it reades rather abstract and doesn't really tell me if what I wrote was correct.

Can you tell me whether the following examples are examples of undefined and implementation-defined constructs:

// Implementation defined:

1) unsigned char c2 = 1256; // implementation defined.
2) char ch = 'j'; char *p = ch; // implementation defined.


// Undefined behaviour:

1) num /= 0; // undefined behaviour.
2) int arr[2] = {0};
for(int i = 0; i < 4; i++) // undefined behaviour.
arr;
3) int arr[2] = {1, 2, 3}; // undefined behaviour.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Gotta few questions if you don't mind:

1) Exc_6_4) Wrote a table of values for the bitwise logical operators and was wondering if any of you could check them out whether they are correct this way.

2) I have to write five examples of undefined behaviour and five examples of implementation-defined behaviour. I'm I correct in assuming that undefined behaviour happens when you've written code that is although accepted by your compiler, doesn't behave as you intented or even let's your computer crash and that implementation-defined is when you've written code for which your compiler gives you a warning about?

JoBe 36 Posting Pro in Training

Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

Well, I actually don't have a credit card :cheesy: And yeah, like Bench said, in what kind of restaurants do you go and eat :lol:

Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.

It's not that I dislike it, on the contrary, it shows that your parents brought you up in a very descent manner and we to are thought to be curtious and polite, just, .... not on a forum like this :cheesy:

But still if you don't like it, I will try to refrain from saying it the next time, though there are no guarantees...

Thanks s.o.s, appreciated ;)

Well, I guess kids these days don't get taught any respect!

LOL :lol: Never thought that at the age of 37, I would be called a KID LOL :lol: Thanks .... Mr. Bench ;)

JoBe 36 Posting Pro in Training

I don't know Mr. Jobe if you already know this but there is a book which presents the solutions to the problems posed in C++ Programming Language

Nope, didn't know that s.o.s, thanks for that.

I'd appreciate it if you didn't call me Mr. Jobe, makes me feel old and superior somehow, either JoBe or Johan(my real name) will do ;)

And if you think the book is too expensive or not worth it...

I certainly think it's worth it, but, due to lack of trust in online buying with a credit card, I tend to use a store called proxis.be and they don't sell it anymore.

Anyway, thanks for that, I'll surely see if I can't buy it anywhere else :!:

JoBe 36 Posting Pro in Training

it could be correct if b and d were both of type int* - but since the OP's assignment doesn't say otherwise, a safe assumption is that these are just plain old numbers (or perhaps literals)

Yep, tried that and it worked, but, as you say, there is no explanation as to what the variables represent, pointers or numbers.

JoBe 36 Posting Pro in Training

Hi! :)

Hi Ravalon ;)

You missed some parentheses. << and & have different precedence.
& is the bitwise AND operator, so the expression is valid and can be parenthesised.

Darn :cheesy:

That's a cruel example. It's a valid expression, but whomever wrote it was trying to be mean and trick you with whitespace. The first - makes 1 negative, the second + is a unary operator that makes b nonnegative and the -- applies to b. If you use more sensible spacing, it looks like a = -1 + +b-- - 5 .

Well, you know Bjarne, cheeky fella :cheesy:

That's a little off. ;)

Darn Darn :cheesy:

JoBe 36 Posting Pro in Training

10 is not a valid statement. you can write a short program that tests each of your problems.

Ok, I'll do that AD :!:

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive got an exercise in which I have to fully parenthesize expressions using the higher-lower precedence, for instance:

a+b*c would become (a+(b*c)) because multiplication has a higher precedence then addition.

Here are my solutions for the following expressions:

1) a = b + c * d << 2 & 8
(a = ((b + (c * d)) << 2 & 8))

2) a & 077 != 3
Don't think there is a solution for this since & is "adress of", but how can you have an adress of 077?

3) a == b || a == c && c < 5
(((a == b) || ((a == c) && (c < 5))))

4) c = x != 0
(c = (x != 0))

5) 0 <= i < 7
((0 <= i) < 7)

6) f(1, 2) + 3
((f(1, 2)) + 3)

7) a = - 1 + + b -- - 5
Don't think there is a solution either since the expressions are not correct?

8) a = b == c ++
(a = (b == (c ++)))

9) a = b = c = 0
(a = (b = (c = 0)))

10) a[4][2] *= * b ? c : * d * 2
((a[4][2]) *= (* b ? c : ((* d) * 2)))

11) a-b, c=d
((a-b), (c=d))

I was hoping that you guys could check these out and tell me which ones are wrong?

Thank you.

JoBe 36 Posting Pro in Training

Try writing a 'hello world' app or something simple just to see if it works in there.

Yep, that did the trick Bench, thank you :!:

JoBe 36 Posting Pro in Training

I do believe it is related to Intellisense, because when I enter for instance
std:: I get a message: 'No additional information available'. It's that information I want again.

In the past, when I entered std:: --> I got a list with all possibilities, the same for instance with cin. --> I got a list with for example get, clear, ignore, etc...

It's not the shortcut Ctrl-J I'm after, it's the above.

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Was wondering if any of you could help me out with the following, sometime ago I had to reinstall MS V++ EE and ever since then, when I enter for instance 'std::' or 'cin.' I don't get a list with all the possibilities it has, normally I had like a list for cin. like get, clear, ignore, etc...

Can any of you tell me how I can activate that again? Hope you understand what I mean, was difficult to explain it correctly.

Thank you.

JoBe 36 Posting Pro in Training

Its never too late to start. I was 40 when I started, but it is a lot better to start at a much younger age.

And I thought I was old when I started at the age of 35 :cheesy:

Serious, did you really start at 40 and made it your career AD?

JoBe 36 Posting Pro in Training

I like the list Dave Sinkula posted sometime ago: C++ book list :!:

Bjarne Stroustrup's C++ Programming Language is great, however, in my personal opinion, not the first C++ book you should read as a beginner!!!

JoBe 36 Posting Pro in Training

Narue wrong on a C++ question?? This thread is getting stickied to commemorate!

Narue WRONG ????

NAH, can't be, it was just a fault in the Matrix :cheesy:

JoBe 36 Posting Pro in Training

iamthwee, thank you very much for that link :!:

Made this from the example you gave:

for (size_t i = myWord.find(mySecArr, 0); i != std::string::npos; i = myWord.find(mySecArr, i))
	{
		count++;
		i++;		// Move past the last discovered instance to avoid finding same string.
	}

The i++; is really necessary, otherwise the loop just keeps on going, am I correct that as the text stated, when not using this, the string never fails finding the char and therefore i != std::string::npos; (search failes) is never reached?

JoBe 36 Posting Pro in Training

Maybe the C++ version was supposed to use .find()?

HI Dave, long time no see, I see you lost your mod stick, good to see you here again though :)

I tried using .find() and .compare(), but it seems that they can only be used to find if there is A resemblance of code in both strings, not several. Could it be that I have to write it in a way it traverses threw the string that I want to compare? If so, I can only traverse threw one by using a loop and either string[i] or *(string + i) right ?

This is enough room for one character plus the terminating null:
char letters[2];

DARN, I forgot the terminating null, it does work though, I'll change it to [3] and add the '\0' at the back, thanks for the tip.

There are other ways, but not necessarily better. For just two characters yours way is efficient.

Oh, ok, thanks.

If you had to test 4 or more, a loop might be better.

Nope, don't understand that, I thought I was using a loop?

There are also the C-string str???() functions and C++ strings compare methods. Either of those you can look into.

Thought so, thing is, as said above, how do I use .find() and/or .compare() when having to go threw the whole string when trying to find more then one occurence of the same piece of string?

JoBe 36 Posting Pro in Training

Hi guys,

Solved the exercise, but, I'm not that happy with it because both functions actually look very similar, meaning, the c-style string and the cpp string. Don't think that was the idea of this exercise.

//Exercises from the book: The C++ Programming Language by Bjarne Stroustrup.

/*Write a function that counts the number of occurences of a pair of letters in a string and
  another that does the same in a zero-terminated array of char(a C-style string). For example,
  the pair "ab" appears twice in "xabaacbaxabb".
  */

#include <iostream>
#include <string>

int compareChar(char *myArr, char *mySecArr);
int compareString(std::string myWord, char mySecArr[2]);

int main()
{	
	char mychar[] = "xabaacbaxabba";
	char letters[2];
	std::string myWord;

	myWord = mychar;

	std::cout << "Enter two letters to be compaired with the char string '" << mychar << "'" << std::endl;
	
	std::cin >> letters;

	int count = compareChar(mychar, letters);
	std::cout << "The pair '" << letters << "' appears " << count << " time(s) in " << mychar << "." << std::endl;

	std::cout << "Enter another two letter to be compaired with the string string '" << myWord << "'" << std::endl;

	std::cin >> letters;

	count = compareString(myWord, letters);
	std::cout << "The pair '" << letters << "' appears " << count << " time(s) in " << myWord << "." << std::endl;

	std::cin.ignore(2);

	return 0;
}

int compareChar(char *myArr, char *mySecArr)
{
	int count = 0;

	for (int i = 0; i < strlen(myArr)-1; i++)
	{
		if((myArr[i] == mySecArr[0]) && (myArr[i+1] == …
JoBe 36 Posting Pro in Training

I would reverse the -1 and go with: int strLength = strlen(myArr); and for (int i = 0; i < strLength-1; i++) The reason is strLength actually represents the string length, which you can modify in the loop. And you can still use strLength elsewhere in your program if the need arises. By subtracting 1 first, it's really only useful for this particular part of the program.

Yep, that seems to make better sence, thanks Walt.

JoBe 36 Posting Pro in Training

Hmmm, well, I came up with this int strLength = strlen(myArr)-1; and then in the loop for (int i = 0; i < strLength; i++) but I see where you're coming from :o

Thanks for the example, I'll see that I can find an equivalent to use in my program next week-end.

JoBe 36 Posting Pro in Training

Hi guys,

This exercise goes as follows:
- Write a function that counts the number of occurences of a pair of letters in a string and another that does the same in a zero-terminated array of char(a C-style string). For example, the pair "ab" appears twice in "xabaacbaxabb".

Now, I found the following solution to the C-style version:

#include <iostream>

void compareChar(char *myArr, char *mySecArr);

int main()
{	
	char mychar[] = "xabaacbaxabb";
	char letters[2];

	compareChar(mychar, letters);
	
	std::cin.ignore(2);

	return 0;
}

void compareChar(char *myArr, char *mySecArr)
{
	int count = 0;

	std::cout << "Enter two letters to be compaired with the char string 'xabaacbaxabb'" << std::endl;

	std::cin >> mySecArr;
	
	while(strlen(mySecArr) != 2)
	{
		std::cout << "Wrong amount of letters, try again!" << std::endl;
		std::cin >> mySecArr;
	}

	for (int i = 0; myArr[i] != 0; i++)
	{
		if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1]))
			count++;
	}

	std::cout << "The pair '" << mySecArr << "' appears " << count << " time(s) in " << myArr << "." << std::endl;
}

I was wondering if any of you could tell me how I can improve this, the reason for asking is that at the end of the last comparison if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1])) the myArr[i+1] goes out of bound. Isn't that bad coding?

JoBe 36 Posting Pro in Training

Hey guys,


I really appreciate the effort and time you take out to help me become a better coder :!: I know I'm not the best and it takes alot of time, but, I'm having so much fun in learning this(even if they are simple programs).

I also, worked out the pseudo code WaltP gave me and turned it into this:

#include <iostream>

int main()
{
	char words[5][10];

	for (size_t i = 0; i < 5; ++i)
		std::cin >> words[i];
	std::cout << std::endl;


	for (size_t i = 0; i < 5; i++)
	{
		bool contr = true;
		for (size_t j = i+1; j < 5; j++)
		{
			if(strcmp(words[i], words[j]) == 0)
			{
				contr = false;
				break;
			}
		}
		if(contr == true)
			std::cout << words[i] << '\n';
	}
	std::cin.ignore(2);

	return 0;
}

I think it differs abit from the pseudo code he gave me, but, it works perfectly. In his pseudo code, he mentions to loop from first to last minus one, wich I "think" means this: for (size_t i = 0; i < 4; i++) . But, that doesn't seem to work for me.