tetron 22 Junior Poster

you are very close to getting ti working the problem is how you are converting an int to char assuming that you are only going from 0 to 9 you can have

//this is either the relative or absolute file path
std::string base_file = "data";
std::string after_number = ".txt"
for(int fid(0); fid < 10; ++fid)
{
 char digit = '0'  + fid;
 std::string cur_filename = base_file + digit + after_number;
 std::ifstream fin(cur_filename.c_str());
 if(fin.is_open() == true)
 {
    //etc..
    fin.close();
 }
 else
 {
     std::cout << "could not open file:" << cur_filename << std::endl;
  }
 
}

the digit code will need expanding for larger number of files but it is just a file manipulation check what the string looks like before opening.

For example if I have this int type variable X that increment from 1 at the beginning to 10 when the program ends, how to open and write to 10 different files (data1.txt,data2.txt,data3.txt....) everytime X increments?

I've tried with the following but it didn't work:

string filename ("data");
int X;
ofstream out;
char number;
for(X=1;X<11;X++)
{
number=X;
filename+=number;
out.open(filename.c_str());
....
...writing....

out.close();
}
tetron 22 Junior Poster

Your first check should n't happen as you always want to prompt for
password so
set should look more like this

and maybe should have a different function name
as you care actually checking password rather than setting s1

void Password::set()
{

cout<<"Enter password"<<endl;
getline(cin,pass);


if (pass != s1)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
if (pass !=  s1)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
if (pass  != s1)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
 }
 if (pass == s1)
 {
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
 }
}
tetron 22 Junior Poster

Ok, I am a little confused I assumed that you were having a problem with the read.

Now several of your methods, are using approaches different to what I would choose to do.

When you say an error do you mean that the code crashes or just doesn't return the value you were expecting.

You might have a problem with !fbin.eof() this can lead to issues but I can't remember precisely why.

It is possible that your for loop is going beyond the end of your file.
You would probably be best off stripping out the open of binary files and stick to ascii as it is easier to maintain, until the code is working.

To find the records - get the size of the file and divide by RecSize would be one option


I am assuming that you are not calling option change_path
does your file_name not have an extension ie ".txt"

for test purposes add the following:

int get_used_records() {
  char temp[20];
  int counter = 0;
  for (int i = 0; !fbin.eof(); i++) 
  {
      std::cout << "i == " << i << " " << counter << std::endl;
      fbin.seekg(recSize * i);
      fbin.read(temp, sizeof(name));
      if (strlen(temp) != 0)
      {
               std::cout << "temp = " << temp << std::endl;
	counter++;	
      }
return counter;
}

and set task to 4

Line 37: sizeof(task_list) / 4 doesn't look pretty

The other question is what exactly is your input …

tetron 22 Junior Poster

I tried to add ios::beg to the function, but it gave no result. I actually though that the seekg and seekp functions set the absolute position in the file stream, but maybe I'm wrong.

They should do but there was a small chance of an overflow error.

I'm not sure what is happening - I assume that you are checking that n is meaningful.

it might be worth checking the return for all the methods
ie.
http://http://www.cplusplus.com/reference/iostream/istream/read/

and that the file is_open();

tetron 22 Junior Poster

you have not got {} matched in all places.

The compiler won't give you a line number for this so if your code isn't too long and you still can't find the error post it.

I will show the code if you want to see the error.... Am still working through this program but with this error i am not getting anywhere..

stricklypni commented: THANK YOU I KNOW C++ DONT TAKE THE SMALLEST SYNTAX ERRORS +0
tetron 22 Junior Poster

It might be an idea to use ifstream and ostream and open and close the file as needed.

I have looked at your code and haven't spotted the mistake. seekg(pos) should be setting an offset. However, I'm not sure whether the reason that you are having to move to the begining of the file first might be because the file is still open for writing.

What I suggested before is make line 92:

fbin.seekg(recSize * n, ios::beg);

without line 91
this says to use an offset from the explicit position start

the seekg() returns a value that might be showing an error.

tetron 22 Junior Poster

a couple of things to note == can be used directly

so

if(s1 == pass)
{
}

more importantly
you are using

else if(pass != s1)
{
}

which only only happens (i.e. is checked), if none of the earlier conditions fire

so you meant:

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
	cout << "exit" << endl;
}

if (pass.compare(s1) == 0)
{
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
}

you might want to think about adding a private method

std::string get_password();

and putting this in a while loop to count the max number of tries
with break

tetron 22 Junior Poster

Without seeing a full example, it seems odd behaviour.

As a first thought, I assume that you are talking about opening a file that is acting as a database and you want to move to the first entry.

you might try explicitly adding the ios::beg

fbin.seekg(0, std::ios::beg);

if this doesn't achieve anything we will need more information as to file format and where you opened the file.

P.S. it is helpful to use code tags

Why do I have do use the Seekg()-function two times in a row to make it work?

Code:

