Duoas 1,025 Postaholic Featured Poster

Ah, yes, it is the beginning of the year... so you should be at the beginning of your programming course and not at the end...

Sorry I went over your head.

The basic principles are:

  1. always read user input as a string, using something like getline( cin, s );
  2. test the string to make sure the input is valid
  3. convert the string to the proper type of thing (integer, float, enum, etc.)

This is not lighthearted stuff. But all robust code that interfaces with the user must have it in some form. In academia people get away without it because the focus is often on other things.

There are as many ways to check input as you can think of. You could, for example, just create a function that tells you if it is good or bad.

getline( cin, s );
if (!isanumber( s )) { cout << "fooey!\n"; return; }
n = converttonumber( s )

Somewhere else you'd have defined the handy little functions isanumber() and converttonumber() to help you. The abstraction and organization of your code is up to you.

T.N.Sharma Be careful:

  • Get rid of that cin>>scores; ! It's pure evil, and getting rid of it is the whole point of this thread.
  • The argument should be char scores[] .
  • Don't hardcode ASCII values. Use '0' and '9' .
  • Your function tries to do two things but succeeds with only one.
    1. if your function determines …
Duoas 1,025 Postaholic Featured Poster

He's using ISO-standard C++, so everything he does you can do. However, the code he provides isn't in the same order you would put it in your program, just in the order he tells you about it.

The basic strategy is that you should read all user input as a string.

#include <iostream>

int main() {
  std::string user_input;

  std::cout << "How many years old are you? ";
  std::getline( std::cin, user_input );

  return EXIT_SUCCESS;
  }

At this point, the user could have entered anything --including nothing by just pressing the ENTER key. However, since we can expect users to do dumb stuff like that, we have obtained input in a safe way: the program is still working properly.

Now, we need to verify that the user gave us a string that is correctly formatted. Since we are asking for a number, let's create some way to verify that the user did, in fact, enter a number using the digits 0..9. I'll use the same kind of technique referenced in the article.

#include <algorithm>
#include <iostream>
#include <cctype>

// This is just a fancy way of making a function that returns
// true if c is not a digit in '0'..'9'. Except, instead of a function,
// it is an object.
class nondigit {
  public:
    bool operator () ( char c ) { return !isdigit( c ); }
  };

