Agni 370 Practically a Master Poster Featured Poster

in case you are wondering, why you haven't got any useful replies, let me tell you. The question is vague, you have not provided any main function so we don't know which functions you want to call, how is your input stored and why you are not able to pass it to your class functions, you have started your post with a node::add_coupons function and provided several other function implementations which do not seem related to your question.

Agni 370 Practically a Master Poster Featured Poster

I don't thing it is overly complicated, seems neat to me. You could divide the class declaration and function definitions in different files, .h and .cpp and main could be in another 3rd file which includes graph.h and just uses whatever it wants to. You can define the structs edges and vertex as part of the graph class itself, since that is the one that uses them.

Agni 370 Practically a Master Poster Featured Poster

Oh that's nice! Thanks a lot I will try it out in a minute.

I think I'll go with vectors because vectors seem a bit simpler

Yes, that would be a good way to do it.

Agni 370 Practically a Master Poster Featured Poster

A very quick answer: You will have to use dynamic arrays

int* arr = new int[word.length];
Agni 370 Practically a Master Poster Featured Poster
int main()
{
	// Request and obtain valid decimal numbers
	int A = obtaindec('A');
	int B = obtaindec('B');

	// Convert A and B to binary and store as vector<char> arrays

	vector<char> Abin = dectobinstring('A', A);
	vector<char> Bbin = dectobinstring('B', B);

	// Create C based on the largest size of the other two
	vector<char> Cbin;
	if ( Abin.size() > Bbin.size() )
		Cbin.resize(Abin.size());
	else
		Cbin.resize(Bbin.size());
	int Cbinsize = Cbin.size();
	for ( unsigned int x = 0; x < Cbinsize; ++x )
		Cbin[x] = 48;

	// Now recalculate C as the result of relational operations
	// on A and B

	for ( unsigned int x = 0; x < Cbinsize; ++x )
		if ( Abin[x] == 49 && Bbin[x] == 49 )
			Cbin[x] = 49;

	cout << "\n\nA & B = ";
	 for ( unsigned int x = 0; x < Cbinsize; ++x )
		cout << Cbin[x];
	cout << endl;
}

Consider Abin to be of size 10 and Bbin to be of size 8 now according to your code since Abin.size() > Bbin.size(), Cbin is resized to Abin.size(), hence 10

Then jump to line 25, you have a loop which goes upto CbinSize, which is 10 and you end up accessing Bbin[8] which could either throw an error or end up giving out some garbage.

Jetsetivan commented: Concise help, thank you! +1
Agni 370 Practically a Master Poster Featured Poster

and shouldn't this

AdditionalWindows(*wxWindow parent);

be

AdditionalWindows(wxWindow* parent);

?

Agni 370 Practically a Master Poster Featured Poster

So your comparison is right but you need to declare sGuess as a 'char' data type. Then you can compare sGuess with sSelectedWord

char sGuess
Agni 370 Practically a Master Poster Featured Poster
void mainSPGameFunc() {

	readFileGetRandom();
	string sSelectedWord = readFileGetRandom(); 
	string sGuess;

	int iWordLength = sSelectedWord.length();
		
	cout << "Guess the word!\n";
	for(int i = 0; i < iWordLength; i++) {
		cout << "_ ";
	}
	
	cin >> sGuess;

	[B]for(int i = 0; i < iWordLength; i++) {
		if(sGuess == sSelectedWord[i]) {
			cout << "RIGHT!";
		}
		system("PAUSE");
	}[/B]
}

Since sGuess and sSelectedWord both are strings you could compare them by simply

if (sGuess == sSelectWord)

by doing sSelectedWord you actually point to the character that position in the string

Edit: just re-read your post..

and I set a for loop up to check if sGuess is the same as any letter in sSelectedWord

