Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If its the source code you want to protect, then the best you can do is put a copyright notice at the top of the software, then when you find someone who has stolen it you can sue him for copyright infringement. But lawyer fees might make that impractical.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't know about faster, but here is another way that only requires a single function. This function only writes to the screen, not to a file.

#include <string>
#include <ctime>
using namespace std;

void Passwords(int size)
{
    std::string pwd;
    pwd.resize(size);
    for(int i = 0; i < 128; i++)
    {
        for(int j = 0; j < size; j++)
        {
            char c;
            // do not allow duplicates in the password
            do
            {
                c = 35 + (rand() % 91);
            } while ( pwd.find(c) != string::npos);
            pwd[j] = c;

        }
        cout << pwd << "\n";
    }

}

int main()
{
    srand( (unsigned int)time(0) );
    Passwords(15);

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Is this the right way to do it?
There are two ways to do it. The preferred way is to use try/catch blocks

int main()
{
    int * bobby;
    try
    {
        bobby = new (nothrow) int [5];
    }
    catch (std::bad_alloc &ba)
    {
        cout << "Oops!  memory allocation failed\n";
        return 1;
    }
    cout << "memory allocation ok\n";
};

The other way (for legacy code) is to use the std::nothrow version

//standard C++ code
#include <new> //required for nothrow new

CWindow p;
p = new(std::nothrow) DerivedWind;
if (!p) //fine now
{
 cout<<"allocation failure!"
 exit(1);
}
//...continue normally

Here is a good link you should read

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I moved this to a new thread to avoid hijacking the other thread. Continue the discussion about the new operator here if you wish.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yeah, you were right, in the book they we're talking about dynamic memory allocation with the new operator, and when allocation with new fails it returns a special pointer value: NULL , otherwise the pointer is pointing to the newly assigned memory :)

Either you are reading a very very old book or its just plain wrong. new operator does not return NULL, it throws an exception and leaves the pointer unchanged.

mplementation

In compilers conforming to the ISO C++ standard, if there is not enough memory for the allocation, the code throws an exception of type std::bad_alloc. All subsequent code is aborted until the error is handled in a try-catch block or the program exits abnormally. The program does not need to check the value of the pointer; if no exception was thrown, the allocation succeeded. The implemented operations are defined in the header <new>. In most C++ implementations the new operator can also be overloaded to define specific behaviors.

http://en.wikipedia.org/wiki/New_(C%2B%2B)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

But my C++ Book says so, by the way, why are we otherwise checking whether an assignment has failed or not using if(ptr == NULL) ?

You misunderstood your book. Better read it again. Maybe the book is talking about smart pointers

tux4life commented: You were right, actually I don't have to doubt about what you say: You're just *always* right :) +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The way I did it was create a new filename every time I wanted to open the file. That way when the date changes my algorithm will automatically pick up the new date.

time_t now = time(0);
struct tm* tm = localtime(&now);
char filename[255];
sprintf(filename,"%04d%02d%02d", 
    tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
// now open the file
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> cout<<"Hello " << strcat(first_name, last_name) <<"!"<<endl;

Don't do that! every time that line is executed strcat() will append last_name to the end of first_name, eventually causing the program to scribble all over memory.

A solution is to declare another variable char name[255]; and after entering the last ane first names, copy them both to that variable -- only ONCE. sprintf(name, "%s %s", first_name, last_name); Do you know about std::string c++ class yet? Are you allowed to use it in your program? If yes, then replace all those character arrays with std::strings

std::string last_name;
std::string first_name;
std::string name;
...
...
name = first_name + " " + last_name;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I have run your program several times and don't see a problem other than you need to put a space between the first and last names when you print them on the screen.

If you don't want to have to type your name every time you run the program then you need to save that info to disk. Then the first thing the program should do is prompt for the account number. Check the data file for the account number and, if found, read the first name, last name, and current balance. Otherwise if the account number is not on file then prompt for the first and last number. Before exiting the program it needs to write that information back to the file.

Also the name is getting printed wierdly

Welcome to First National Bank ATM!
ATM software developed by: Shaun Dougherty
Please Enter your first name
mel
Enter your last name
sto
Hello melsto!
Please enter your account number
100
Welcome, doejohndoe<<<< HERE 
Account #:100
Current Balance: $2000.99
-----------------------------------------
 **********************************
 *               MENU             *
 *  1. Deposit     2. Withdrawal  *
 *  3. Fast Cash ($60) 4. Check Balance *
 * 0 = exit                             *
 ****************************************
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

After making the change I suggested, I get this output

ctor1 is called!
ctor1 is called!
ctor1 is called!
ctor1 is called!
//////
//////
//////
//////
dctor is called!
dctor is called!
dctor is called!
dctor is called!
Press any key to continue . . .
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Try this:

#include <cmath>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

class mVect
{
    private:
        // NO PRIVATE DATA
    public:
        double i, j, k;
        mVect();
        mVect(double,double);
        mVect(double,double,double);
        void print(); // NEEDS EXTRA LIBRARIES
};

// INITIALIZE EMPTY VECTOR
mVect::mVect()
{
    i=0;
    j=0;
    k=0;
}

// OUTPUT THE VECTOR IN THE FORM [i,j,k]
void mVect::print()
{
    cout << "[" <<  i << "," << j << "," << k << "]\n";
}


// INITIALIZE 2D VECTOR
mVect::mVect(double a, double b)
{
    i=a;
    j=b;
    k=0;
}

// INITIALIZE 3D VECTOR
mVect::mVect(double a, double b, double c)
{
    i=a;
    j=b;
    k=c;
}

void main()
{
	ofstream out_file;
	ifstream in_file;
    int y;
	char *out_name = "test.bin";
	out_file.open(out_name,ios::binary);

	double matrix[2][3] = {1,2,3,4,5,6};
	// write matrix to a disk file.
	for (y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			out_file.write(reinterpret_cast<char*>(&matrix[y][x]),sizeof(double));
		}
	}
	out_file.close();
	cout<< "Matrix has been written to test.bin."<<endl;

    // read the matrix from the former file.
	double matrix2[2][3];
	in_file.open(out_name,ios::binary);
	
	for (y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			in_file.read(reinterpret_cast<char*>(&matrix2[y][x]),sizeof(double));
		}
		mVect vec(matrix2[y][0],matrix2[y][1],matrix2[y][2]);
		vec.print();
	}
	in_file.close();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Does curl involve WinApi (like i believe winsock does, or at least it seems that way) ?

