killerqb 0 Newbie Poster

I seem to have an error updating a checkbox using an SQL Query.
I am trying to get the box to be checked if equal to 1 and unchecked if equal to 0.

My database is updating correctly, but the checkbox is not.

<input type="checkbox" <?php if ($row['create_event'] = 1){ echo "checked='yes' value='check'";} else{ echo "checked='no' value='unchecked'";} ?> name="option1" > Create Event<br>

I call

echo $row['edit_event'];

before executing this line and it returns 0

I call

echo $row['edit_event'];

after executing this line and it returns 1

This makes me think that my if statement is really updating that row instead of performing a check. Any suggestions would be greatly appreciated.

killerqb 0 Newbie Poster

This was actually the original function I found online, but I was trying to modify it in order to get it to work with my code. However I am unfamiliar with size_t and the U at the end of the InitialFNV initialization.
So can someone explain the U and size_t please.

static const size_t InitialFNV = 2166136261U;
static const size_t FNVMultiple = 16777619;

/* Fowler / Noll / Vo (FNV) Hash */
size_t myhash(const string &s)
{
    size_t hash = InitialFNV;
    for(size_t i = 0; i < s.length(); i++)
    {
        hash = hash ^ (s[i]);       /* xor  the low 8 bits */
        hash = hash * FNVMultiple;  /* multiply by the magic number */
    }
    return hash;
}
killerqb 0 Newbie Poster

I was trying to play around with the FNV function but my program keeps seg faulting

static const unsigned InitialFNV = 2166136261;
static const unsigned FNVMultiple = 16777619;

// /* Fowler / Noll / Vo (FNV) Hash */
unsigned HashTable::hashFunc(const string &s)const
{
    unsigned hash = InitialFNV;
    for(unsigned i = 0; i < s.length(); i++)
    {
        hash = hash ^ (s[i]);       /* xor  the low 8 bits */
        hash = hash * FNVMultiple;  /* multiply by the magic number */
    }
    return hash;
}
killerqb 0 Newbie Poster

Yes, sorry I meant speed by optimize, I guess I did not clarify. I'm kinda new to hash functions.

killerqb 0 Newbie Poster

Thanks, but when I implemented your hash function it took nearly twice as long. Any other functions I should give a whirl?

killerqb 0 Newbie Poster

I am trying to optimize my hash function, for a separate chaining hash table that I have created from scratch. My hash function is as follows:

int hash( const string &key, int tableSize)
        {
            int hashVal = 0;
			for(int i = 0; i<key.length();  i++)
			hashVal = 37*hashVal+key[i];
			hashVal %= tableSize;
			if(hashVal<0)
			hashVal += tableSize;
			return hashVal;
        }

I was wondering if one could point me to either a better hash function or how I should go about modifying this function.

killerqb 0 Newbie Poster

Im sorry im not sure what your edit exactly means

killerqb 0 Newbie Poster

My compiler is throwing me this error when I try to make a new one.
I am trying to make it with this

string a = "five";
   HashTable<string> *hashy =  new HashTable<string>(a,100);