Do you want to compare sGuess with any letter of sSelectedWord? Not sure then what that means :( .. the answer above might not help. Probably you need to declare sGuess as a 'char'. Can you explain a bit more in detail?

Agni 370 Practically a Master Poster Featured Poster
template <typename T, typename LESS_THAN>
void RestrictedSet::insert (const T elem) 
{
  _S.insert (elem);
  if (++_size > _max_size) drop_worst(); 
}

The problem must be somewhere in the template part, I get the error:

agenda.cpp:28: error: ‘template<class T> class RestrictedSet’ used without template parameters
agenda.cpp:28: error: variable or field ‘insert’ declared void
agenda.cpp:28: error: expected nested-name-specifier before ‘T’
agenda.cpp:28: error: expected ‘(’ before ‘T’

I also tried variants like:

void <T, LESS_THAN> RestrictedSet::insert (const T elem)

Hope someone can help!

When you define a template function outside the class body you need to put the template types after the class name too,

RestrictedSet<T,LESS_THAN>::insert

secondly your insert statements signature doesn't match in the class body and definition, one uses a reference.

As an aside, Usually it is better to place the template function definitions in the same file as the class declaration and not make a different .cpp for it since most compilers do not implement a separation model for template compilation, atleast i tried on g++ 4.4.1 and it was not working.

Agni 370 Practically a Master Poster Featured Poster

Hi,

This is a very convoluted problem I spent hours debugging to find that when I call std::vector<object>::push_back(Object()) destructor of previous Object is called.

Is this the behaviour of std::vectors or is something wrong with my code?

It could be happening due to resizing of the vector. Once the limit of the vector exceeds it would allocate new storage, copy all the previous objects to the new storage and then delete the old one's. Try putting come cout in copy ctor and see it that's what's happening

this is obviously supplement to all the other advice niek_e has given.

Agni 370 Practically a Master Poster Featured Poster

Since sm_fileData is static member, wherever you are defining it, at that point the default ctor of class QFile is getting called and sm_fileData is getting initialized. You cannot call another ctor on it at any later point of time. You can try using the QFile's open function (cross check the documentation for this, i'm not 100% sure of the available fns) to attach it to the user given file name, if you know the file name at compile time, just initialize it with it at the first instance.

Agni 370 Practically a Master Poster Featured Poster

It doesn't look like C++ code at all. And the moderators might prove that in sometime by moving it to the C forum !!

Agni 370 Practically a Master Poster Featured Poster

InvalidArgumentException::InvalidArgumentException(class InvalidArgumentException const &)" (??0InvalidArgumentException@@AAE@ABV0@@Z)

I think the problem is that your copy ctor is declared but not defined and somewhere you are trying to make a copy of your class 'InvalidArgumentException' .

InvalidArgumentException(const InvalidArgumentException&);

so you could either provide a copy ctor definition or remove any copy attempts from your code or remove this declaration from your code so that the compiler generates it for you when needed

Agni 370 Practically a Master Poster Featured Poster

I must be missing something, how do I properly return a 2-d string array from a function?

you could do something like

typedef string (*StrArrPtr)[numCols];

StrArrPtr get_elements()
{

   //Do your stuff, string arr[numrows][numcols]

 return arr;
}

words of caution: Make sure you are not returning address of a local array variable.
and try avoiding using arrays in general, use vectors if you can.

Agni 370 Practically a Master Poster Featured Poster

So basically this is all that is required?

~live() { if (_next) delete _next; }

for the destuctor?

you don't need the 'if' either, if _next is NULL then delete will not create any problems.

Agni 370 Practically a Master Poster Featured Poster

you can write a function to check if the value is equal to any of those given values and return a bool value

for example:

bool checkValue(char c)
{
    return ((c == 'r') || (c == 'p') || (c == 'g') || (c == 'm') );
}

//and then call it in the while loop like

while(checkValue(g.one) || checkValue(g.two) || checkValue(g.three))
{
 //do something
}
FancyShoes commented: helped me with do while problem +1
Ancient Dragon commented: Excellent suggestion :) +25
Agni 370 Practically a Master Poster Featured Poster
while( (g.one != 'r') || (g.two != 'p') || (g.three != 'g') || (g.four != 'm'))

that's how you give multiple values, you can modify it to suit your requirement

Agni 370 Practically a Master Poster Featured Poster
//input/output functions
    
	//void read();
	friend istream &operator>>(istream &str, Date&d)
	{
		char skip_char;

		str >> mn >> skip_char >> dy >> skip_char >> yr;
	}

The compiler might be asking you to access 'mn' , 'dy' , 'yr' using the Date reference.
something like

str >> d.mn ...
Agni 370 Practically a Master Poster Featured Poster

By binomial coefficient you mean finding the combinatorial number, is that right ? So you should be using the formula n!/(n-k)!*k!

for this the pseudocode should be

numerator=1*2*3..*n
fact1 = 1*2*3...*n-k
fact2 = 1*2*3....*k
denominator = fact1 * fact2
binomial coefficient = numerator/denominator

can you code that?

Agni 370 Practically a Master Poster Featured Poster

start quote.

for (int n = 0; n < adj2.length; n++) {
    for (int m = 0; m < adj2.length; m++) {
        for (int i = 0; i < adj2.length; i++) {
            for (int j = 0; j < adj2.length; j++) {
                path += adj[i][j]*adj[j][i];
            }
            if (path > 0) {
                adj2[n][m] = 2;
            }
        }
    }
}