int main() {
  std::string user_input;

  std::cout << "How many years old are you? ";
  std::getline( std::cin, user_input );

  // …
Duoas 1,025 Postaholic Featured Poster

No.
Years.
Check out this thread.

Duoas 1,025 Postaholic Featured Poster

If what you want is something:

  • with text and/or graphics
  • that you can click on
  • that represents the newly executed program
  • that appears on your menu bar (or "menustrip"/"taskbar")

then you need to add a new menu item to your menu bar/menustrip/taskbar when the new program is executed.

If I still don't understand what it is you want then you'll need to explain yourself much more clearly and using much more concrete language than you did in your last post. And avoid circular references.

Duoas 1,025 Postaholic Featured Poster

Ah, young grasshopper, you have advanced to the next level: you have recognized the need for better input validation.

It might be worth it to make yourself a little function that reads and returns a validated number.

Duoas 1,025 Postaholic Featured Poster

It is unlikely that anyone is going to write code for you...

However, what you want to do shouldn't be too difficult. Every time you start a new program you just need to add a new menu item to your menustrip at the bottom of the window. Part of the information you keep about each running application should be the index of the menu item (so that you can update it, and whenever anyone clicks it, you can search through your running applications array for the application associated with that menu item).

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

The app doesn't restart -- it doesn't terminate until you drop off the end of the main() function.

If you really want to clear the screen, this is how Microsoft does it.

Enjoy.

Duoas 1,025 Postaholic Featured Poster

Ah, yes, this is exactly the file I thought it was. This is for bit-graphics programming. You cannot use printf with it. (Well, you could, but it wouldn't be as nice or manipulatable as you'd like.) However, you can make your own little window for printing text.

typedef char FillPatternType[ 8 ];
typedef struct fillsettingstype FillSettingsType;

typedef struct {
  int left, top, width, height;
  } rect_t;

typedef struct {
  rect_t rect;
  int textcolor, bgcolor;
  } textwindow_t;

/* ----------------------------------------------------------------------
  Create a textwindow area and put a nice border around it.
  Preserves the fill pattern/style and fg and bg colors.
 */
textwindow_t create_textwindow(
  int left, int top, int width, int height,
  int bordercolor, int bgcolor, int textcolor
  ) {
  textwindow_t result;
  int right, bottom;
  int savecolor;
  FillPatternType savefillpattern, testfillpattern;
  FillSettingsType savefillsettings;
  int is_fillpattern_used;

  result.rect.left   = left +5;
  result.rect.top    = top  +5;
  result.rect.width  = width  -10;
  result.rect.height = height -10;
  result.textcolor   = textcolor;
  result.bgcolor     = bgcolor;

  right  = left +width  -1;
  bottom = top  +height -1;

  /* Preserve the fill pattern/settings */
  getfillsettings( &savefillsettings );
  getfillpattern( savefillpattern );
  memset( testfillpattern, 0xFF, sizeof( FillPatternType ) );
  is_fillpattern_used = memcmp(
    savefillpattern, testfillpattern, sizeof( FillPatternType )
    ) != 0;

  /* Clear the background */
  setfillstyle( SOLID_FILL, BLACK );
  bar( left, top, right, bottom );

  /* Draw the border */
  savecolor = getcolor();
  setcolor( bordercolor );

  arc( result.rect.left,  result.rect.top,     90, 180, 5 );
  arc( result.rect.left,  result.rect.bottom, 180, 270, 5 );
  arc( result.rect.right, result.rect.bottom, 270, 360, 5 );
  arc( result.rect.right, result.rect.top,      0,  90, 5 );

  line( left, …
Duoas 1,025 Postaholic Featured Poster

Nice! Boost rocks!

Sorry I wasn't paying attention when I responded earlier...

Duoas 1,025 Postaholic Featured Poster

I'm getting a 404 for that link. By default, all files under dvirl/ are private, so you may have to change access rights to it.

Directories get 755, files get 644.

A better solution, though, is to give me a link to where you got the graphics.h file, or attach it here. (Because once you open your directories to public access, anyone can read its contents.)

Often a school will give you the ability to keep a home page in a specific subdirectory, like /dvirl/html/ or something, which you can safely chmod 755 (I presume it is a school --I can't read Hebrew [well, not yet, anyway... :) Someday I'll learn...]).

Duoas 1,025 Postaholic Featured Poster

Karkaroff, we're trying to help you. There is no need to get snippy.

I've spent some time debugging this for you. The problem is that the compiler is tripping over the type of the offset value when you seek to the last record on line 25. Change it to stfile.seekg( -(std::streamoff(sizeof(item))), ios::end ); That should get you rockin'.

Also, when I wrote stfile.open( "fooey.dat", ios::in | ios::out | [B]ios::binary[/B] ); i meant it. Play with fire and you'll get burnt. Keep your hand in the fire and you'll get toasted.

[EDIT] Nice links, iamthwee. Karkaroff hasn't violated any serialization rules though...

Oh yeah, while I'm still thinking about it (for the nth time), replace that gets(name) on line 19 with cin.get( name, sizeof( name ) ); Hope this helps.

Duoas 1,025 Postaholic Featured Poster

1) I'm a little confused now. The Turbo C graphics.h library is not a textual interface library. Are you using some other, same-named library that is? (If so, please give me a link to it so I can look at how it works and what you can do with it.)

2) Ah, I understand your confusion on "screen". For all intents and purposes, you can say that you have only one "window" with which to work. If you are using the old graphics.h library that I think you are, it is a very easy technical issue to make a sub-region/window. Let me know about the library and I can point you in the right direction.

3) (A diagnostic is information that describes something, typically for debugging and validation purposes.)

Let me know more about graphics.h and I'll be able to give you some useful info.

Duoas 1,025 Postaholic Featured Poster

I think the original problem (causing the compiler error) is this: EventStream<complex<Lambda[B]>>[/B] evstrTrain( ... C++ compilers can't distinguish this from the right-shift operator. You need to have a space between them: EventStream<complex<Lambda[B]> >[/B] evstrTrain( ... Hope this helps.

iamthwee commented: good catch +13
Duoas 1,025 Postaholic Featured Poster

There is no button in graphics.py. How exactly are you trying to create one?

Duoas 1,025 Postaholic Featured Poster

It will also fail if the file "stock.txt" doesn't exist before you start, since there is nothing to read and you haven't reset the error condition.

Duoas 1,025 Postaholic Featured Poster

It is quite likely that I don't understand what you are trying to do. You didn't give me very much information to work on.

What I know is:

  1. you want to use printf(), which is a console I/O function
  2. you want a "small screen" in the "big screen", which can be read two different ways:
    • a subwindow or subregion of your program's main window
    • a separate window on the desktop

    (since you are using console I/O I assumed the first)

  3. and now I know that you are writing a game

So, for further help, I need further information.

  1. What kind of game are you writing? Textual or graphical? If graphical, are you using SDL or OpenGL, or the Win32 API or some other 2D library?
  2. What exactly do you mean by "big screen" and "small screen"?
  3. What kind of information do you want to print to the small screen? Is it diagnostic? Or information the game player needs to see?
Duoas 1,025 Postaholic Featured Poster

See here for links.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You have to do some planning on how exactly your messages are to be formatted (more information than just text must be sent).

Use an INET, STREAM socket. Avoid datagrams.

You will have to have a server running on some system everyone can access, which accepts connections and duplicates information received from one connection to all the appropriate target connections. You may or may not choose to use threading to implement each connection.

For the 3D stuff, you will want some sort of library. Here's some C++ 3D stuff I googled. The OpenGL things might be most friendly to you.

You've got some work ahead of you. Implementing a 3D chat is not trivial. Relatively simple, yes, but not fare for the queasy beginner.

Hope this helps.

[EDIT] Hey there, iamthwee. Are you also thinking, "gee, this could be done in Blender"? :)

Duoas 1,025 Postaholic Featured Poster

You are not properly initializing your fstream. Try: stfile.open( "fooey.dat", ios::in | ios::out | ios::binary ); I'm still a bit dubious about what you are doing in your code. (There's nothing wrong with writing an object to file like you are; it's just that your code looks really scattered.)

Hope this helps.

[EDIT] BTW. How did this thread get a four-star rating from only ~40 views and no responses?

Duoas 1,025 Postaholic Featured Poster

You haven't really provided enough specific information.

What do you mean by "system" and "ping"? You've collected some sort of text packet over some socket into a file?

How is the string to be found? Can you search for an exact match? Or would something more flexible be required?

In either case, just read the file one line at a time and search each line for your match. If an exact match will do, use one of the std::string find functions. If you need something more complicated, check out Boost's regex library.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Yes. And so do you.

Remember, get out the crayons and construction paper and draw what you are trying to do at each step.

Duoas 1,025 Postaholic Featured Poster

Check out pdcurses.

The other ways to do it would be to use the BIOS console control or the Windows Console API directly.

I don't think TurboVision is available for C...

Duoas 1,025 Postaholic Featured Poster

Every dimension you add to an array increases its space requirements exponentially. In anything above a two-dimensional array the need for all that space becomes rarer. In such cases, programmers use what are called "sparse arrays", which really aren't arrays at all, but are treated as if they were. In C++ and other object-oriented languages, this is the perfect use of a class type abstraction.

An array of strings is really a trick. So far (except for "sparse arrays" --which are also a trick) you have thought of an array as a sort of table or grid or matrix. This is called a static array, as it has a fixed domain and range. An unconstrained array is one where one or more dimensions of the array do not share the same range of indices --like the array of strings: one string may have a different length than another. A specific type of unconstrained array is a dynamic array, where the domain of one or more dimensions of the array may be changed at will.

Static arrays are simple, because all the computer needs to know is known at compile-time: how many dimensions and what is the size of each dimension respectively? The array can then be stored as a simple, contiguous list of elements which the computer indexes according to the information it has about that array.

Unconstrained arrays cannot be that simple, so to implement them usually involves pointers. Hence, an array of strings …

Duoas 1,025 Postaholic Featured Poster

Link.

Even if you are familiar with a language a good reference is invaluable. C++ is so popular that if you google anything alongside "c++" you can usually get good hits also.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Ah, yes, my mistake. You are using graphics.py. The type of thing matters. A point is an object, not a tuple.

So, you need to create a point using the appropriate constructor: p8 = [B]Point([/B] p3.getX() + (distance / 5.0), p3.getY() [B])[/B] etc.

Sorry I missed that.

Duoas 1,025 Postaholic Featured Poster

You must have been hit by some neutrinos or something. ;)

Glad you got it sorted out. :)

Duoas 1,025 Postaholic Featured Poster

What you want isn't technically "security related issues" but "copy protection" issues. The link lists various copy restriction schemes.

You can always compile your python code to a native binary to make it less crackable.

Python is an interpreted language, which byte-compiles itself; both factors make it more crackable than non-interpreted languages like C, C++ and Pascal. So compiling to a native executable helps here a little. But not a whole lot. In general though, once compiled you have guarded your code against most people.

Passwords are used to prevent unauthorized users from running the program.

You can include simple MD5 or CRC checks and the like to verify that code has not been modified at startup or other intervals --but employing such a system is tricky. You'll have to do some research.

There is no way to prevent someone from copying your program to another PC. To date, the best way to prevent someone from executing your program on an unauthorized PC is by using a dongle, which is not cheap to manufacture or buy.

Unless your program is so slick that it threatens Microsoft's/IBM's/Sun's/etc. future revenue, I doubt that you really have too much to worry about someone trying to steal your secrets. Using a program is usually enough for experienced programmers to have a pretty good idea of what is going on behind the scenes.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

You're kidding, right? You demand us to help you in less than two hours? Go get a job and see how long people put up with you.

Since you fixed it yourself I won't bother giving you my answer.

Salem commented: Absolutely correct! +15
Duoas 1,025 Postaholic Featured Poster

It would seem that quickreport somehow corrupted your delphi installation. You can try uninstalling quickreport but if it modified something important you may have to completely uninstall and reinstall delphi. Sorry.

Duoas 1,025 Postaholic Featured Poster

You've not given enough information for us to help you.

Try setting some breakpoints and/or stepping through your program. When you have identified the section of code causing the problem, post it and we can go from there.

Duoas 1,025 Postaholic Featured Poster

You're doing well; you've just made a simple mistake.

p8 is the upper-left corner of the door
p7 should be the lower-right corner of the door