fbin.seekg(0); //jump to record at start of file
	fbin.seekg(0); //jump to record a second time (else it won't work)
	fbin.read(name, sizeof(name)); //read from data field 1
	fbin.read(reinterpret_cast<char*>(&age), sizeof(int)); //read from data field 2

	cout << "Name: " << name << endl;
	cout << "Age: " << age;
tetron 22 Junior Poster

Ok I will give this a try. One question currently though. what is v_pos ?

What I was giving as an alternative design had v_pos as a std::vector<int> the idea is that the last item is the last move tried.
so pos is just an index, on reflection this might have been better wrapped in side a coord class in the first place.

Now it is not necessary to change your entire design over at this point as it should work for small boards.

If you use the functions to remove the magic numbers then you can alter the , width & height of your board. This might indicate if my worries about your code taking too long to run will be addressed.

I would first check that al the solutions are being found with 4x4 and 8 knights and 5 x 5 with 13 knights

and then try increasing the size gradually.

I have not tested this to see if there is an issue but my concern is that, in effect, you are trying to think 31 moves ahead and even with 2 or three options a turn this is getting close to being too big to solve iteratively.

I know in a game a PC can take 45 minutes for 10 move depth with a heuristic algorithm for some games.

If I was doing this without iteration I would make the algorithm learn the shapes of placing the knights, In …

tetron 22 Junior Poster

minor issue in you check position in that are you checking the current position is empty as well as not attacked?

There are some maintenance issues creeping-in and these are being exagerated by changing design half way throuh

you might want some methods or classes that hide some of the magic numbers

int get_ind(int row, int col)
{
 int ret(-1);
 if(row >= 0 && row < width)
 {
   int ret = row + col *width;
   if(ret >= size)
   {  
       ret = -1;
   }
 }
 return ret;
}
bool is_knight(bool * board, int row, int col)
{
 bool ret(false);
 int ind = get_ind(row, col);
 if(ind != -1)
 { 
    ret = board[ind];
  }
 return ret;
}
bool am_I_attacked(int row, int col)
{
  bool ret(false);
  int ind = get_ind(row, col);
  if(ind != -1)
  {
     if(is_knight(row + 1, col + 2))
     {
        ret = true;
     }
//etc
   }
}

this makes it easier to see rather than the -17 and col >2

although I think from number of steps you might need to completely change board int board[width * height];

void add_knight(int * board, int row, int col)
{
//just set inds greater than current pos
 set_board(board, row + 1, col + 2);
 set_board(board, row - 1, col + 2);
set_board(board, row - 2, col + 1);
set_board(board, row + 2, col + 1);
++knightTracker;
 }

void set_board(int * board, int row, int col, bool b /*=true*/)
{ 
 int ind = get_ind(row, col);
 if(ind != -1) …
tetron 22 Junior Poster

KB.cpp(18) : error C2447: missing function header (old-style formal list?)

public:
	Password(void);

I would not expect to see void void being used as a parameter () is standard.

but what I don't see is a definition of

Password::Password() 
{
}

and the compiler might be complaining that it thinks that you have a function without a return type and not seeing a constructor.
though you might well just not have shown us :)

as well as line 18 as banfa says

tetron 22 Junior Poster

My comment in my last post was concerned with the following condition.

if (kCheck is false this second condition:

)||(KNIGHTS-knightTracker > spacesLeft)

is unaltered as the number of knights remaining is the same so
unless spaces remaining are reduced this will not fire.

As for finding no solutions...
This is a more complex issue and as I expressed badly before the order of complexity of the problem is such that a brute force recursive function might fail to find one of the two solutions.

If I was doing this recursively, my logic would be every time I add a knight to the board I would add all the squares that are attacked by it. Then every attacked square I would add an attack too reducing the number of spaces left at the same time.

This uses an int for each square rather than a bool so that the move can be unwound easily when removing the last knight placed.

so I would consider the logic of kCheck as it should adjust the spaces left.

The other line that I find confusing without running it is as follows:

findSolutions(boardN, col+1, i,SIZE-(col+rows*WIDTH));

Now this might be corret to give a solution.
What i read this as doing is:
1 - you are stepping horizontally along the row
2 - stepping along each column
3 - reducing the number of spaces left to reflect squares untested by latest run

tetron 22 Junior Poster

You are going to have to avoid some pitfalls with te 32 Knights problem

first is that as a brute force search it will often fail to find a solution

There are only two solutions: as you pointed out.

If you step in a vertical line it will not fail until the 24th piece is down.

This makes it much higher order complexity than the queens problem and you need to check that your code is going to complete rather than run for years.

having said that:

Ok so after a few days, I finally got the 8 queens problem solved. After having so much trouble I decided to practice recursion by doing the same problem but with 32 knights. 8x8 board, place 32 knights without them attacking one another. I am using a 1d array. When calling the program I get a heap corruption error. I am not sure why but recursion is really escaping me. Please help. I can provide other pieces of the code if requested. Thanks