end quote.

Just a suggestion: Don't you think this loop is a bit too nested? you could make your code much readable if you simplified these nested loops.

Agni 370 Practically a Master Poster Featured Poster

The null is because it's reading past the last line of the file. So when it has read the last line the 'while' is true, then it reads the next line(one after last one) then when you access the length of tmp you get a nullpointer exception. re-write your loop to prevent that.

As a future strategy, try to put a lot of prints and you might be able to figure out such bugs very quickly.

Agni 370 Practically a Master Poster Featured Poster

upload the data files if they are not too big, we'll see if we can help.

Agni 370 Practically a Master Poster Featured Poster

'tmp' string might be null. put a check like this after you do the 'readLine' and try

if(tmp != null)
{
//your code
}
freelancelote commented: thanks +2
Agni 370 Practically a Master Poster Featured Poster

Is this the exact code you compiled? And is that the only error you got? Your code has a lot of other compiler errors too as of now. It'll be good if you paste the entire code after you remove all the errors that you can and then we'll be able to help you better with the one's remaining.

Agni 370 Practically a Master Poster Featured Poster

you need to pass the reference to the pointer to make it work. You are just modifying the copy of the pointer here. So the changes will not be visible once you leave the fn.

fn signature could be

void to_lower(const char*& s)

while calling you could do

char s[] = "Test";
const char* p = s;
to_lower(p);
cout << p << endl;
Agni 370 Practically a Master Poster Featured Poster

If you are compiling them one-by-one make sure you only compile and don't go to the linking process. I think you might be doing that and while trying to create an executable its not finding main. Read the compiler man pages and find the flag used to only compile and not link. Once everything is compiled you can link the object files together and create the executable.

Salem commented: Seems about right to me. +36
Agni 370 Practically a Master Poster Featured Poster

Why don't you post the problem statement first. Then explain how you are trying to achieve it in your code. Then as I said, read about data-types. how did you find out about c_str fn? And I posted the correct function signature for atoi in my post above, does it have 2 parameters? Did you even read that?

Agni 370 Practically a Master Poster Featured Poster

Hmmm.. don't know where to start from. First things first, 'num' is of type 'char', do you have a class 'char' which has a fn c_str defined? No. Its a fn of the 'string' class. Then you didn't even read my earlier post properly.
I guess you need to read up the chapters on the 'data types' a couple of times over. Read about int,char,double etc. Then read about arrays, chars and pointers. And then about the 'string' class. And then come back to this problem again.

Agni 370 Practically a Master Poster Featured Poster

1->atoi signature is this:

int atoi ( const char * str );

2-> read some more threads here to find out alternatives to atoi. You should avoid using it.

Salem commented: A valiant effort, but I'm afraid the OP hasn't learn anything from any of their other posts either. +36
Agni 370 Practically a Master Poster Featured Poster

Well i copied your class, created a class definition out of whatever was given and used it and it worked fine for me.

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

class RupLogger
{
	public:
		RupLogger();
		~RupLogger();
		void log(std::string msg);
		std::ofstream logfile;
};

RupLogger::RupLogger()
{
	logfile.open("log.txt", std::ios::out);
}

RupLogger::~RupLogger()
{
	logfile.close();
}

void
RupLogger::log(std::string msg)
{
	logfile << (msg + " \n");
}

int main()
{
	RupLogger().log("Hi there");
}

Don't see any reason why it should crash. Could it be something else causing the crash. Did you check the core file?

Agni 370 Practically a Master Poster Featured Poster

My bad, i didn't see the function signature properly, you're returning by value so the copy ctor will get called.

However, in Union(), if setB has less items than the first set then the for loop will be trying to access invalid memory because setB.items will not exist when i >= setB.maxItems.

that doesn't seem to be so because

maxItems = sB.maxItems;

and hence the loop will never go out of bounds for sB.items.
can you post the entire code if it's not too big, or alteast a compilable portion of it.

Agni 370 Practically a Master Poster Featured Poster

If you are passing the address of result then it's definitely a problem because you have not allocated 'result' on the heap. It's a local variable in the function Union and it disappears when we exit the function, if you try to access that address you can get run-time errors.

Agni 370 Practically a Master Poster Featured Poster

The only draw back is that at no point of time, the value of any expression exceed one digit.

unfortunately then it would fail to evaluate his original expression of
264+* ...right?

