Salem 5,265 Posting Sage

> How should i make my program go asleep and wake up when OS tells that i have something to read?
You just described select()

You don't need to do anything to make your process go to sleep, or to get it to wake up later on.

while ( (status=select(....)) >= 0 ) {
  if ( status == 0 ) {
    // timeout
  } else {
    // one or more file descriptors are ready, do something
  }
}
Salem 5,265 Posting Sage

Where did you get 1000 from?
Or indeed the idea that select() might be expensive, from the link you posted.

In principle, the process should be asleep in a select call until the OS wakes it up with something to do.

It's gotta be better than a bunch of poll() calls, or heaven forbid a while(1) loop.

Salem 5,265 Posting Sage

> recv( sock,buff,255,0);
1. You ignore the return result.
2. Despite all your memsetting, you still have the potential for a buffer overflow. If the recv() happens to FILL your buffer, there is no \0 at the end of it to mark the end of the string.
3. TCP is a stream protocol, so the minimum guaranteed number of bytes in a successful recv() call is 1.

The server might send "PING\r\n", but you might only recv() "PIN" on the first call, then "G\r\n" on the second call. This message reassembly is YOUR job to do.

It's a good idea to separate the "get a message" from the "process a message".

> So is there a way to change 'while' into some kind of 'when'.
Yes, look up the select call.
You can use this to wait for all sorts of sockets to become ready.
You can even supply a timeout in case you get bored of waiting, and need to go and do something else for a while.

Salem 5,265 Posting Sage

The directory specified by the environment variable TEMP or TMP is a good bet.

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

It's a bit much to dump 650+ lines of badly formatted code without any clue as to what it's supposed to do, then demand we fix it for you in the next 24 hours!

When was the first test? 5 minutes before posting the code here.

Read this before your next attempt at writing any substantial code.
http://cboard.cprogramming.com/c-programming/88495-development-process.html

Compile often, test frequently, make regular backups of known working code IS a development strategy.

When it stops working all of a sudden, and you can't work out why, then you have several bits of useful information:
- a previous version which did work
- a good description of what you did next
- a good description of what your test was (and how it failed).
More importantly, you stop making the problem WORSE until you've fixed it and are back on track.


Write everything, compile once, run never is a "hail mary" pass into the end zone with 10 seconds to go at the end of the 4th quarter (or any other suitable sporting last minute heroics you care to use) is NOT a strategy which will work for you in the long run.


Here's a free "gimme"
Why are there so many open file calls?
ifstream block_file("block.txt");

Salem 5,265 Posting Sage

The check is fine.

The code is broke, if all the elements of the array are negative though.

Salem 5,265 Posting Sage

Is it this one?
http://www.barcodelib.com/net_barcode/main.html

Have you asked them about it, via their support channels?

Also,
http://demo.barcodelib.com/LinearWebStreamDemo.aspx
I notice from the demo that you can specify a rotation.

In the API, is the rotation an enumeration (set of discrete values), or a continuous number (0 to 360 degrees). If it's the latter, and you're even a little bit off, then yes, you're going to get jaggies.

Salem 5,265 Posting Sage

Why don't you try your own solution?
Rather than looking at someone else's code, and trying to do basically the same thing over again.

Snake:
- snake moves every 'tick' (ticks get quicker as the game progresses)
- snake hits wall - dead
- snake hits snake - dead
- player presses key - snake changes direction
- snake hits food - snake gets longer

That's pretty much all there is to it.

> do I need to know about: structures? array? etc.
Yes.
Or rather, your life will be a lot easier with careful use of these things.

Salem 5,265 Posting Sage

And what does that have to do with the original question?

You said it was coming out "zigzag", not that it was completely the wrong type of barcode.

Salem 5,265 Posting Sage

Are you sure it's not skew in your printer as the print head tracks left/right?

Create some known test images using say mspaint to check.

Say something like this.

If these come out fine, or other printing applications come out fine, then you may have a software problem.

If something simple like an image is broken, then you've got a hardware problem.

Check if your printer has a setup/calibration option (try right-click on it's status icon in the systray ).

sknake commented: nice barcode +7
Salem 5,265 Posting Sage

Which Operating system and compiler are you using?

Salem 5,265 Posting Sage

I would probably suggest that you have mis-matched braces, and that you don't have the scope you think you have.

A small and COMPLETE compilable example would be a lot better than the .... abbreviation you've posted.

Salem 5,265 Posting Sage

No, you read the rules first, then make an effort.

Salem 5,265 Posting Sage

They're just a helpless skiddie wannabe, trying to do something or other with someone else's malware kit.

I've seen it, it's rubbish.
http://www.daniweb.com/forums/thread194496.html
http://www.daniweb.com/forums/thread195594.html

Salem 5,265 Posting Sage

> Your boss doesn't mind you cheating him?
Apparently, there's a lot of it going around

You'd think in times like this that employees would be keeping their noses squeaky clean and up against that grindstone, making sure that their position was still useful.

But no, dangle a couple of sunny days and the chance of a self certificated week off, and they're out the door.

The fact that they might have some 'explaining to do' a few months later when they ACTUALLY get the flu never crosses their minuscule minds.

Salem 5,265 Posting Sage

How about beginning with learning about memory allocation, and use of pointers?

explode(), pm(), msg() and get() are all a disaster area.

Eg.
output[0] = tok;
where does this write tok?
You didn't allocate any space for it.

> msg ( strcat ( "PASS ", pass ) );
PASS is a string constant, so modifying it is not allowed.

I could go on, but the errors are systemic.

Salem 5,265 Posting Sage

> will the value of counter automatically change as well?
a) no
b) did you try it?

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