void Board::findSolutions(bool *board, int col, int rows, int spacesLeft)
{
	if(kCheck(board, col, rows)){//Checks the 8 positions around the knight that he can attack
		board[col+rows*WIDTH] = true;// If he can be placed set the position to true
		knightTracker++;

should there be --spaces_left what is happening on the else where the knight is being attacked?

tetron 22 Junior Poster

Ok so after a few days, I finally got the 8 queens problem solved. After having so much trouble I decided to practice recursion by doing the same problem but with 32 knights. 8x8 board, place 32 knights without them attacking one another.

A bit off topic so apologies :)

EDIT this is an extremely wrong comment - you would not believe how good I am at games and missed this, sorry :( Obvious diagonals only solutions.

/*
but a knight in the corner attacks 2 squares.
A knight on the edge 3 - 4 squares 
so it is not possible to place 32 knights without attacking a knight

so i would not recommend a recursive function for this.

You could try a recursive traverse shape function where you paint an area with in lines. If you have a board this is a small step up
*/
tetron 22 Junior Poster

I will try to be succinct:

1. a static member variable acts like a single common variable
that is a global
so there is only really one 'n' in the whole project

2. a constructor () is called every time the computer has to allocate memory and give an address to a variable

so

//n = 0
CDummy a; //same as CDummy a() first  
//n +=1 
CDummy b[5];// creates 5 cdummies
/*
 so CDummy is called once per slot = 5
n += 5;
*/
CDummy * c = new CDummy(); //an obvious constrcutor
//n += 1

finally the dsstructor is called when the parameter is finished wuth
on delete of a pointer
or end of scope

{//start of scope
//created
CDummy d;
 if(x == 7)
 {
 }
}//end of scope //d is destroyed

so

delete c;
//destructor called once as not delete[]
n -=1
tetron 22 Junior Poster

cout
doesnt recognise arrays so you are outputting a pointer what you wanted to do is

#include <iostream>
int main() {
  int list[12] = {8,1,11,4,2,9,10,5,3,12,6,7};
//can go faster but this is easiest to read
for(int j =0 ; j < 12; ++j)
{
 std::cout<<list[j] << " ";
}  
//output a new line
std::cout << std::endl;
return 0;
}
LeoC++ commented: Thanks!! Great fast accurate answer! +0
tetron 22 Junior Poster

if you load the file #include <fstream> read in the file using std::ifstream("folder\\file.txt" now two options load into a single string or line by line.
Single std::string will be easiest

there are google articles on how to do this

The next thing is

std::string data;
//load data

//this is like an int showing current loc in string
std::string::size_type pos(0);
std::string::size_type cur_pos = data.find('<', pos);
if(cur_pos != std::string::npos)
{
//found the start of tag
//advance to first letter
+cur_pos
//now find end
 pos = data.find('>', cur_pos);
std::string tag = data.substr(cur_pos, pos - cur_pos); 
//check is closing tag
if(tag.size() > 0 && tag[0] = '/')
{
   tag = tag.substr(1);
   //are we inside or not
}
else
{
  //new tag
}

what else you have to do is add
a tag to an open std::vector<std::string> and if it closes it should be the last tag on the list
you have to store the strings between tags as parameters

now I have left a fair bit for you to do yourself here.

But if you assume no errors in xml and no self closing tags the
above methods should suffive

can anyone give me some hints on this im having issues

thanks

tetron 22 Junior Poster

First glance it is

using namespace std;

causes your compiler to find std::copy instead of copy
when it looks at copy

tetron 22 Junior Poster

Coming in late you have colours that you want to convert to different types and formats:

This would normally be a single set of formulae for each conversion and would not require a convert

but if you are pairing names with values and vice versa

couldn't you just use maps
one with key of string and colour value
one with key of the colour you want and string value.


otherwise a single pointer full look up could be implemented if speed is critical

edit: > hmm lots of posts came inbetween when I started and finished this

josolanes commented: Thanks for the ideas! :) +1
tetron 22 Junior Poster

Active window can have changed.

http://msdn.microsoft.com/en-us/library/ms633497(VS.85).aspx

is an EnumWindows function that appears to let you step through all of the windows
if GetDlgItem is failing to find your resource it could well be the wrong window although I though t it returned a handle

If this doesn't help the question might be down to how you are handling the messages do you then post a message to the window? Why can't maincontrol just be an event listener in a window?

how does your tab page (? type of tab control work)

Sorry, I'm probably being a bad understander today:)

tetron 22 Junior Poster

The use of active window might not be what you want.

Where are you creating the window?

If you have visual studio there are ways to avoid having to do all this.

But with an object like a button it is an object that has to be attached to a window so you first need to have a window to add it to.

The active window may not give you this level of control to be able to add things.

If you try going through the second example I gave to get a window to show with a button I think you might see the point that I am making.

the reason for ASSERTing that the handle was not 0 is in case you had not initialised the window propperly. It is tricky to create a window but once you have done it once the code does not alter.

I have had temporary good results using ff code:

CWnd* myWnd = GetActiveWindow();
CWnd* pControl = myWnd->GetDlgItem (IDC_EDT_XXX);
HWND hwndControl = pControl->m_hWnd;
pControl->SetWindowText("Good!!!");
pControl->InvalidateRect (NULL);
pControl->UpdateWindow ();

This worked once, after that the trigger event doesnt trigger any more and I get the Access Violation again.

tetron 22 Junior Poster

I have become a little confused...

You want to add a button at runtime and then set its text?
At first glance what might be going wrong is the SetWindowText I think the button has to exist for this
to work and it doesn't until your create it.

to add a button as well as its constructor
you need to create it

try google or msdn:
http://msdn.microsoft.com/en-us/library/bw4e0cww.aspx

a tutorial that seems ok too is:

http://www.functionx.com/visualc/controls/button.htm

Your problem that you are showing there though is not dependent on the handle so I might not be answering your question very well.

tetron 22 Junior Poster
if(r1 > r2)
{
}

I had already given an example like this (see page 2) the purpose of the bool b was to try and demonstrate how the compiler interprets r1 > r2 and if(condition) but I might have missed the mark

tetron 22 Junior Poster

This is going to be a stupid question, but If I do use the

//tet: changed the function name and removed friend
bool greater_than (Rational &Fraction1, Rational &Fraction2);

Operators are really just functions that the compiler can read in a user friendly manner

so to use this function

if(true == greater_than(r1, r2))
{
std::cout << "rational number r1 is bigger than r2" << std::end;
}

you should convert back to a single rational number I think if you work out in the short Rational example I gave whatshould go into the operator function it would help you

an operator> returns a bool this can be used with if etc

but it has a special layout

bool b = (r1 > r2);
if(b)
{
}
WaltP commented: Tetron, use periods and commas. I'll start infratcin if you keep making unreadable comments. -2
tetron 22 Junior Poster

It is nice to have friends but as a programmer you should not see friend s very often. It doesn't want to be here.

It is allowed to have a class that has two rational numbers in it but you should probably give it another name:

if we go back to what a single rational number is

numerator/denominator

say

2/9

then if we had another rational number

3/11

it makes sense to
say

if(2.0/9 > 3.0/11)
{
std::cout << "first is bigger" << std::endl;
}

I have had to add the .0 to stop the int making both numbers 0

now the computer knows how to compare float and double and you know how to convert a single rational number to a double

class Rational
{
public:
Rational(int num = 0, int denom = 1);
double convert_to_double();
bool operator>(const Rational &r) const;
private:
int numerator. denominator;
]

this is a simplified class
now here note that Rational only can be set in the constructor

double Rational:: convert()
{
 double ret(numerator); //change numerator to 
 if(denominator > 0)
 {
    ret /= denominator;
 } 
 else
 {
    std::cout << "error denominator should not be 0!" << std::endl;
    ret = 0.0;
  }
return ret;
}

in main we want

// r1 = 2/9
Rational r1(2, 9);
//r2 = 3/11
Rational r2(3, 11);
if(r1 > r2)
{
  std::cout << "r1 is bigger than r2" << std::endl;
}

double d1 = …
tetron 22 Junior Poster

I assume that you are asking this question for a windows system which won't let you create a directory from fstream so you need

#include <windows>
int main()
{
std::string folder("C:\\my_folder");
CreateDirectory(folder.c_str(), NULL);
return 0;
}

The Null is for the security settings which might let you lock the folder but I'm not sure whether you will be able to take control of the harddrive using legitimate means

Hi everyone. I am trying to write a program that controls access to my hard drive.i.e when this program is running, the specified directory can not be accessed. But first, s there any way one can create a folder using c++?

tetron 22 Junior Poster

I probably should not jump into a thread where so many people have ready jumped in but here goes...

You have a lot of posts but clearly there are somethings that have never quite grasped.

First what you have to understand is what a rational number is:
an integer is any whole number ie , 45, 678687, -23
and a rational number is one integer on top of another integer

ie 2/9 rather than the decimal 0.2222222

but if you set a float as 2/9 you would get the output to show 0.222

The idea is that you write a class to store the number

This is saying like, int, double, float you want to have something to store your number

in this instance your number say 3/7 needs to be stored in 2 parts
a numerator and a denominator
where here:
numerator would = 3
and
denominator = 7

so first as you cannot say
Rational r = 3/7;
as the compiler sees 3/7 differently from you
you have to tell the class how to load in the numbers

so you would have

r.numerator = 3;
r.denominator = 7;

and to display the number:

std::cout << r.numerator << "/" << r.denominator << std::endl;

but this would only work with numerator and denominator being public:

what is happening is each time you delcare a rational number a

jonsca commented: well done +2
PDB1982 commented: Thanks Very Much! You actually took the time to explain the topic to me, which makes this the greatest help I've ever received on this site. Thanks again! +1
tetron 22 Junior Poster

I am rusty on this but as you say if hWind = 0
then you are using a method that requires a handle to a window if this is 0 it most likely means that you have not created a window successfully. So your new might not have created a window!

Another possibility is that you just need a method to get either ther current window or its parent without seeing context it is tricky to say for sure.

It would be helpful if you had included the code where m_hWind is called :)

Hi @ all, b4 things get The question is why since I create an instance of BERT_EVAL which is CTabPage based.

Thx in advance.
t.o.

tetron 22 Junior Poster

the [] only work if string is defined

string has useful methods such replace()
find()

if you read up on the methods you will find cleaner solutions

but at first glance I think your error is here

output += alpha[t]

output[r] = alpha[t];

tetron 22 Junior Poster

If your string is just a std::string then your write methods are not taking advantage of the methods available to you .c_str() this converts a std::string into a char *

and .size() gets you the length of a string

therefore

std::ofstream fout("test.txt", std::ios::bin);
if(fout.is_open())
{
 fout.write(str.c_str(), str.size());
 fout.close();
}
tetron 22 Junior Poster

There are a huge amount of resources on minimising errors. Designing the idea before you charge in for classes is a good idea.

For this case the use of the word const would cause the compiler to pick up the error

void set(const int x);

another common mistake that can be difficult to spot is:

if(x = 0)
{
//this will not run and x has been set to 0 
}

you can write in reverse

if(0 == x)
{
//this would have given a compile error with single =
}

In the long term having a test class to test every function you write can be beneficial. but normally this is too time consuming to be attractive until things have gone wrong

The first thing that you want to do is comment your code so that you know what you intended to do later

Choose meaningful function names.

There are many other pitfalls to watch out for and you will get better at spotting them if you keep your code short and in classes you will be able to reuse your code and so you can know where the error is.

The last thing I would recommend is always use braces {} for any if
- Spotting a wrongly matched else when they are not used can be very difficult

Thank you very much, that fixed the issue. I always make one careless error like this. Anyone have any …

tetron 22 Junior Poster

This is unusual behaviour that you are getting and I would make sure as others mentioned that you are writing to a place that you should be able to write. I have had unusal interactions occasionally in the past and to minimise you could consider the following steps

It might be worth:

- creating a new project for the main in case their is some IDE corruption

- use an explicit file location equivalent to your My documents
C:\\Documents and Settings\\user\\

the \\ is needed for a single \
output the filename to screen to check the location

check that you can create a file in this folder using some basic program or copy and paste.

put everything in a try catch block and see if an exception occurs

I am not familiar with your OS and it could just be that the IDE does something odd to prevent the executable running but it doesn't sound like it should be behaving the way it is.

Hi - I copied and pasted your code in, and it compiled, but when I chose build and run, no file seems to have been written, but neither did I get the "could not open file" message (in my version, I did get the "File didn't open" message). I did a search for the file on the hard disk anyway, but got zero hits. I'm thinking I'll try it on the computers at the school …

tetron 22 Junior Poster

I assume that your code outputs "cannot open file" and does not crash

it is normally a good idea to put markers next to chars when outputting as there are many chars that appear as space and sometimes buffers hold onto return characters for example.

I normally use is_open() method to check

One thing to try is to write a file using

#include <fstream>
#include <string>
#include <iostream>
int main()
{
std::string file_name("test_file.txt");
std::ofstream fout(file_name.c_str(), std::ios::out);

if(fout.is_open() == true)
{
//currently doing nothing other than create the file
fout.close();
}
else
{
std::cout << "could not open file!" << std::endl;
}

return 0;
}

the idea is to see where the file writes if it works but you can't see
the file search for it. In XP some applications can lock the file and stop you from opening another problem can be that characters in your string might not be what you think they are as wchar can look identical to chars and your compiler just assumes that the name is correct.

tetron 22 Junior Poster

Why not just iterate through the vector once
you have your test condition is_odd()
this has to be tested once per element if as you say it
is more complicted.
then add to v1 if true
and v2 if false

an example using std::vector<int>
sorting on the contents rather than the indices
if you want the indices this only requires adding and incrementing an int.

The sort algorithms seem somewhat slow to me as you only want to split not reorder

//std::vector<int> orig somewhere higher up
std::vector<int> v1, v2;
//if known estimare sz of two v1
v1.reserve(v1_sz);
v2.reserve(orig_sz - v1_sz);

std::vector<int>::iterator it_stop(orig.end());
for(std::vector<int>::iterator it(orig.begin(); it!= it_stop; ++it)
{
  if(is_odd(*it))
  {
     //condition 1 say is odd
     v1.push_back(*it);
 }
  else
  {
     //condition 2
     v2.push_back(*it);
  }
}

Naming convention is intentional, reminds me that unary predicate passed to remove_copy_if shall return negative result of what I want to copy ;)

PS1. Your solution also needs to check the whole vector items 2 times.

PS2. My current solution is based on what I've found in another thread, now it looks like that:

{
  ItemVector::iterator it = std::stable_partition(vec.begin(), vec.end(),isOdd);

  std::sort(vec.begin(), it, srt1); // no stable sort needed here

  std::stable_sort(it, vec.end(), srt2);
}

Thanks anyway :)