Agni 370 Practically a Master Poster Featured Poster

Not allowed to use the <string> header?

Agni 370 Practically a Master Poster Featured Poster

It worked for me without any problems.

Agni 370 Practically a Master Poster Featured Poster

malloc returns a void* to the memory allocated and you need to ensure that you type cast it to the correct type.

Agni 370 Practically a Master Poster Featured Poster

There's a post at the top of the forum Read This before posting. It talks about using code tags and posting well indented and easy to read code. Please go through it.

Nick Evan commented: sounds like good advice to me :) +12
Agni 370 Practically a Master Poster Featured Poster

I think the problem is that you are not setting the 'next' pointer of the last node to NULL. It cab be containing some garbage and giving unexpected results. Can you try this.

void add_to_end(int data)
{
	cout << "add to end" << endl;
	node *temp;

	if (hd == NULL)
	{
		node *new_node;
		new_node = new node;
		new_node->data = data;
		[B]new_node->next = NULL[/B];
		//if I omit the next line then the print function works fine
		hd = new_node;
	}
	else
	{
		for (temp = hd; temp->next != NULL; temp = temp->next){
			cout << "for loop" << endl;
		}

		node *new_node;
		new_node = new node;
		new_node->data = data;
		[B]new_node->next = NULL[/B];
		//if I omit the next line then the print function works fine
		temp->next = new_node;
	}
}

edit:concurrent replies i guess. hope you got the point.

Agni 370 Practically a Master Poster Featured Poster
for(j=0;j<5;j++) 
{
      cout<<"The name List\n"<<name<<"\n";  
}

somehow i didn't feel like replying because it seems like you didn't try to debug this on your own even once before shooting a post here. But just in case its genuine, while outputting also you need to use the subscripts, like name[j].

next time please post well indented code with code tags for specific language.

Agni 370 Practically a Master Poster Featured Poster

fileList = new std::ofsteam(argv);

and you can change 'ofsteam' to 'ofstream' if its like that in your original code too.

Agni 370 Practically a Master Poster Featured Poster

Why don't you write a test program to check it?

Agni 370 Practically a Master Poster Featured Poster

you can use the istringstream class and convert string to int

string a = "byebye";
istringstream s(a);
int i = 0;
s>>i 
...

I think you can try this..

Agni 370 Practically a Master Poster Featured Poster

I would say you can use the following algorithm

1> convert both the arrays to all upper case or all lower case
2> sort both the arrays
3> compare the contents
4> at first mismatch print 'its not an anagram' and exit
5> else print 'its an anagram'

There will be many shorter one's but this is easy to implement.

homeryansta commented: awesome guy! +1
Agni 370 Practically a Master Poster Featured Poster

>It will then store the alphabetical character into an array ignoring all other character
>compare to see if they have the same alphabetical characters in there.

so first you fetch the alphabets from the input and store it in an array and then check array for alphabets again?? it doesn't make sense to me

do you mean if the alphabets stored in array make any word it will say its an 'anagram'?

Agni 370 Practically a Master Poster Featured Poster

>it is not giving me the right answer

and what is the right answer?? what is the program supposed to be doing?? The header doesn't say what it does and i don't want to check the code to find that out.

Agni 370 Practically a Master Poster Featured Poster

for a 2-dim char array values can be extracted like

char *a[] = {"tom","som"};
cout << a[0] << "  " << a[1] << endl;
Agni 370 Practically a Master Poster Featured Poster

where's the code for TestAccount.cpp?

Agni 370 Practically a Master Poster Featured Poster

well when you create the object of the child class the base class ctor gets called and since you have declared your own ctor the compiler doesn't give you a default ctor. In the definition of the child class ctor you should pass the required value to the base class ctor.

something like

SavingsAccount(double initBalance, double initInterestRate):Account(initBalance) {
			InterestRate = initInterestRate;
			Balance = initBalance;	
		};
Agni 370 Practically a Master Poster Featured Poster

1> i dont know if this will work(i mean the vector thing, not struct way). but assuming it does,
one issue i see with using a vector is if you have to verify that a particular point is on the right path or not. Suppose you have a maze and someone is trying to go through it and you need to verify that he's on the right path how do you do that? do you fetch each element which is an array and then compare each element of that array with the present co-ordinates? That would be pretty slow.
3> May be we can create a string of x-y coordinates.
row = 2, col =3
create a string "0203" and store strings in vectors, comparison might also be easier.
4> All these are as per my understanding of the problem ;)

Agni 370 Practically a Master Poster Featured Poster

Header required for 'TButton' is missing i think.