The "IT DOESN'T WORK!" thread

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
chridd
Posts: 698
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby chridd » Wed Feb 05, 2014 9:38 am UTC

Diadem wrote:A related question, something strange I discovered while investigating the above. Take the following code:

Code: Select all

template <typename T>
T testfunction(T t) {
    return t;
}

template <>
int testfunction<int> (int t) {
    return 3;
}

template <>
int testfunction<double> (double t) {
    return 4;
}

The first two functions compile, but the third one does not. The error the compiler gives is: "error C2785: 'T testfunction(T)' and 'int testfunction(double)' have different return types ". So that's not allowed. But why? I keep staring at that error message and thinking "So what? Who gives a damn".
Did you mean "double testfunction<double> (double t)"? int testfunction<double>(double) doesn't follow the pattern of having the return type and the parameter be the same type, which was established in the first definition. (Unless you're trying to make an exception to the rule for that one; I'm not sure if that's possible.)
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics? What phonotactics?) · ze or they · Forum game scores
It's spring; Easter has to be sometime around now, right?
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
Diadem
Posts: 5585
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Diadem » Wed Feb 05, 2014 12:07 pm UTC

chridd wrote:Did you mean "double testfunction<double> (double t)"? int testfunction<double>(double) doesn't follow the pattern of having the return type and the parameter be the same type, which was established in the first definition. (Unless you're trying to make an exception to the rule for that one; I'm not sure if that's possible.)

No. I meant what I wrote. I know it doesn't follow the same pattern, which is why it doesn't compile. I just don't understand why the compiler rejects it (other than it being the standard). There is no ambiguity. Anyway that is a minor issue, just curiosity on my part. I'm more interested in resolving vector template specialization question.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

EvanED
Posts: 4305
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Wed Feb 05, 2014 2:20 pm UTC

Did you try this? Because I didn't, but I suspect it'll work. I also suspect if it doesn't, then you can't. :-)

Code: Select all

// primary
template<typename T>
T dothingy() {};

// partial specialization
template<typename T>
vector<T> dothingy() {};


The difference between a partial and explict (often called "total", but the standard uses the term "explicit") specialization is really easy... if you have template<>, it's explicit. If you have template<...args...> with a non-zero number of args, then you have a partial specialization.

Importantly, the args in a partial specialization don't really have anything to do with the template args in the primary template, and you should always look at the primary template to figure out how to call it. (That's the perhaps-confusing part.) E.g. dothingy<vector<double>> should call the specialization in my example even though the template parameter in the specialization is going to be double.

If it doesn't work and re. your other question, you could try GCC too. Might be an MSVC deficiency.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11018
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Wed Feb 05, 2014 3:08 pm UTC

There is no partial specialization of function templates. There is just overloading. Often overloading acts like you would expect partial specialization would work, including close-enough syntax, so people do not notice.

There is partial specialization of class templates. There is no overloading of class templates.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Flumble
Posts: 1697
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Wed Feb 05, 2014 3:46 pm UTC

Diadem wrote:No. I meant what I wrote. I know it doesn't follow the same pattern, which is why it doesn't compile. I just don't understand why the compiler rejects it (other than it being the standard).

Since it's a template specialization (template<>) of the template T testfunction(T ) (or t->t in functional notation), it must be of the form int testfunction(int) or double testfunction(double). Anything of the form t->u with t≠u is unacceptable within the template's rules, so it can't compile.

The suggestion by EvanEd leaves the compiler puzzled, unfortunately:

Code: Select all

templatetest.cpp: In function 'int main()':
templatetest.cpp:20:32: error: call of overloaded 'dothingy(int)' is ambiguous
templatetest.cpp:7:24: note: candidates are: T dothingy(int) [with T = std::vector<int>]
templatetest.cpp:13:32: note:                 std::vector<T> dothingy(int) [with T = std::vector<int>]

source:
Spoiler:

Code: Select all

#include <vector>
#include <iostream>

using namespace std;

// primary
template<typename T> T dothingy(int x) {
   cout << "primary selected" << endl;
   return static_cast<T>(x); //don't judge me
}

// partial specialization
template<typename T> vector<T> dothingy(int x) {
   cout << "overload selected" << endl;
   return static_cast<vector<T> >(x); //especially don't judge me here
}

int main() {
   vector<int> vec;
   vec = dothingy<vector<int> >(5);
   return 0;
}

User avatar
Yakk
Poster with most posts but no title.
Posts: 11018
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Wed Feb 05, 2014 4:01 pm UTC

Yes, because those are two overrides, and there is nothing from the signature to determine which you want to call

Code: Select all

    template<typename V> struct is_vector:std::false_type {};
    template<typename T, typename A> struct is_vector<std::vector<T,A>>:std::true_type {};

    template<typename T, typename=typename std::enable_if< !is_vector<T>::value >::type>
    T dothingy() { return {}; };

    template<typename V, typename=typename std::enable_if< is_vector<T>::value >::type>
    V dothingy() { return{}; };

creates two overrides, one of which handles vectors, the other handles other types.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

EvanED
Posts: 4305
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Wed Feb 05, 2014 4:08 pm UTC

Yakk wrote:There is no partial specialization of function templates. There is just overloading. Often overloading acts like you would expect partial specialization would work, including close-enough syntax, so people do not notice.
I forgot about that fact, and have been tripped up by it before. As a result, you won't be able to specialize your template for "all vectors". (I may have also gotten the syntax wrong for that non-existant feature: I should have said template<typename T> vector<T> dothingy<vector<T>> (int) {} I think.)

My guess is that's why my earlier suggestion didn't work -- because the template parameter isn't used in the arg list in this case, you won't be able to overload it.

The closest I can offer off the top of my head is the following: Yakk gives a more working suggestion :-)

Code: Select all

template <typename T>
T dothingy(int x, T const & dummy) {}

template <typename T>
vector<T> dothingy(int x, vector<T> const & dummy) {}

which you would then call as

Code: Select all

SomeType dummy;
dothingy(4, dummy);

instead of

Code: Select all

dothingy<SomeType>(4)
.

(Incidentally, you should really use template <typename T, typename A> and then vector<T,A>; I'm abbreviating above.)

User avatar
Yakk
Poster with most posts but no title.
Posts: 11018
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Wed Feb 05, 2014 4:33 pm UTC

If you really want specialization based pattern matching, forward.

Code: Select all

template<typename T>
struct real_work { int operator()() const { return 0; };

template<typename T>
auto fascade()
->decltype( real_work<T>()(); ) {
  return real_work<T>()();
}

where we forward everything to a functor class. Partial specializations of real_work can be done.

Another approach is tag dispatching:

Code: Select all

#define RETURNS(X) ->decltype(X) { return (X); } // redundant in C++1y, but required in C++11 to avoid repetition

template<typename T>
int helper( T const&, std::true_type /* is vector */ ) { return {}; }
template<typename T>
double helper( T const&, std::false_type /* is vector */ ) { return {}; }

template<typename T>
auto fascade()
  RETURNS( helper( T{}, is_vector<T>() ) )

where we make our tests explicit, then rely upon overloading on tag types to determine which final implementation we are using.

This makes the overloading extremely clear, and is how iterator category specialization in standard libraries is currently done in every case I've looked at.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Jplus
Posts: 1686
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Jplus » Wed Feb 05, 2014 5:26 pm UTC

I've had this problem before and I'm pretty sure one of the following options should do the job.

Code: Select all

template<>
template<typename T>
vector<T> dothingy<vector<T> >() {};

template<typename T>
template<>
vector<T> dothingy<vector<T> >() {};
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
Diadem
Posts: 5585
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Diadem » Thu Feb 06, 2014 3:33 pm UTC

I have to admit this is all way over my head. Hmm. time to hit the books I guess.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

User avatar
Xenomortis
Not actually a special flower.
Posts: 1356
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Xenomortis » Fri Feb 07, 2014 2:53 pm UTC

~/.vimrc

The following does not work

Code: Select all

command P27 call SetPythonVersion( python_27 )
command P3 call SetPythonVersion( python_3 )

"//SetPythonVersion creates a command to run the relevant python version


The following does work

Code: Select all

command Py27 call SetPythonVersion( python_27 )
command Py3 call SetPythonVersion( python_3 )

(as does replacing P with pretty much anything)
Image

User avatar
Yakk
Poster with most posts but no title.
Posts: 11018
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Fri Feb 07, 2014 3:08 pm UTC

Jplus wrote:I've had this problem before and I'm pretty sure one of the following options should do the job.

Code: Select all

template<>
template<typename T>
vector<T> dothingy<vector<T> >() {};

template<typename T>
template<>
vector<T> dothingy<vector<T> >() {};

There is still no partial specialization of functions. There is still only overloads of functions.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
phlip
Restorer of Worlds
Posts: 7524
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Fri Feb 21, 2014 4:42 am UTC

Any git experts in the house?

My workplace recently moved to git for its source-control needs, and there's now a very large project in a git repo that we need to use. Unfortunately we're in a little sub-office not connected to the main office where the repository is stored, so we have to fetch it over the net, over a VPN, and our Internet connection isn't that great, and the project is quite large... so it's not unheard of for it to take upwards of 15-30 minutes to do a fetch.

So, I tried to set up a mirror on a server in our office, that will regularly fetch from the head office, and then we fetch from that, which makes everything go a lot faster... This is what I'm doing, cobbled together from a number of howtos on the net:

Code: Select all

# To create the mirror:
git clone --mirror http://upstream-server/gerrit/$PROJECT $PROJECT

# To update:
git fetch origin
git remote prune origin
git update-server-info
# via a wrapper script that ensures it's only running once at a time
then pointed a webserver at the mirrored repo so we can pull from it over http.

So far, so good... except, sometimes, when I do a fetch from my machine, I get an error like:

Code: Select all

$ git fetch origin
error: Unable to find d63b6806e990f05021eec611dd254c262f810ea9 under http://10.0.0.97/gitmirror
Cannot obtain needed blob d63b6806e990f05021eec611dd254c262f810ea9
while processing commit 3c46b827333462f630cdd0f090dd1e864f2120c0.
error: Fetch failed.
At first I thought maybe it was a transient problem because I happened to be doing a fetch while the mirror was updating at the same time, or something, but I can wait until the server is definitely finished its fetch, and try it again, and I still get the same error (with the same hash being complained about).

However... if, on my local, I add the upstream head-office server as a remote, and fetch from that... don't pull anything from it, just fetch it... and then do a fetch from the mirror again, then it works. It's slow and annoying, but it runs through without errors.

Things I've tried: if I run "git cat-file" on the mirror, with the hash that the error message complains it's unable to find, it's there... that blob does exist on the mirror. I'm just not sure why the fetch on my local can't find it. I've tried running "git fsck" on the mirror, but it has no effect.

I've basically run through everything I know about git, and everything I can find via Google, and can't figure out what's breaking... if anyone knows how to fix this error, or wants to suggest a better way of setting up the mirror to avoid it, then I'd love to know...

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

EvanED
Posts: 4305
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Fri Feb 21, 2014 4:46 am UTC

I'm not going to be able to help, sorry, but what happens if you try to clone your mirror again instead of pulling from an existing repo? Same thing?

User avatar
phlip
Restorer of Worlds
Posts: 7524
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Fri Feb 21, 2014 6:21 am UTC

Yeah, I've tried that. It fixed it that one time, but eventually happened again.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Aldarion
Posts: 133
Joined: Thu Jul 19, 2007 7:00 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Aldarion » Fri Feb 21, 2014 1:32 pm UTC

Continuing this:
Aldarion wrote:In order to get perhaps a bit closer to solving this, I found a tool, available here, which may help me put a bound on the problem.
The thing is, the tool is just two .class files, with no manual or explanation how to run it.
All I know about the program is that it's supposed to get a .cnf file and spit out a number.
The developers were also kind enough not to leave any contact information whatsoever.
Now, I did study Java for my BA, but it was four years ago, and I haven't had any need to practice it. So for the last hour or so I've been trying to import these classes into a project in Eclipse in all kinds of ways to no avail, and anyway, even if I managed to do so, I've still got no idea how to run the gorram thing.

Any help would be appreciated.

e^iπ+1=0 wrote:I searched the site of the uni that the creators are from for their names and managed to find contact info for the third guy: Roland Yap.


Unfortunately, Dr. Yap didn't reply to my mail. Could anyone still help me use the software?
I'm not good, I'm not nice, I'm just right.

User avatar
phlip
Restorer of Worlds
Posts: 7524
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Tue Feb 25, 2014 9:37 am UTC

Re my git problem, and so as to not be that guy, I think I have solved the problem... somewhat. I changed my local repository to fetch from the mirror over SSH instead of over HTTP, and the error seems to have gone away... Still waiting to see if it returns at some point, but looking good so far.

Still not sure what the problem was, but my guess is that there was some stray thing in the Apache config that stopped it from being able to fetch that file, some rewrite rule or something that it just didn't like. That or maybe the bare-repository-shared-raw-over-HTTP system just isn't as reliable as one would hope.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
SpringLoaded12
Posts: 350
Joined: Wed Oct 08, 2008 1:58 am UTC
Location: Guarding the Super Missile
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby SpringLoaded12 » Sat Mar 01, 2014 11:27 am UTC

I'm struggling with a bit of JavaScript.
The code is simple: a page is displayed with two lines of material. The first line contains a textbox and a Submit button. The second line contains another textbox.
In the page's code are variables storing integers for a minimum, maximum, and secret number. The user's objective is to guess the secret number by entering a number into the first line's textbox and clicking Submit. When they do this, their entered number is compared to the minimum, maximum, and secret number. The code then returns a message in the second textbox, saying whether the guessed number was correct, too high, too low, above the maximum, or below the minimum. It must do all this without reloading the page.

I've got most of it working below:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title>Number Guessing Game</title>

<SCRIPT TYPE="text/javascript">

var minimum = 1;
var maximum = 200;
var secretnumber = 116;
var guess;
var guessint;

function compare() {
   guess = document.getElementById("numguess").value
   guessint = parseInt(guess, 10)
   if (guessint > maximum) {
      document.getElementById("result").value = "Your guess is over the maximum.";
   }
   if (guessint < minimum) {
      document.getElementById("result").value = "Your guess is under the minimum.";
   }
   if (guessint < maximum && guessint > minimum && guessint < secretnumber) {
      document.getElementById("result").value = "Your guess is too low.";
   }
   if (guessint < maximum && guessint > minimum && guessint > secretnumber) {
      document.getElementById("result").value = "Your guess is too high.";
   }
   if (guessint == secretnumber) {
      document.getElementById("result").value = "Correct!";
   }
}

</SCRIPT>

</head>
<body>
<br />
<form name="numinput">
Enter a number: <input type="text" id="numguess">
<button onclick="compare()">Submit</button> <br />
Result: <input type="text" id="result">
</form>

</body>
</html>


The page successfully does everything it's supposed to do from receiving the input number to returning the right response, except that the page immediately reloads after returning a response, leaving the user with only a split-second to read the response before it vanishes. How do I stop this from happening?
Also, if possible for the sake of a cleaner look, how can I make the returned message appear on the page as if part of normal written text in HTML instead of being stuck inside this textbox?

This problem is making me feel like an idiot. All this time I've spent learning C, C++, Java, Racket, HTML, XML, etc. and I can't even get a damn scripting language to do basic I/O.
As a sidenote, can anyone recommend a way to get customized runtime feedback from the script, e.g. printing custom messages to the console in procedural/OO languages to determine exactly where a program fails?
"It's easy to forget what a sin is in the middle of a battlefield." "Opposite over hypotenuse, dipshit."

User avatar
Xenomortis
Not actually a special flower.
Posts: 1356
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Xenomortis » Sat Mar 01, 2014 11:52 am UTC

<button> causes postback, triggering a page reload.
Try another <input> element instead.

This works for me.

Code: Select all

<input type="button" value="Submit" onclick="compare()" />
Image

User avatar
jaap
Posts: 2047
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby jaap » Sat Mar 01, 2014 11:55 am UTC

Xenomortis wrote:<button> causes postback, triggering a page reload.
Try another <input> element instead.

This works for me.

Code: Select all

<input type="button" value="Submit" onclick="compare()" />


Or just add the type="button" attribute to the button.

Code: Select all

<button onclick="compare()" type="button">Submit</button> <br />

The default for the 'type' attribute is "submit" (at least in the browser you are using, it varies apparently) which submits the form and reloads the page.


Edited for clarity
Last edited by jaap on Sat Mar 01, 2014 12:08 pm UTC, edited 2 times in total.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1356
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Xenomortis » Sat Mar 01, 2014 11:59 am UTC

jaap wrote:at least in the browser you are using, it differs apparently

Chrome, on my laptop.
Funny, I just read a stackoverflow answer/comment that was adamant <button> wouldn't do anything without being explicitly told, and yet...
Image

User avatar
Jplus
Posts: 1686
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Jplus » Sat Mar 01, 2014 12:34 pm UTC

SpringLoaded12 wrote:Also, if possible for the sake of a cleaner look, how can I make the returned message appear on the page as if part of normal written text in HTML instead of being stuck inside this textbox?
Make a <span> instead of a <input type="text"> with id="result". Then, put text into it by assigning to its innerHTML.

As a sidenote, can anyone recommend a way to get customized runtime feedback from the script, e.g. printing custom messages to the console in procedural/OO languages to determine exactly where a program fails?
All major browsers have debuggers that let you set breakpoints etc.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
chridd
Posts: 698
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby chridd » Sat Mar 01, 2014 9:01 pm UTC

SpringLoaded12 wrote:As a sidenote, can anyone recommend a way to get customized runtime feedback from the script, e.g. printing custom messages to the console in procedural/OO languages to determine exactly where a program fails?
If it's just for testing/debugging, console.log will display a message to the JavaScript console, which you can show from the menu (at least on the versions I have, in Firefox it's Tools > Web Developer > Web Console, and in Safari and Chrome it's in the web inspector, though there may be other ways to access it).

(This might cause an error in older browsers, though.)
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics? What phonotactics?) · ze or they · Forum game scores
It's spring; Easter has to be sometime around now, right?
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
SpringLoaded12
Posts: 350
Joined: Wed Oct 08, 2008 1:58 am UTC
Location: Guarding the Super Missile
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby SpringLoaded12 » Sun Mar 02, 2014 1:53 am UTC

Thanks for the tips, guys. Combining your recommendations worked like a charm. Now the page doesn't reload, and the result text is formatted a lot better.

If anyone cares, here's the final code:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title>Number Guessing Game</title>

<SCRIPT TYPE="text/javascript">

var minimum = 1;
var maximum = 200;
var secretnumber = 116;
var guess;
var guessint;

function compare() {
   guess = document.getElementById("numguess").value
   guessint = parseInt(guess, 10)
   if (guessint > maximum) {
      document.getElementById("result").innerHTML = "Your guess is over the maximum.";
   }
   if (guessint < minimum) {
      document.getElementById("result").innerHTML = "Your guess is under the minimum.";
   }
   if (guessint < maximum && guessint > minimum && guessint < secretnumber) {
      document.getElementById("result").innerHTML = "Your guess is too low.";
   }
   if (guessint < maximum && guessint > minimum && guessint > secretnumber) {
      document.getElementById("result").innerHTML = "Your guess is too high.";
   }
   if (guessint == secretnumber) {
      document.getElementById("result").innerHTML = "Correct!";
   }
}

</SCRIPT>

</head>
<body>
<br />
<form name="numinput">
Enter a number: <input type="text" id="numguess">
<button type="button" onclick="compare()">Submit</button> <br />
Result: <span id="result"></span>
</form>

</body>
</html>
"It's easy to forget what a sin is in the middle of a battlefield." "Opposite over hypotenuse, dipshit."

User avatar
Xanthir
My HERO!!!
Posts: 5082
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby Xanthir » Sun Mar 02, 2014 6:48 am UTC

Out of curiosity, why is your <script> tag capitalized when the rest of your tags aren't? Also, throw out the type='' attribute on <script> - it doesn't do anything useful.

(It lets you opt into VBScript in IE, and certain experimental JS features in FF, but if you're just writing normal JS you don't need any type='' attribute - the <script> element is defined to contain JS by default.)

Also, note that you can declare your variables inside of your functions. You don't need to initialize "guess" and "guessint" as globals, and really don't want to.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
PM 2Ring
Posts: 3599
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: The "IT DOESN'T WORK!" thread

Postby PM 2Ring » Mon Mar 03, 2014 4:26 am UTC

Also, you don't really need the <form> element.

Forms are good when you want to submit the form data back to the server so it can be processed by a CGI program, with the results of that processing being sent back to the browser (or at least an indication that the form data was accepted by the server). But you're not doing that here - all the processing is being done the client (i.e., the browser).

PS
You could reduce the clutter in your compare() function by assigning document.getElementById("result") to a local variable, eg

Code: Select all

var resultspan = document.getElementById("result");


PPS
It's a good habit to always terminate statements in JavaScript with a semicolon. True, the interpreter will generally cope without the termination, but it will sometimes do unexpected things...
I see that most of your statements are properly terminated, apart from the two statements at the start of the compare() function, so I guess those two are probably just typos.

User avatar
Tomlidich the second
Posts: 1230
Joined: Thu Mar 22, 2012 5:38 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Tomlidich the second » Fri Mar 07, 2014 3:44 pm UTC

got this stupid PHP problem:

It is supposed to take the html form data and just store it in an SQL database, someone else is designing the rest of it:


Code: Select all

<html>
<body>

<form action="insert.php" method="post">
Keyhash: <input type="text" name="field one"><br>
validation: <input type="text" name="field two"><br>
<input type="submit">
</form>

</body>
</html>




i need it to pass the data in those two input boxes (done by an automated client someone else is building)

to a PHP page something like this, to make sure only data with the proper validation code gets through. (obviously the number will not be 12345.)

Code: Select all

 <?php
if ("fieldtwo" = 12345)
 {
$con=mysqli_connect("example.com","Field one","field two","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO Tableone (Field one)
VALUES (Field one)");

mysqli_close($con);
 }
else
 {
 echo "invalid code"
 }
?>

I haven't even built the SQL part yet, been stuck on this part all day.

I even tried getting it to print out the form data entered, it won't even do that.

im not exactly new to web coding, i just don't understand enough about PHP form handling to figure out what i did wrong.
Image

User avatar
Sizik
Posts: 1099
Joined: Wed Aug 27, 2008 3:48 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Sizik » Fri Mar 07, 2014 4:35 pm UTC

I don't know much php, but I can tell you that "fieldtwo" is a string, and 12345 is a number, so they're likely not equal.
gmalivuk wrote:
King Author wrote:If space (rather, distance) is an illusion, it'd be possible for one meta-me to experience both body's sensory inputs.
Yes. And if wishes were horses, wishing wells would fill up very quickly with drowned horses.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1356
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Xenomortis » Fri Mar 07, 2014 5:00 pm UTC

And "if ( x = b )" probably doesn't do what you think it does.
Spoiler:
Comparison in any "C-like" language is done using ==.
Many C derivatives will not allow something like "if ( a = b ) {...", where a is assigned the value of b inside the test, but C itself will.


Sizik wrote:I don't know much php, but I can tell you that "fieldtwo" is a string, and 12345 is a number, so they're likely not equal.

This is PHP.
My understanding is that the output of a C++ compiler is more predictable than the actions of the PHP interpreter.
Image

User avatar
PeteP
What the peck?
Posts: 1451
Joined: Tue Aug 23, 2011 4:51 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby PeteP » Sat Mar 08, 2014 12:11 am UTC

^PHP allows assignments in an if too. And php does automatic type version so if I remember right "12345" == 12345 is true (if you don't want that add another =)

Well something more like if ($_POST['fieldtwo'] == 12345) should work better. ($_POST is the PHP array which contains variables passed via the post method which your form does.)

User avatar
ahammel
My Little Cabbage
Posts: 2115
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby ahammel » Sun Mar 09, 2014 6:01 pm UTC

Xenomortis wrote:This is PHP.
My understanding is that the output of a C++ compiler is more predictable than the actions of the PHP interpreter.

No C++ compiler will complain of a "paamayim nekudotayim" error, for one thing.
He/Him/His/Alex
God damn these electric sex pants!

User avatar
Yakk
Poster with most posts but no title.
Posts: 11018
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Sun Mar 09, 2014 6:22 pm UTC

Code: Select all

static_assert( false, "paamayim nekudotayim" );
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2059
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster, UK

Re: The "IT DOESN'T WORK!" thread

Postby e^iπ+1=0 » Sun Mar 09, 2014 7:15 pm UTC

They have offices in Israel. It's not as absurd as it first looks.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
ahammel
My Little Cabbage
Posts: 2115
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby ahammel » Sun Mar 09, 2014 7:42 pm UTC

e^iπ+1=0 wrote:They have offices in Israel.

And they therefore decided that exactly one error message should be in Hebrew?
He/Him/His/Alex
God damn these electric sex pants!

User avatar
PeteP
What the peck?
Posts: 1451
Joined: Tue Aug 23, 2011 4:51 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby PeteP » Sun Mar 09, 2014 7:56 pm UTC

ahammel wrote:
e^iπ+1=0 wrote:They have offices in Israel.

And they therefore decided that exactly one error message should be in Hebrew?

Well I would say therefore they named exactly one thing in Hebrew leading to a related error message in Hebrew. Though the difference is small.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1356
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Xenomortis » Sun Mar 09, 2014 8:15 pm UTC

Be grateful; if PHP had unicode support the error message would be even worse for non-Hebrew speakers.
Image

jareds
Posts: 435
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby jareds » Sun Mar 09, 2014 10:17 pm UTC

As I recall, the problem is that PHP gives error messages like "unexpected T_PAAMAYIM_NEKUDOTAYIM". The parsing error messages shouldn't be using the internal name of the token, so it's not fundamentally a Hebrew versus English issue. I don't think I've ever seen a production C++ compiler give an error like "unexpected tok_rbrace".

But, admittedly, I'm one of those people whose primary source of knowledge of PHP is sites making fun of it.

User avatar
Tomlidich the second
Posts: 1230
Joined: Thu Mar 22, 2012 5:38 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Tomlidich the second » Mon Mar 10, 2014 5:07 pm UTC

PeteP wrote:^PHP allows assignments in an if too. And php does automatic type version so if I remember right "12345" == 12345 is true (if you don't want that add another =)

Well something more like if ($_POST['fieldtwo'] == 12345) should work better. ($_POST is the PHP array which contains variables passed via the post method which your form does.)



this is helpful actually, i will give it a go and let ya know.

edit: it works! it f*cking works! oh my god i am so happy.

also, i forgot a semi colon on my "echo" command within "else" so that was causing an error.

thanks guys, i really really appreciate it.
Image

User avatar
Diadem
Posts: 5585
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Diadem » Wed Mar 19, 2014 10:16 am UTC

Ok so I'm working with make under linux now, but I'm pretty new to makefiles. I can't get the dependencies to work properly.

The code is C++. If I change a header file, I want make to only recompile those files that depend on this header. Nothing more and nothing less. Also, it should be done automatically.

I know gcc has an option to output dependencies in makefile format. So I can use that:

Code: Select all

sources = file1.cpp file2.cpp file3.cpp
objects = $(sources:%.cpp=../../Intermediate/module1/%.o)
dependencies = $(sources:%.cpp=../../Intermediate/module1/%.d)

-include dependencies
../../Intermediate/module1/%.o: %.cpp
   $(CPP_COMPILER) $(Preprocessor_Definitions) $(Compiler_Flags) -c $< $(Include_Path) -o $@
   $(CPP_COMPILER) $(Preprocessor_Definitions) $(Compiler_Flags) -MM $< $(Include_Path) > $(@:.o=.d)


This doesn't work, and I think I know why, I just don't know how to fix it. The problem is that the gcc dependency file will look something like:

Code: Select all

file1.o: file1.h file2.h ../module3/file5.h

It is missing the relative path to the object file. So make doesn't recognize it as belonging to the same rule.

Turns out, instead of -MM you can use -MT <string> to change the rule targeted by the dependency file. So I could use that. However - MT also includes all system headers, instead of just my own. What I really need is an -MMT option, but it doesn't seem to exist.

Anybody know how to solve this? Or a more elegant solution to the entire issue. I did a lot of googling on this, but could find surprisingly little, considering this must be a problem that occurs often. "I only want to recompile files that are changed" seems like a pretty standard demand.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

EvanED
Posts: 4305
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Wed Mar 19, 2014 1:41 pm UTC

My suggestion is a probably-unhelpful "don't use Make". Do external constraints force it?

Getting proper include rebuilding to work is one of a handful of basic tasks that pure Make pretty much sucks at, and put them together and I'd never use it for a C/C++ project when I had a choice.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 12 guests