tetron 22 Junior Poster

google will show you lots of information and tutorials

I've found google often to be useful for programming
1 - type the problem into google
2 - find a link to daniweb
3 - normally I find a post on the subject from ancient dragon
;)

Sockets is something that if you are not making an assignment it is normally best to try to find code that you can reuse that has been implemented and tested elsewhere. Things perhaps to look into are topics such as web spiders and bots

tetron 22 Junior Poster

Using sensors, pref motion detecting &heat-signature, & a camera. When the "intruder" gets into the room, camera records /captures the image. Then processes this image to discern if "intruder" is a real threat or just a dog. So this differentiation process is what I don't know if its image processing or ?

If you are trying to identify a person in a situation where they might actively be trying to deceive your sensors then you have a simple shape recognition problem.

In the first instance you need to be able to process your data to convert it into chunks that you can use. Which means avoiding having to write you own codecs

So to modularize your problem it is probably simplest to assume that you have a single frame and want to identify whether there is a human in the picture.

First you have have your motion sensors that tell you that there is something in the room, these have limited recognition benefits but if you are using a neural net it does not hurt to add in a module that tries to decide if it is a person based purely upon the motion sensors.

The other two sensors you have produce a displayable image and it is easiest to use data that you can recognise as your input. This means a single .bmp image either taken from the infra-red or ordinary camera.

Now you can get ordinary cameras that just take a sequence of …

tetron 22 Junior Poster

To go from the right hand side index to the left handside index the maths computationally is as follows

int cycle_rhs(3), cycle_rhs(6);
int temp =  x/cycle_rhs; //this gives a floor so no remainder
int temp2 = cycle_lhs * temp;
/*
0 , 1, 2 ->0
3, 4, 5 -> 6
*/
int offset_lhs(3);

y = temp2 + offset_lhs + x%cycle_rhs;

I note though that you are doing a matrix mthod why
not have a

std::vector<int> get_column(int id);
bool resize_matrix(int width, int height, int value = 0);
bool set_column(std::vector<int> &vals);

set of methods

I'm trying to find a formula for

copy[3] = b.data[0]
copy[4] = b.data[1]
copy[5] = b.data[2]
copy[9] = b.data[3]
copy[10] = b.data[4]
copy[11] = b.data[5]

letting the right hand side be b.data, I can see this is like

copy[3] = b.data[0] multiply by 2, add 3
copy[4] = b.data[1] multiply by 2, add 2
copy[5] = b.data[2] multiply by 2, add 1
copy[9] = b.data[3] multiply by 2, add 3
copy[10] = b.data[4] multiply by 2, add 2
copy[11] = b.data[5] multiply by 2, add 1

there's obviously a modulo pattern going on so I've tried

copy[i*2 + (rows +(-1*i) % (rows + 1))] = b.data_;

(rows = 3)

which gives me the adding pattern 3 2 1 0 3 2 1 0

however I need to miss out the 0, can anyone see how to do this?

Thanks in advance!

tetron 22 Junior Poster

It sound like quite an ambitious project if you don't have an idea of what algorithms to use.

You need to give a bit more information

What kind of recognition :
Camera footage - > simple recogniser or machine learning techniques are possible

Or are you talking about distinguishing between bot behaviour and a human. Here it tends to be repetitiion and efficiency that is the giveaway. And you need to think about what input you are using mouse movement, etc..

Without knowning the algorithm the only hints I can give is if you have a set of human and non-human data you could try reading about either
Hidden Markov models
or Neural Networks

Hello,

I have a software-based project of using human recognition algorithm to detect an intruder and be able to differentiate if the intruder is human or non-human.

Any ideas on what recognition algorithms I could use?:)

Thanks!

tetron 22 Junior Poster

Assuming that x1, y1, x2, y2, x3, y3

c = y3 - (y3 - y2) * x3 + a(x3^2 - x3*x2^2 + x3^3); (4)

is of the form c = k1 * a + k2; (a)
k1 and k2 are both directly calculable

combined with
c = k3 * a + k4; (b)
therefore
k1 * a + k2 = k3 * a + k4
(k1 - k4) = a * (k3 - k2)
a = (k1 - k4) / (k3 - k2);

from (b)
c = k3 *(k1 - k4)/(k3 - k2) + k4;
where
k1 = y3 - x3 * (y3 - y2);
k2 = (x3^3 - x3 * x2^2 + x1^3)
k3 = y1 - x1 * (y1 - y2);
k4 = (x1^2 - x1 * x2^2 + x1^3);

I have done this rearranging quickly so there is a small
risk of an algerbriac error

The only requirement is that k3 != k2
which is that none of the points are identical to each other

Having said this if this is not just trying to find the quadratic for
drawing a graph a more computer based task is probably wanted.

There are several other options you can use:
1 - a convergence technique
2 - Write code to rearrange simple formulae (like I have done)
3 - Matrix definition of the …

tetron 22 Junior Poster

I did not spot anything obvious but in the main
line 36
your while looks a little dangerous while(que.size() != 1) I would have expected while(que.size() > 1) as you are poping off two values.
I suspect that changing this will have no effect

When you create your new InternalNodes for temp
there is not a new() so the temp pointer will not point to valid memory you probably could do with cleaning up your new();
with delete

But I am probably missing the exact error

tetron 22 Junior Poster

as with any programming maths problem first you need to know
roughly what you are trying to by hand.

Another important factor in this instance is why you are doing it. If it is just to find the answer then just put the equation in a function. But if it is to use a particular set of programming tools then it is important to know which ones you have to use to solve the problem.

Are you trying to find a, b, & c
as finding the best fit to three points?

in which case you can either iterate in steps for a, b, & c
to see which values give the smallest error or rearrange
c = y1 - a * ( x1 * x1) - b * x; (1)
b * (x1 - x2) = (y1 - y2) + a * (x2 * x2 - x1 * x1); (2)

(1) ->

c = y1 - (y1 - y2) * x + a( x1* x1 - x1* x2* x2 + x1* x1* x1); (3)
you can use 3 with y1->y3 & x1->x3
like a simple simultaneous equation
to find c then a then b.

If the algebra is too scary even from here, you can iterate over 'c ' and find the values
of 'a' that fit then you can use these values to plot a graph

The matrix methods would be trickiest …

tetron 22 Junior Poster

Now the main problem with this function is that your nmax should be at least the size of of the elements in your matrix but ignoring
this problem

to see if an element exists simply means to check if getVal == 0;
this has an overhead so why no just reuse your code

//needs a bool to let you know
bool SetVal(int i, int j, double Val)
{
  bool ret(false);
if(rValid(i) == true && cValid(j) == true)
{
//valid coordinate so
ret = true;
  for(int c=0;c<3*nent;c+=3)
  {
     if(data[c]==i && data[c+1]==j)
     {
         //must be ok to set
         data[c + 2] = Val;
        return ret;
      }
   }
//we have a new coord
++nent;
 if(nent < nmax)
 { 
//original code for SetVAl
    int ind =3*nent;
   data[ind]=i;
   data[ind+1]=j;
   data[ind+2]=Val;
 }
 else
 {
  //overflow why you need a bigger max
  }
}
else
{
//coordinate out of range
}

return ret;
}

you should check that both matrices have same width and height before adding and subtracting


for multiplying you will have to change nmax = nr*nc /2; lose the /2
so

nmax = nr *nc;

the only thing that I have not covered is that if setVal takes a
Val of 0
that you will wnat to remove the entry from the list
and you might sort the list for a real application