This is the compiler error
(.text+0x1d3): undefined reference to `HashTable<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::HashTable(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
collect2: ld returned 1 exit status

//
#include "hashTable.h"
#include <iostream>
#include <list>
#include <string>
#include <math.h>
#include <iterator>
bool checkforlargeprime (int num);
 
bool checkprime (int nn) {
    if (nn > 100) {
        return checkforlargeprime(nn);
    } else {
        int k=2;
        while (k < nn) {
            int sd = nn%k;
            if ( sd == 0) {
                return false;
            }
            k++;
        }
    }
    return true;
}
int getNextPrimeNumber (int num) {
    int nam = num+1;
    bool das = true;
    while ( das == true ) {
        if (checkprime(nam))
            das = false;
        else
            nam = nam+1;
    }
    return nam;
}
bool checkforlargeprime (int num) {
    if (num > 100) {
        int sss = ((int)(sqrt((double)num)))+1;
        int pn = 2;
 
        while (pn < sss) {
            if (num%pn == 0) {
                return false;
            }
            pn = getNextPrimeNumber(pn);
        }
        return true;
    } else {
        return false;
    }
}
 
template <class HashedObj>
        HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size )
          : item( notFound ), theLists( getNextPrimeNumber( size ) )
        {
        }
template <class HashedObj>
        void HashTable<HashedObj>::insert( const HashedObj & x )
        {
            list<HashedObj> &whichList = theLists[hash(x, theLists.size())];
            typename std::list<HashedObj>::iterator itr = whichList.find(x);
 
            if( itr.isPastEnd( ) )
                whichList.insert( x, whichList.zeroth( ) );
        } …
killerqb 0 Newbie Poster

Thanks, did that now, but now it gives me a compiler error of
undefined reference to `HashTable<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::HashTable(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
collect2: ld returned 1 exit status

killerqb 0 Newbie Poster

When I declare a new Hashtable such as:
String a = null;
HashTable<string> = new HashTable(a, 100);

It tells me HashTable is not a type.

#ifndef HASHTABLE_H
        #define HASHTABLE_H
#include <iostream>
#include <vector>
#include <list>
using namespace std;
template <typename HashedObj>
 class HashTable
 {
 public:
 explicit HashTable( const HashedObj & notFound, int size = 101 );
 HashTable( const HashTable & rhs ): item( rhs.item ), theLists( rhs.theLists ) { }
 void makeEmpty();
 void insert(const HashedObj &x);
 void remove(const HashedObj &x);
 bool contains(const HashedObj &x)const;
 const HashedObj & find( const HashedObj & x ) const;
private:
const HashedObj item;
int size;
vector<list<HashedObj> > theLists;
};
template <typename HashedObj>
int hash(const HashedObj &x, int tablesize);
#endif
//
#include "hashTable.h"
#include <iostream>
#include <list>
#include <string>
#include <math.h>
#include <iterator>
bool checkforlargeprime (int num);

bool checkprime (int nn) {
    if (nn > 100) {
        return checkforlargeprime(nn);
    } else {
        int k=2;
        while (k < nn) {
            int sd = nn%k;
            if ( sd == 0) {
                return false;
            }
            k++;
        }
    }
    return true;
}
int getNextPrimeNumber (int num) {
    int nam = num+1;
    bool das = true;
    while ( das == true ) {
        if (checkprime(nam))
            das = false;
        else
            nam = nam+1;
    }
    return nam;
}
bool checkforlargeprime (int num) {
    if (num > 100) {
        int sss = ((int)(sqrt((double)num)))+1;
        int pn = 2;

        while (pn < sss) {
            if (num%pn == 0) {
                return false;
            }
            pn = getNextPrimeNumber(pn);
        }
        return true;
    } else { …
killerqb 0 Newbie Poster

I know an AVL Tree guarantees a time of log(n), but what is the time cost of the actual re balancing of the tree and why is this ignored?

killerqb 0 Newbie Poster

Well im not declaring it with new in the header file, so I guess, but I'm not exactly sure. I thought maybe because it was in the header it would be different.

killerqb 0 Newbie Poster

My code is an expression tree program, in which I make treenodes pointers and push them in the end all into the 1st spot of a stack, in which is declared in my header file of the program. I wipe all the treenode pointers with a recursive traversal. Is this enough to prevent a memory leak or can the stack be deleted as well?

killerqb 0 Newbie Poster

I'm creating an expression tree that reads in postfix notation and converts it and prints it out into infix notation. I was wondering how would I go about correctly distributing parentheses and balancing them properly.

killerqb 0 Newbie Poster

I'm having a problem reading in a file to a scanner, it is supposed to take in a filename and then evaluate whether it is a URL or filename, but it having trouble reading just a local file.

public boolean loadSongs(String filename) {
		
		Scanner load1 = null;
		if (filename.contains("http")) {

			try {
				URL theFile = new URL(filename);
				load1 = new Scanner(theFile.openStream());
				while (load1.hasNextLine()) {

					String tempName = load1.nextLine();
					while (tempName.length() == 0) {
						tempName = load1.nextLine();
					}
					if (tempName.contains(" //")) {
						int slash = tempName.indexOf(" //");
						tempName = tempName.substring(0, slash);
					}
					String tempArtist = load1.nextLine();
					while (tempArtist.length() == 0) {
						tempArtist = load1.nextLine();
					}
					if (tempArtist.contains(" //")) {
						int slash = tempArtist.indexOf(" //");
						tempArtist = tempArtist.substring(0, slash);
					}
					String time = load1.nextLine();
					while (time.length() == 0) {
						time = load1.nextLine();
					}
					if (time.contains("//")) {
						int slash = time.indexOf("//") - 1;
						time = time.substring(0, slash);
					}

					int colon = time.indexOf(":");
					String min = time.substring(0, colon);
					int intmin = Integer.parseInt(min);
					String sec = time.substring(colon + 1);
					int intsec = Integer.parseInt(sec);
					if (intsec >= 60) {
						int minute = intsec / 60;
						intmin = intmin + minute;
						intsec = intsec - (60 * minute);
					}
					String stringURL = load1.nextLine();

					Song song = new Song(tempName, tempArtist, intmin, intsec,
							stringURL);
					this.songList.add(song);

				}

			} catch (IOException e) {
				System.out.println("could not access URL");
				e.printStackTrace();
				return false;
			}
		} else {
			// File theFile = new File(filename);
			File theFile1 = new File(filename);
			
			try {
				
				Scanner load = new Scanner(theFile1);
				while (load.hasNextLine()) {

					String tempName = load.nextLine();
					while …
killerqb 0 Newbie Poster

Nvm, got it wasn't compiling it with the stack.cpp, whoops, thank you so much

killerqb 0 Newbie Poster

thanks, but now its throwing me undefined reference errors when I try to run it

killerqb 0 Newbie Poster

Here's the header of the postfix

#ifndef POSTFIXCALCULATOR_H
#define POSTFIXCALCULATOR_H
#include <iostream>
#include <stack>
using namespace std;





class postfixCalculator {
	public:
  int  evaluate(string word);			
	
};
#endif

This code worked when using #include <stack>, but I'm trying to implement my own

#include <iostream>
#include "postfixCalculator.h"
#include "stack.h"
using namespace std;

stack<int> stacky;
string calc;
string a;
int x;
int y;
int i;

int postfixCalculator::evaluate(string word){
  calc = word;
  while(calc.length()!=0){
   i = calc.find(" ");
 if( i >0 ){
  a = calc.substr(0,i);
 calc = calc.substr(i+1); 

 if(a.length() > 1)
   stacky.push(atoi(a.c_str()));
 else{
 switch(a[0]){
      case '+':      
       x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << x + y << endl;
        stacky.push(x + y);
        break;
      case '-': 

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << y - x << endl;
        stacky.push(y - x);
        break;
      case '*':

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << x*y << endl;
        stacky.push(x*y);
        break;
case '~':

        x = stacky.top();
        stacky.pop();
	// cout << stacky.top() << endl;
        stacky.push(x*-1);
        break;
      case '/':                                                                          

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << y/x << endl;
        stacky.push(y/x);
        break;
      default:
        stacky.push(atoi(a.c_str()));        
        break;
 }
 }
  }
else{
 switch(calc[0]){
      case '+':      
       x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << x + y << endl;
        stacky.push(x + y);
        break;
      case '-': 

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << y - x << endl;
        stacky.push(y - x);
        break;
      case '*':

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
        //cout << x*y << …
killerqb 0 Newbie Poster

The stack.cpp is compiling fine now thank you, but when I try to implement it in my post fix calculator program it doesn't work. It goes back to the header file, but then claims that stack is not a template and the use of stack is ambgious and first declared as 'template<class T> struct stack' here. Should I add my postfix implementation

killerqb 0 Newbie Poster

I'm having a problem with creating my own stack class and implementing it in a postfix calculator program I made. I was trying to make the stack.h file, but it keeps telling me stack is not a template

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;

template <typename T>
class ListNode
{
public:
  ListNode();	
private:
    T value;
    ListNode<T> *next;
friend class  stack<T>;
    	
};

template <class T>
class stack
{
public:
  stack( ); 
  ~stack( ); 
  void push(T const&);
void makeEmpty();
  bool isEmpty() const;
  void pop();
 T top();
  
 private:
  ListNode<T> *topOfStack;

};
#endif

and here is my implementation:

#include <iostream>
#include "stack.h"
using namespace std;


template<typename T>
ListNode<T>::ListNode(){
  next = NULL;
 value = NULL;
};


template<typename T>
stack<T>::stack(){
  topOfStack = NULL;
};
template<typename T>
stack<T>::~stack(){
  makeEmpty();
}
template<typename T>
T stack<T>::top(){
  return topOfStack->value;
}
template<typename T>
void stack<T>::pop(){
  ListNode<T> *newnode = new ListNode<T>();
  topOfStack = topOfStack->next;
  delete newnode;
}
template<typename T>
bool stack<T>::isEmpty()const{
  return topOfStack==NULL;
}
template<typename T>
void stack<T>::makeEmpty(){
  while(!isEmpty()){
    pop();
  }
}

template<typename T>
void stack<T>::push(T const& x){
  ListNode<T> *newnode = new ListNode<T>();
  newnode->value = x;
  newnode->next = topOfStack;
  topOfStack = newnode;
}

int main(){
  return 0;
}

This is my first time working with templates so im not sure if its right