Probably, but its transparent to us programmers because its cross-platform. It uses the api from whatever platform we want to use it on.

I am having huge trouble finding my way around the WinApi :/

Join the party :) We all have problems with it because it is so huge.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I see one problem -- line 61 default constructor does not allocate any memory for GridUnit so when the class is destroyed it will try to deallocate an unallocated pointer. If you don't want the class to allocate memory then set gunit = 0; and all will be well.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>T.setB(4).setC(5);

The second method you posted is preferred. The above method is unconventional and may be easily misunderstood and confused by readers.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If this turns into another "I love you Dani" thread, I swear I will ban you! (not really, but I'd like to).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to completely rewrite the file.

open original file for reading
open temp file for writing
while not end-of-input-file
   read a line from input file
   if this is the line to be replaced
        write new line to output file
   otherwise 
        write the line to the output file
end loop
close both files
delete original file
rename temp file with the name of the original file
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Test T(1).setB(4);

Should be

Test T(1);
    T.setB(4);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>im waiting for your reply
I'm not going to write it for you. YOU write the code and we will halp with whatever questions you may have. But the code you turn in must be your own, not ours or mine.

tux4life commented: Well said :) +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>i should use sql file as database format
That means nothing because there are thousands of sql file formats.

>>please help me to do this
I already have told you how I would approach the problem. But since I have not been in your class lectures I can't be sure if I was right or wrong.

The first thing I would do is write a program that reads the schema file and builds the linked list I already mentioned.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The requirement for CREATE TABLE indicates that the table contents should be read from a text file (aka schema), not hard-coded in your program.

One common way to do that is to create a structure that contains the information for each field in the table

struct field
{
    std::string field_name;
    int length; // only for strings
    int dataType; // either 1 = string, or 2 = numeric
    bool PrimaryKey; // true = this is a primary key field
};

Now when the program reads the schema file it should build a linked list or c++ vector of the above structures for each field.

What is ambiguous to me is: does your instructor expect you to actually create a table in a real SQL database such as MS-Access ? Or are you to just use a text file of your own design for it?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Which edition of 2008 are you using (Express, Pro, etc) ? I have the Express version, downloaded the SDK Aug 2008, set up the directory paths, compiled and linked a sample program, and all went without a problem.

Before trying to write your own problem compile one of the samples to make sure your compiler's directories are set up correctly.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use references

class MyClass {
public:
  typedef vector<int> Vector;
  MyClass();
  ~MyClass() {;}
  Vector&  getVector() { return m_vector; }; 
private:
  Vector m_vector;
};

MyClass::MyClass() {
  m_vector.push_back(1);
  m_vector.push_back(2);
  m_vector.push_back(3);
  m_vector.push_back(4);
}