void sMatrix::setEl(double r,double c, double val)
{
	int i;
	if (rValid(r) && cValid(c))
	{
		// when adding a …
tetron 22 Junior Poster

Identify a project that is of interest to you.

Do you have any boardgames that you like the advantage of picking such a project is that you can test that your design ideas
are implementable by making test versions of some of the design.

This will not be a waste of time as you might even want a final working version to play around with yourself for fun.

The other plus is that there are core elements such as the
display
logging positions
AI

and you have the option to extend the design to a multi-user web application with databases of ranks etc.

So that you have several options as to where you can stop your design.


Be careful not to pick something that you don't know how you
would design the modules. Remember this doesn't need to lead to an application that is better than anything else that exists it is about thinking through the design and viewing pitfalls. This should therefore include test modules to test your functions.

adcodingmaster commented: A good and brief reply. Thanks +0
tetron 22 Junior Poster

When you say is there a more efficient way to get the shape than a txt file the answer is probably no. But it is always important to know what it is you are wanting the computer to do before you start programming.

Looking at you function get_shape it is not obvious what you are trying to with it.

It looks like that you are trying to load a char is this supposed to be used for the fill function? is this supposed to identify the shape?
Or is it supposed to load a shape into the memory?

If it is the first one then it is an easy fix.
I imagine identifying the shape is well beyond the scope of the problem. But you might have a choice of shapes say, square, rectangle, hexagon, triangle, circle.

Prompt the user for information to get them to choose the shape ie

cout << "Please choose a shape 1-5:" << endl;
cout <<  "1: square" << endl;
cout << "2: rectangle" << endl;
//other shapes 
int shape_id;
cin >> shape_id;

Then you would use shape id to select a draw method such as
the earlier method did with squares. I would recommend that you
stick to squares and rectangles until you get everything else working.

Then getsize()
are you trying to get the user to tell you how big the shape should be? If so you want to get a rectangle …

tetron 22 Junior Poster

Following-on from some of your comments in the thread that has been closed on this some basic things to note:

This is not a standard way of dealing with matrices and the name of
the class is misleading.

What is happening is that the data is being logged as a series of
points with each value having a row and column stored for it

In the constructor I am still uneasy about the nmax = nr*nc/2;
for your example nr = 3, nc = 3 so nmax = 4 (rounds down as int)
And therefore you can only have four records maximum even for a sparse matrix this can be an issue for adding.

As you have started to write your get_val needs to search the data
until it finds the point that you are looking for to find the value.

You have a mistake in your for loop stop condition: for(j = 0; nent--; j++) you are changing nent each time you go through the loop so when you set the next value...

When you add
+= (is a special function known as an operator so does not exist for your new class);
you need to add an element by
check size of M & N are the same
getting all of the elements from N
see for any element N != 0
then you need to find the i, j for …

tetron 22 Junior Poster
LinkedPQNode* current = NULL;
        current = queues[i]; // error is here

It is always useful to give full error code and all the initialisation but I am fairly sure I have spotted the error

You say queues is a SinglyLinkedList singlyLinkedNode * p_sll; could point to a LinkedPQNode because it LinkedPQNode is a type of SinglyLinkedNode that is what you tell the compiler when you use inheritence

but queues is not a type of LinkedPQNode so current cannot point to it;
I suspect you meant current->function(queue[i]); But I don't know what you want to do with the line that is failing
your while loop immediately after might well fail

I suspect that current is merely the wrong type of pointer

and you mean the equivalent to:

if(0 != queue[i])
{
 queue[i]->write(outfile);
}

but wanted a pointer to do some of the work but used the wrong pointer but be aware of what I said about streams as variables!

tetron 22 Junior Poster
current = queues[i];

our old object files so I can't put any kind of access method in singilyLinkedList

When reusing old code it is still permissible to extend the class
as long as the extension does not alter any existing behaviour and the method is something meaningful to the code itself.

It is helpful if you post error codes and the piece of code where the problem line is.

But first a couple of observations: the code tags have highlighted remove as a keyword and so an alternative function name might be needed.

Second for the line you are using to work current = queues[i] There has to be a definition of operator[] if this is an array this should be fine and current needs to be a compatible pointer to queues

ofstream and ifstream are funny objects and cannot necessarily be passed into a function meaningfully. It is probably a good idea to place the ifstream and ofstream inside of the function and pass in a file_name.

write can fail and should return a bool

nothing else jumps off the page immediately - the error code is probably needed and the lines where you call the queues;

tetron 22 Junior Poster

I am trying to start the odometer reading at 0 to start with, but I want it to increase everytime the user inputs MilesDriven. I can't seem to get it to work...any ideas?

1st Do you know what a constructor is

in you class
add in the public Odometer(); there is no return

now this function gets called when you create
an Odometer now you can
set an intial value in this function
so you can make sure that you have set a value to 0
by calling Odometer_reset you can then return the value to 0

a function needs to be written like has been done for MPG()

tetron 22 Junior Poster

Is there a way to modify that function where it can get a different shape instead of just a box?

Yes, there are ways to modify it
and it is the general priniciple of putting things in functions is that it is easy to change

What you have to do to draw other shapes is define what you want to look like first

so you have a grid

0000#0000
000#0#000
00#000#00
0#00000#0
#########
000000000

You can draw a triangle like that
you will be able to find some examples on the web for this and
circle
I assume that you have some basic geometry:
A triangle is defined by three points

so you grid has an [row, column] for each cell
and you need to tell the computer when to draw a #

If you pick your three points as a coordinate
Then you can work out what the equation of the line would look
like for each side:

y = m*x + c

you have y1 & x1 for one corner and
y2 & x2 for the other this lets you find m & c via a
simultaneous equation

y1 = m*x1 + c (1)
y2 = m * x2 + c (2)

(1) - (2)
(y1 - y2) = m * (x1 - x2)
rearrange:
m = (y1 - y2) / (x1 - x2) (3)

programer411 commented: it was good +1
jonsca commented: Mor rep for this thread that never ends :) +2
tetron 22 Junior Poster

To test getshape() will take for ever if you don't get it right
what you need to do is to test your function first for ideas that you can check by hand.

1000*1000 inputs will take a long time so
use smaller MAX values
if you need something this big you will have to load from file
for now I recommend that you have a test get_shape() function
that just gives you a square and a display function so that you can see what is happening then run your own get_shape for a small input and see what happens when you call display
so you want to add something a bit like:

const int max_row(5), max_col(5);
void dummy_get_shape(char shape[max_row][max_col]);
void display_shape(char shape[max_row][max_col]);

int main()
{
	char my_shape[max_row][max_col];
	dummy_get_shape(my_shape);
	display_shape(my_shape);
	return 0;
}

//gets a big empty square
void dummy_get_shape(char shape[max_row][max_col])
{
	char edge('#');
	char empty('\0');
	for(int c = 0; c < max_col; ++c)
	{
		for(int r = 0; r < max_row; ++r)
		{
			char to_add = empty;
			if(r == 0)
			{
				//left edge
				to_add = edge;
			}
			else if(r == max_row - 1)
			{
				//right edge
				to_add = edge;
			}
			else if(c == 0)
			{
				to_add = edge;
			}
			else if(c == max_col - 1)
			{
				to_add = edge;
			}
			shape[r][c] = to_add;
		}
	}
}

void display_shape(char shape[max_row][max_col])
{

	for(int c = 0; c < max_col; ++c)
	{
		for(int r = 0; r < max_row; ++r)
		{
			std::cout << shape[r][c];
		} …
jonsca commented: Rep for this monster thread +2