People tinker with the layout and format of web pages all the time.
Start by verifying the input data is as you expect (visit it with a browser).

Salem 5,265 Posting Sage

> nextArray();
You said it needed a parameter, but you didn't pass any!

Try
nextArray(life);

Salem 5,265 Posting Sage

All the digits in a row, lunchtime today :)

Salem 5,265 Posting Sage

I said nothing different than my FIRST post in this thread.
How much earlier would you have liked it? :icon_rolleyes:

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

Tabs are OK, if (AND ONLY IF) you use them for EVERY indent.

As soon as you mix them in the code, which is very easy to do, the result when posted on a forum is a mess.

I see this has turned into yet another meta-discussion about something other than the original problem :(

Salem 5,265 Posting Sage

First, your code with decent indentation. FYI, turn off TABS in your editor, and set it to use spaces all the time.

// CO1401 Reassessment 2008/9
//Liam Moncur BSc Computer Networking
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

double max(const double *data, int N);
double min(const double *data, int N);
double sum(const double *data, int N);
double mean(const double *data, int N);

int main ()

{
  char N, filein[10];
  ifstream myfilein;
  double sum=0;
  double mean=0;
  double max=0;
  double min=0;

  const int NMAX = 10;  // Maximum number of items to be processed

  do
  {
    cout << "Enter the name of an existing text file: ";
    cin.get (filein,10);

    myfilein.open (filein);

    if (!myfilein.is_open())
    {
      cout << "Unable to open file, sorry!" << endl;
      cout <<  "Please enter the file name that you which to be processed : ";
      cin.get (filein, 10);
    }
  }
  while (!myfilein.is_open());

  cout << "These are the contents of the file: " << endl;

  while (myfilein.good())   // loop while extraction from file is possible
  {
    myfilein.get(N);       // get character from file
    if (myfilein.good())
      cout << N;
  }

  myfilein.clear(); // set state to good
  myfilein.seekg(0); // go to the beginning of the file
  while (myfilein >> N)
  {
    sum += N;
  }

  myfilein.clear(); // set state to good
  myfilein.seekg(0); // go to the beginning of the file
  while (myfilein >> N)
  {
    mean = sum/5;
  }

  myfilein.clear(); // set state to good
  myfilein.seekg(0); // go to the beginning of the file
  while (myfilein >> N)
  {
    if …
Salem 5,265 Posting Sage

's all right, I've no interested in helping the clueless who resort to insults before showing some effort.

Salem 5,265 Posting Sage

Wow, that was easier than I thought - thanks for marking the thread solved.

Salem 5,265 Posting Sage

I thought you wanted help with graphics, not cryptography.

Oh wait, it's not encrypted, it's drivel-speak
http://www.catb.org/~esr/faqs/smart-questions.html#writewell

Salem 5,265 Posting Sage

Well, showing something of what you know about linked lists would be a start.
Then we have something to work with.

With no effort on your part, our choices are
- ignore you
- do your homework for you.
Neither are beneficial to you.

Salem 5,265 Posting Sage

> If f() is called first than, the whole purpose of precedency is lost.
Oh man, learn to read.

f() called first, result stored in temp1 - have we done + or * yet - no.
g() called next, result stored in temp2, have we done + or * yet - no.
h() called last, result is still in a register.
Multiply register with temp2 - woohoo!, we did * first.

Sure, you might be thinking
call g(), result in temp1, call h(), multiply by temp1, store in temp2
Then call f(), add temp2 and store result.

Different - yes
Same result if all the functions are free of side effects - yes
This order is mandated by the standard - nope.
Undefined outcome if any of the functions have overlapping side effects - you bet your ass it's undefined!.

Aia commented: you bet your ass it's undefined! :0 +17
yellowSnow commented: 'nuff said! Man, that was tough going! +2
Salem 5,265 Posting Sage

Why would you want another way?

Salem 5,265 Posting Sage

> according to you which one of the 3 functions will be called first.
No idea, it doesn't matter, I don't care.
That's for any individual compiler writer to decide. It's not in the language spec to define such things.

* happens before +, which in turn happens before =
That's all you have to go on, anything else which happens is up for grabs.

> You mean to say that f() will be called first in f()+g()*h().
Read - MAY BE CALLED FIRST.

If your code depends on it being called first (or last), then your code is broken - no ifs, buts or maybes.

Salem 5,265 Posting Sage

So
abcde
becomes after 1 step
ebcda

Move towards the middle, and repeat....

Salem 5,265 Posting Sage

Are you allowed to use recursion?

Salem 5,265 Posting Sage

Read the description again, they're arranged in a ring, each connected with it's neighbours.

> how it can be calculated that which valve will be open to equallize the level of tanks
I've no idea - that's the clever bit for you to work out (well not entirely true, I've an idea or two of where to start, but that's not the point is it).

Designing algorithms from analysing a problem is a key skill which has to be learnt. Nothing is served if we just blurt out the answer for you.

Create more examples, draw them out on paper, and work your way through them until you come up with a strategy for solving the problem.

When you can describe the algorithm on paper, you'll be a lot closer to solving the algorithm using program code.

Salem 5,265 Posting Sage

I would check those results with an actual multimeter to make sure the readings are accurate.

Salem 5,265 Posting Sage

What happens if you just swap the first and last elements of the array?

Salem 5,265 Posting Sage

> In you case f()+g()*h(), g()*h() will be done first coz * has higher precedence than +,
> but which one of the two g() and h() will be called is entirely dependent on the compiler.
Read Dave's link, there is nothing which says that f() won't be called first (or second for that matter).

t1 = f();
t2 = g();
t3 = h();
t4 = t2 * t3;
answer = t1 + t4;

Is just as plausible as any other evaluation.

Precedence defines the order in which intermediate results are combined, but says nothing about the order in which the intermediate results themselves are arrived at.

Throw in some CSE and bits of your overall expression could be all over the place.

Salem 5,265 Posting Sage

You only enter a string, not a number.

Typing in 123
will likely get you
49 50 51
as output.

If you were expecting 123 to be printed, you need more code.
At the moment, you just print the decimal value of ASCII character codes.

Salem 5,265 Posting Sage

> but I don't know if it's a good idea to let them know I'm looking for another job
Are you that set on finding another job?

I mean, if your current employer offered you more money, a better desk, more responsibility, different project (or whatever), would you take it?

If you're good at what you do, it's a lot better for them to try to keep you than to spend time and money looking for a replacement.

If the managers in question are clued in enough, then simply asking them to be a referee should be enough of a nudge to get them to pay attention to some of your concerns. If they just let you walk away, then you made the right choice. However, getting your current company to improve it's offer, or better yet two companies engaged in bid/counter bid for your services can only improve your lot both now (and in the future, whenever you decide to move).

Who knows, maybe they're looking to open an office where you want to go, and wondering how just to make a start - you could be golden at that point!

In these times, a lot of companies go for last in, first out. Being a newbie at a new place would not be good.

Salem 5,265 Posting Sage

> It seems USA army is going to try such veg robot in afganistan shortly
Have you seen Afghanistan? The place is a desert where it isn't a mountain.
Robot's gonna starve.

It should really plant trees as it goes for full sustainability ;)

Salem 5,265 Posting Sage

> The second line starts with the number of basins n (3 £ n £ 200), a space, and then n
> integers in the range 0 to 99, separated by single spaces,
> representing the water levels in the tanks.
You read the spec wrong

> In the first input 4 4 4 3 3, if 2 valves are open then the level will be 3.5 4 3.5 3.5 3.5.
You've got 5 tanks, not 4

First number is the number of tanks
4
The remaining n numbers (4 of them) are the tank heights
4 4 3 3

One 4 joins to one 3, and the other 4 joins to the other 3
Result: 2 valves, and all tanks level.

Salem 5,265 Posting Sage

Read the manual?

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

readfile.open("highscore1.txt");
if ( readfile.is_open() ) {
  // exists, do something like you're doing now
} else {
  // doesn't exist
  ofstream outfile.open("highscore1.txt");
  // yada yada
}
Salem 5,265 Posting Sage

> what if the file i am writing and reading to does not exist?
Test the result of the first attempt to open the file (you're assuming success at the moment).

If it doesn't exist, just write a new highscore table.

Salem 5,265 Posting Sage

So why are you telling me, and not your other kind helper?

Or better yet, why haven't you taken what has already been given and made an attempt to change it to your needs (and posted the result of that attempt)?

sknake commented: thanks buddy +6
Salem 5,265 Posting Sage

You shouldn't be calling strcpy() AT ALL in a C++ program using std::string to store strings.

The only time you need to do it is for the case where you're calling an historic interface that requires a "char*" pointer. Historic interfaces which accept "const char*" are not a problem.

Eg.

void hist1( const char *p );
void hist2( char *p );
...
string mystring = "hello";

// can be called directly using the c_str() result
hist1( mystring.c_str() );

// need to make a temp copy, just in case hist2() decides to
// modify the string.
char *temp = new char[mystring.length()+1];
strcpy( temp, mystring.c_str() );
hist2( temp );
// mystring = temp; // if you're interested in the result.
delete [] temp;
Salem 5,265 Posting Sage

You're reading AND writing the same file at the same time.

Read from the old one, write to the new one.
When you're done, delete the old and rename the new.

Salem 5,265 Posting Sage

So what's wrong with the answer on your other thread?
http://www.daniweb.com/forums/thread209172.html

Salem 5,265 Posting Sage

Why is someone with so many letters in their sig unable to find a display driver?

> is there anybody to help me out?
Have you tried google, or the manufacturer's website?