the mistake is when you say p7=(p8, p8 is a point, not a number... :$

You'll need to add something to p8's x coordinate.

Duoas 1,025 Postaholic Featured Poster

Until now I'd never heard of Reflection X, so my help may be inadequate.

According to the online documentation [ 1 ], you should have a connection option checkbox labelled "Tunnel X11 connections". Make sure it is checked.

If you don't have that option (versions 12.0-13.0.3) you'll have to enable port forwarding using a few other steps (see the link for details).


Technical doc 1814 [ 2 ] takes you through the steps of configuring your SSH program to use X11 port forwarding. (If you haven't done this already --by default SSH programs don't do this.)

I hope this helps.

Duoas 1,025 Postaholic Featured Poster

My brain won't read stuff outside of code tags right now...

Just set the entire block to zero. You can do it with a nested loop, or just one loop, or just using a library function.

#include <cstring>
...
  char array[7][10];  // seven strings of 10 chars each

  // using nested loop
  for (int y=0; y <  7; y++)
  for (int x=0; x < 10; x++)
    array[y][x] = '\0';

  // using one loop
  for (int i=0; i < 7 * 10; i++) *(array + i) = '\0';

  // using library function
  memset( array, '\0', sizeof( array ) );

The nested loop is least efficient; the library function is most efficient.

BTW, never name a variable "array"...

Duoas 1,025 Postaholic Featured Poster

Link.

Pay special attention to the section on "Reading and Writing Complex Data". You are interested in integers, but the same principles apply. Here's their example using int instead of Data.

#include <fstream.h>
    ...
    int x;
    int *y = new int[10];

    fstream myFile ("data.bin", ios::in | ios::out | ios::binary);
    myFile.seekp (location1);
    myFile.write ((char*)&x, sizeof (int));
    ...
    myFile.seekg (0);
    myFile.read ((char*)y, sizeof (int) * 10);

Good luck.

Sawamura commented: thx for the link and code +1
Duoas 1,025 Postaholic Featured Poster

When in doubt, read the documentation.

I just did, and realized I made an error. It should be: cin.ignore( numeric_limits<streamsize>::max(), [B]'\n'[/B] ); Hope this helps.

Duoas 1,025 Postaholic Featured Poster

I presume you are trying to do X11 tunneling?

Is your laptop running *nix or Windows? If Windows, what software are you using to connect?

Duoas 1,025 Postaholic Featured Poster

They shouldn't, but sometimes they do. This is because a great deal of optimization is based on heuristic rules, which you can break in quite a number of different ways.

According to Microsoft, you should always get your program working and thoroughly tested before enabling optimizations, and then test it yet again. If you do find discrepancies, you can compile using an "optimized debug" build for tracking down the differences, then use pragmas to selectively disable optimizations where it makes a difference.

I would like to think that the code you are using was designed to be compiled as optimized code, but it is more likely that whoever programmed it originally just got lucky.

Without knowing more about your code, or what you are doing, it is impossible for me to point you in the right direction.

Duoas 1,025 Postaholic Featured Poster

I could go to Ireland and speak to someone in Spanish, but I would get deported. (Just because someone who speaks Spanish can go and speak it in Ireland does not mean that he will be accepted by the Irish people.)

We are really at our limits for explaining things more clearly for you without just giving the answer away (I would argue that we already did...)

Think about it a little...

[EDIT] By the way, I'm not being rude. Everything in this post applies directly to your problem.

Duoas 1,025 Postaholic Featured Poster

Yes, it can be done, but I've never done it...

The toplevel window needs to have the -container flag and -use for the window ID of the SDL window.

However, PyGame wasn't really designed to coexist with other toolkits. See Why Gtk/Qt/WxWidgets... are bad.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Yeah, and tell us what you decided to do... :-/ :)

Duoas 1,025 Postaholic Featured Poster

1. The prototype should read like one of the following int TheData::ApplyFilter( TheFilter &Filter ) (operates on self) TheData TheData::ApplyFilter( TheFilter &Filter ) (returns new data)

Personally, I would have made ApplyFilter a method of the TheFilter class... but so long as you can justify your use you are fine.

2. The point of overloading operators is to make hard things easy. So, you were using it correctly when you got the error message. The error message is because there is no member field named "values". There is, however, one named "Values". (Don't slap yourself too hard.)

The procedures themselves should verify that the index is valid:

double& operator[] (unsigned int Index) {
  if ((Index < 0) || (Index >= Length))
    throw 0; // throw some appropriate exception here, instead of int.
  return Values[Index];
  }

My sample code just throws an int, but you should include <exception> and throw something like std::out_of_range.

3. That is because you are having problems with the default copy constructor, which only makes a shallow copy. Thus, when a temporary copy of your object is deleted, it deletes the original object's data. Then, when you delete the original object, the heap complains.

Make sure to have both a copy constructor and an overloaded assignment operator that makes a deep copy of the data. (This will be useful in your assignment.)

Use Google to learn about shallow and deep copies.

4. Don't hazard. Just do it. ;)

Duoas 1,025 Postaholic Featured Poster

You are doing it the hard way. You really should download something like Shalom and use it...

The problem is that your help file doesn't have a table of contents. This is usually stored in a separate file, so you'd have frobinator.cnt frobinator.hlp How are you calling the help file? It doesn't need to have a contents, but WinHelp will complain if it is instructed to display the contents page for a help that doesn't have one.

(My own help files are often pretty small and succinct, so I tend to omit contents pages. However, all substantial help files should have a table of contents...)

In your code, you should have something like

procedure TForm1.FormCreate( Sender: tObject );
  begin
  // The help file is named the same as the program,
  // and is located in the same directory.
  application.helpFile := changeFileExt( paramStr( 0 ), '.hlp' )
  end;

procedure TForm1.AboutClick( Sender: tObject );
  begin
  // Context 42 is the help file's about page...
  application.helpContext( 42 )
  end;

If you use Shalom Help Maker, it can generate a little include file for you that gives constants for all the help contexts. So it could read:

...
implementation
{$R *.dfm}
{$include MY_HELP_Constants.inc}

...

procedure TForm1.AboutClick( Sender: tObject );
  begin
  application.helpContext( MY_HELP_ABOUT )
  end;

procedure TForm1.HelpClick( Sender: tObject );
  begin
  application.helpContext( MY_HELP_CONTENTS )
  end;

...

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

The C++ >> I/O operator corresponds to the C scanf() function. It is not really designed to input strings or binary data, even though you see it all the time.

A good reference (especially in C++) is invaluable.
I like cppreference.com, because it is simple. For more detail, check out cplusplus.com.

The .read() is a method of the istream class.

There is also a sticky thread at the top of the C++ forum for books you can get, and you can google for c++ tutorials. These will help you out a lot.

Good luck.

Duoas 1,025 Postaholic Featured Poster

The >> and << file operators only read until it encounters the next non-whitespace object in the file. In your case, it is the word "RECORDS".

Your buffer is 1000 characters long, so you cannot read more than 1000 characters at a time. So, try this instead:

...

        char txt_records[1000];

        ofstream file_records_writin("records.txt");
	file_records_writin
          <<"\t\t\t\t\tRECORDS\n"
          <<"name-------------------------------time\n\n"
          <<endl;
	file_records_writin.close();

	ifstream file_records_readin("records.txt");
	file_records_readin.read( txt_records, 1000 );
	cout<<txt_records;

...

You would be better off, though, reading each record one at a time, using getline().

I hope this helps.

Duoas 1,025 Postaholic Featured Poster

buffer is type (char *), so
sizeof( buffer ) == sizeof( char * ) == 4

Make sense?

Also, again, you are assuming something about the size of the file. What if it is different than Size_of_the_file? What if the file is super-huge?

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

An array is a static structure, so the compiler must be able to know its size when compiling the code. The size of the file may change, depending on the file, so the compiler says "well, exactly how much space am I to reserve, then?".

You say that Size_of_the_file is a global, constant int declared earlier in the file? Your compiler doesn't think so. It is probably right.

You are also going to have problems if Size_of_the_file is less than 100, since you are reading and writing 100 bytes every time. (That is probably causing your program to crash!)

Better choices are to move the array to the heap (allocate it using new) or to use a container class (like std::string) to store it, and reading and writing only as many bytes as you have allocated.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

Ah, I get it.

The OnCreate function is separated out just for some modularity. You could stick everything in one spot, but that would be confusing and large. By putting initialization stuff in a routine by itself it is both obvious and re-usable: the OnCreate is used when creating a window, and more than one window can be created easily.

This follows the philosophy that each routine should stick to doing one thing.

Hope this helps.

Duoas 1,025 Postaholic Featured Poster

That's actually a tough question. Most of the window managers I am familiar with (which isn't many) will properly execute a text file beginning with the magic words: #! /usr/bin/python or #! /usr/bin/env python For a specific distribution, python should be able to install properly so that you don't have to worry about it. But, in the end, it is the choice of window manager that makes all the difference. In essence, it is the end-user's responsibility to make it double-click executable. Yeah, stupid, I know.

If you want to be most robust, you can try to do some tricks:
1. Rename it from foo.py to foo.sh (making sure the magic lines are still in the header).
2. Use something like py2exe to compile your script into an actual executable (ELF, in your case).
3. In your installation script, check to see if the user is using a known window manager and update the wm from the installation script (but ask first!).
4. Use a utility like makeself. (This is really just a variation on #1.)

Sorry I couldn't be of more help.

Duoas 1,025 Postaholic Featured Poster

OK, so there are no neutral states.

I'm afraid I can't give you much more help than I already have, since this homework is a test of definitions.

Hint: if the modified NFA cannot halt on B, then what difference does it make if B is part of the automaton?

Duoas 1,025 Postaholic Featured Poster

OnCreate is a standard Borland idiom for a procedure/method executed whenever a new form is created (or other windows object).

So what is your problem?