int main() {
  MyClass t;

  // This works...
  MyClass::Vector& myVector = t.getVector();  
  for(MyClass::Vector::iterator itr = myVector.begin(); itr != myVector.end(); ++itr) {
    cout << (*itr) << " ";
  }
  cout << endl;

  // This doesn't
  for(MyClass::Vector::iterator itr = t.getVector().begin(); itr != t.getVector().end(); ++itr) {
    cout << (*itr) << " ";
  }
  cout << endl;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

To increment (*ptr)++; Compile and run this little test program and you will see what it does.

#include <stdio.h>

void foo(int* n)
{
    (*n)++;
}

int main()
{
    int n = 0;
    int i;
    for(i = 0; i < 5; i++)
    {
        foo(&n);
        printf("%d\n", n);
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>It's urgent for me pls sir

But it's not urgent for any of us. I could care less whether you pass or fail -- its your grade not mine. And we are not all men, so "sir" is inappropriate.

Post what you have tried, or have you even tried to write it yourself?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

just after cin make a test for cin.good(). The program is adding n to total whether cin.goo() is true or not.

Or you could reset n to 0 before the cin line.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

add another int variable and increment it inside that loop -- that will count the number of iterations that loop made. Then do the math after that loop terminates.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

thats nice ancient dragon how about the total is divided by the times you input no.


long n is divided by how many times you input a no. :)

I'll leave that up for you to do. I don't want to spoil all your fun;)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Do you see an if statement in the code I posted? If you don't understand how it works then compile and run it yourself. When you are done typing just enter any non-numeric character, doesn't matter what you type as long as its not a digit.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you could write a copy function to avoid the duplicate code you have

char* copy(char* to, char* from)
{
   while(*from)
     *to++ = *from++;
   *to = 0;
   return to;
}
char* cat(const char *arg1,const char *arg2)
{
    char *p = new char[strlen(arg1)+strlen(arg2)+1];
    copy( copy(p, arg1), arg2);
    return p;
}
Sky Diploma commented: Thanks-- That Really Helped. +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 23: oops! that just lost the location of the memory that was allocated on line 13. You need to use a different pointer for incrementing and leave the original pointer unchanged.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Index would be similar

if(highTemp >= YrTemp[x][0])
      {
           highTemp = YrTemp[x][0];
           highIndex = x; // set index to be returned
     }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You could use any non-numeric character as the terminator.

int main()
{
    long n;
    long total = 0;
    while(cin.good())
    {
        cout << "Enter a number or 'n' to stop\n";
        cin >> n;
        total += n;
    }
    cout << "total = " << total << "\n";
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>pow(+1.0, n+1)/pow(n*n,2.0);
How does (1/9) translate to the above equation? When n = 1, the above will be pow(1,2) / pow(1,2) which is 1^2/1^2 = 1. When n = 2, we get pow(1,3)/pow(4,2) which is 1/16

It looks to me your formula is completely wrong. How you get from (1/9) to (1/25) I don't know, but it doesn't work that that formula or program.

Another point about that equation -- pow(1.0, n+1) -- the result will always be 1 so the formula boils down to 1/pow(n*n,2.0)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

that's dumb.

what if he wants to add 9999 as one of his numbers

That's a common way to terminate user input, another is to type Ctrl+Z. If he needs to let the user enter 9999 then his program should choose a different number to terminate input.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Then if you did write it (I'll believe you for now) why are you begging someone to finish it for you? Surely you are smart enough to do it yourself. I already told you there are only two things left to do. So get to doing them. We don't mind answering questions about what or how to do it, but please don't ask us to finish the program for you.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

My guess is that you did not write the code you posted but copied it from someone else (plagiarism comes to mind). If you had written it then you would know how to finish the problem.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

its done like this:

class Camera
{
public:
    Camera(Array2D& a): worldRef(a) {} 
    ~Camera() {}
private:
    Array2D& worldRef;
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You are almost done! :) All you have to do now is calculate the total points earned by the team and keep track of who earned to most points.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can not typecase char* to wchar_t* because wchar_t is a different data type, under MS-Windows its a short, but under *nix its a long integer.

For string literals there is a macro in tchar.h address.streetAddress = _TEXT("Albareda 23");

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Swap the entire structure, not just the time

struct players temp;
int a, b;

for (a=0; a < 32;a++)
{                

    for (b=0; b<32-a-1; b++)
        if (world[b].best_time < world[b+1].best_time)
           {
           temp = world[b];
           world[b] = world[b+1];
           world[b+1] = temp;
           }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>does anyone know how i could do this
No, not I. But you could try this.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Have you studied your text book? Have you paid attention in class (or even attended them) ? That assignment is not for beginners, but more likely mid-term or later. If you do not know what a pointer is then you will not be able to complete the assignment. Same with structures.

If pointers confuse you (which it does most people) here is a good tutorial.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't have a "bright" future -- I have a "brilliant" future:) and I only see it once. Maybe you have a problem with your browser. I am using FF on Vista Home. IE8 also does not have that problem for me.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

seems to have been fixed because I don't see that behavior. I see that you are only in the rough just once. So you must have gotten out of the rough a couple times :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

why not just pass the linked list by reference

Link<std::string> filled;
//fill filled with data

TransferLink(filled);

//other file
void  Link<T>::TransferLink(Link<T>& start)
{
   // fill in the linked list here
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't think you will be able to do that. AFAIK the referrals are part of the signup process, which is not public information.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Do you have PMs turned off in your control panel?

I don't know if there is a way to get them again or not.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You get PMs from Happygeek. Although honestly I have never heard of those people who mentioned me as a referral, at least I don't recognize their user names.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I never knew music files could generate random numbers :icon_eek: