WolfPack 491 Posting Virtuoso Team Colleague

change this

FILE *inp, *outp;
    inp = fopen("E:reservat.txt", "r");
    if(inp == NULL)
    {
        printf("Cannot open the file ");
        exit(1);
    }
    for(i = 0; i < MAX; i++)
        reserved[i] = 0;
    n = 0;
    
    for
    (
        int status = fscanf(inp, "%s", first[n]);
        status != EOF;
        status = fscanf(inp, "%s", first[n])
    )
    {

to
this

FILE *inp, *outp;
    int status;
    inp = fopen("E:reservat.txt", "r");
    if(inp == NULL)
    {
        printf("Cannot open the file ");
        exit(1);
    }
    for(i = 0; i < MAX; i++)
        reserved[i] = 0;
    n = 0;
    for
    (
        status = fscanf(inp, "%s", first[n]);
        status != EOF;
        status = fscanf(inp, "%s", first[n])
    )
    {

The reason is you can't declare variables everywhere in C like in C++. All the variables in C must be declared at the beginning of a block before any other C statements.

WolfPack 491 Posting Virtuoso Team Colleague

Are you compiling this as C or C++?

WolfPack 491 Posting Virtuoso Team Colleague

That is what I am trying to do. The least you can do is quit whining.

WolfPack 491 Posting Virtuoso Team Colleague

What is your problem?

WolfPack 491 Posting Virtuoso Team Colleague

What are the errors you are getting?

WolfPack 491 Posting Virtuoso Team Colleague

So isnt it obvious? If the return type of a function is void , that means it is not returning anything. If you want to return consolestream , make the return type of the function the same as the data type of controlstream .

Change void GetFromET() to char* GetFromET() . These are the C/C++ basics. What are you doing writing Windows applications without getting the basics right?

WolfPack 491 Posting Virtuoso Team Colleague

I guess the functions should look like this. I see that you are using global variables. If you are trying to use a dll, avoid using global variables. Since you can't be certain how many processes access these variables, you can't be sure when these variables change value, and will give you unpredictable results.
So design your functions so that they will be using no global variables.
dll.h

#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

DLLIMPORT void SendToET(char *String);
DLLIMPORT void GetFromET();

#endif /* _DLL_H_ */

dll.cpp

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>

void SendToET(char *String)
{ 
   SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)String); 
   SendMessage(hwndEdit, WM_CHAR, CR, 0); 
}
void GetFromET()
 { 
   SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)sizeof(consolestream), (LPARAM)consolestream); 
}
WolfPack 491 Posting Virtuoso Team Colleague

There should be a way of creating a template DLL in Bloodshed DevCP. Have you tried it?
A DLL is nothing but a collection of functions. It does not have a main function.
I dont have any DevC++ specific tutorials but I think you can get an idea by this link. Create an empty DLL template in DevCPP, and try to compile and use these simple samples. You should be able to get your DLL working.

WolfPack 491 Posting Virtuoso Team Colleague

Does turbo c++ even support c++ style strings? My guess is you'd have to use char arrays or switch to something better than turbo c.

Who knows? I have never used anything other than the Microsoft Compiler. Don't people know that you can get much better compilers free? I usually don't answer turbo c threads, and you can see why. :-|

WolfPack 491 Posting Virtuoso Team Colleague

What do you mean by unneccesary? You can use the std::string library.

WolfPack 491 Posting Virtuoso Team Colleague
WolfPack 491 Posting Virtuoso Team Colleague

Mr. Wolf any suggestions to my working code, can it be made better.

int isPrime (int number)
{
    if (number == 2)
        return 1;

    if (number % 2 == 0)
        return 0;

    for (int i = 3; i < number / 2; i += 2)
    {
        if ((number % i) == 0)
            return 0;
    }
    return 1;
}

Wan't there a suggestion in a previous reply that searching only upto squareroot of n is enough? replace n/2 with sqrt(n) and it should be much better. There is more information here.

WolfPack 491 Posting Virtuoso Team Colleague

I meant why do you care about thespeed when calculating the first 500. It took me <1 second. :P

So are you writing two programs?
One that is slow to calculate the first 500
The second that is fast to calculate the rest.

The main objective of the program is to be accurate. After that you should do everything to make it fast as possible under the given restraints. Some restraints are
the clarity of a program
easy maintainability
the extra cost of implementing a more efficient algorithm

Anyway because of time restraints on our part, we can't sometimes make time to provide efficient code in the forum. But if someone else does provide accurate fast code, it is always superior to accurate slow code.

WolfPack 491 Posting Virtuoso Team Colleague

Why me?

WolfPack 491 Posting Virtuoso Team Colleague

I don't think there is a reason for GCC to give an error because of the missing const . The assignment operator of Class C can have any parameter of C, C&, const C&, volatile C&, or const volatile C&. .

It ought to be something else. But I dont know what. Maybe GCC is guarding against self-assignment (which is possible if const is not used) while VC++ is not.

WolfPack 491 Posting Virtuoso Team Colleague

You probably wouldn't need the temporary variable if you use something like this.

iifstream    file(filename);
string      line;
if (file)
{
    string token;
    stringstream iss;
    int i;
    while ( getline(file, line) )
    {
        iss << line;
        i = 1;
        while ( getline(iss, token, ',') )
        {
            cout << token << endl;
            i++;
        }
        iss.clear();
    }
}

edit:
Dragon has posted a more or less equal version before me. I considered deleting this but decided otherwise because of a subtle difference. That difference is that the stringstream variable is created before the while ( getline(file, line) ) block. I only wanted to refraining from calling the constructor over and over again. But the additional overhead (if any )due to the call of iss << line; and iss.clear(); should be inspected.

WolfPack 491 Posting Virtuoso Team Colleague

try

ifstream file(filename);
if (file)
{
    while ( file.getline(line,SIZE) )
    {
        tmp =static_cast<string>(line);
        vector <string> array;
        string token;
        istringstream iss(tmp);
        int i=1;
        while ( getline(iss, token, ',') )
        {
            array.push_back(token);
            v_List.SetItemText(nItem, i, token.c_str());
            i++;
        }
    }
}
WolfPack 491 Posting Virtuoso Team Colleague

God, that has got to be one of the most hilarious jokes I have heard in a long time. LOL

Heh. The fact that you Indian must have helped. :mrgreen:

WolfPack 491 Posting Virtuoso Team Colleague

Try this line

v_List.SetItemText(nItem, i, token.c_str());

This is not a cast, but you will be able to access the C style characters in token.

WolfPack 491 Posting Virtuoso Team Colleague

An Indian was sitting with a Pakistani and an American in Saudi Arabia,
sharing a smuggled barrel of beer, when all of a sudden, Saudi police
entered and arrested them. They were initially sentenced to death but
they contested this and were finally imprisoned for life.

But, as it was their National Holiday, the Sheikh decided they should be
released after receiving 20 lashes of the whip. As they were preparing
for their punishment, the Sheikh suddenly said: "It's my first wife's
birthday today, and she asked me to allow each of you one wish before
your whipping."


So the American guy thought for a while and then said: "Please tie a
thick pillow to my back."
This was done but the pillow only lasted 10 lashes before the whip went
through.

The Pakistani guy, watching the scene, said: "Please fix two pillows on
my back".
But even two pillows could only take 10 lashes before the whip went
through again.

Before the Indian fellow could say something, the Sheikh turned to him
and said: "As you are from a very poor and small country, and your
cricket team is terrible
:cheesy:, you can have two wishes!".

"Thank you, Most Royal and Merciful Highness", the Indian replies.
"My first wish is: " I would like to have 40 lashes."

"If you so desire", the Sheikh replies with …

WolfPack 491 Posting Virtuoso Team Colleague

I knew Dani was upto something when she offered me this Mod ship.

WolfPack 491 Posting Virtuoso Team Colleague

See! Flaming should be compulsory, not against the rules.

Hear Hear.

WolfPack 491 Posting Virtuoso Team Colleague

Seen this issue for sometime. When I initially open the C/C++ page from my bookmark, the online indicator does not show it's green color. After refreshing the page or viewing a thread or do something with the webpage, the online indication button turns green.

Image Attached

WolfPack 491 Posting Virtuoso Team Colleague

I still get them. So I guess it is you. :cheesy:

WolfPack 491 Posting Virtuoso Team Colleague

Don't use fake mathematics. For starters, N^2/2 is nowhere near N^2. And even if you wrote N^2/2, the distance away as N appoaches infinity would be infinity also.


[tex]\frac{N^2}2 + \frac{N}2 \leq k N^2[/tex], for all [tex]N \geq N_0[/tex],
where in this particular case, [tex]N_0 = 5[/tex] and [tex]k = 1[/tex] are satisfying values (but of course others work).

That is why the function is [tex]O(N^2)[/tex].

Your intuition is correct, of course; it would be correct to say that
[tex]\lim_{N\to\infty}\frac{\frac{N^2}2 + \frac{N}2}{N^2} = \frac12[/tex]. And that is also sufficient to prove big-O-ness.

I know that [tex]N^2[/tex] is nowhere equal to [tex]{N^2} \over 2[/tex]
But I thought since both[tex] N^2[/tex] and [tex]{N^2} \over 2[/tex]is going to infinity what the hell. Infact I thought there was a theorem to that effect.:cheesy: Anyway no excuses. It has been 10 years since my last calculus lecture. Don't feel like brushing up either. You should be correct, since you are more in touch than I am.

WolfPack 491 Posting Virtuoso Team Colleague

lookup for the keyword goto But using goto for looping is very very bad practice. For looping you can use the for , while , do-while keywords. It is much easier to understand and easier to maintain than goto .

WolfPack 491 Posting Virtuoso Team Colleague

Okay I had the initial doubt as Dave, but we will give you the benefit of the doubt. If you want the user to login for the program to start, look for Windows Startup Programs. If you want only the computer to be switched on, look for Windows Services.

WolfPack 491 Posting Virtuoso Team Colleague

Should someone login for the program to run (example anti-virus program)? Or "is the computer running?" the only criteria (example Telnet Server)?

Can you explain the type of program you are trying to create?

WolfPack 491 Posting Virtuoso Team Colleague

Hi. welcome to Daniweb.

As a computer fan,

You must be really close to the CPU.:cheesy:

WolfPack 491 Posting Virtuoso Team Colleague

[tex]1+2+3+...+N[/tex]

= [tex]N*(N+1)\over 2[/tex] Geometrical Series Addition

= [tex] {N^2 \over 2} + {N \over 2}[/tex]

[tex]\lim_{N\to\infty}{N^2 \over 2} + {N \over 2} = N^2[/tex]

So it is [tex]O(N^2)[/tex]

WolfPack 491 Posting Virtuoso Team Colleague

Can you tell me where exactly compiler places that string?(which segment of memory)

This depends on the compiler, so you shouldn't worry about it. The standard has made this part implementation dependent. Infact it hasn't even said whether the same string literal used in two different places are equal or not.

For example

char* p = "string";
char* q = "string";

The above two string objects "string" can be stored in two different places or in the same location to save memory. The Microsoft Compiler enables both.

Microsoft Specific
In some cases, identical string literals can be "pooled" to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. /GF enables string pooling.
END Microsoft Specific

Reference.

WolfPack 491 Posting Virtuoso Team Colleague

No need to appologise. We know you are a good fellow.

The problem I am working on is basically to do the merge of two lists without using the std library.

If you are already using the standard list, what is keeping you from using the merge function?

I have looked every where and I cannot find the code for the stl merge (). Is there a place I can find such things.

Well this code is old, but you can take a look. The copyright says that it is free as long as you do not delete the disclaimer. If anybody sees any legal issues in posting this link here, notify me and I will delete it.

I think this is analogous to finding the code behind operators and keywords. What are your thoughts? Can you help - I think I am being asked to re-invent the wheel.

Unless you are writing this code for academic purposes, I'd advice you to use the reliable standard library.

WolfPack 491 Posting Virtuoso Team Colleague

Who the hell calls himself Mr? welcome anyway.

WolfPack 491 Posting Virtuoso Team Colleague
WolfPack 491 Posting Virtuoso Team Colleague
WolfPack 491 Posting Virtuoso Team Colleague

Why, do u like it that much ;)

Because in these forums you get people who say everything is the fault of everybody else but themselves.

WolfPack 491 Posting Virtuoso Team Colleague

Just out of curiosity, who wrote this code?

int main () 
{ 
    char board[NUM_ROWS][NUM_COLS];   // Message board 
 
    // Initialize the message board to all periods. 
    fillRectangle(board,0,0,NUM_COLS,NUM_ROWS,'.');
WolfPack 491 Posting Virtuoso Team Colleague

Gia

WolfPack 491 Posting Virtuoso Team Colleague

That link does not work, but this does. Also Check the top two links in this google search.

@wingwarp: By the way, this thread is begining to get on my nerves! So the next time, you say this and that does not work, just make sure that it is not "File or Directory not Found". Because that is due to you not having correct settings, and we have given all the necessary help to rectify that. If you get that error again, you reinstall devCpp, reinstall OpenGL or reinstall the Operating System and start again, I don't care. But if I see the same thing being repeating again and again in this or any other thread, I close it.

WolfPack 491 Posting Virtuoso Team Colleague

Haven't you even tried writing the merge function? You may have written the other parts, but that is not relevent here. Merging two lists can't be that hard. It is just a matter of connecting the tail of one list, with the head of the other list.

Please remember that when you ask a question, you must try to post only the parts that are needed for us to answer that particular question. Posting all code and expecting us to weed through that is outright rude.

Since you are using the standard list, you can use the splice method.

WolfPack 491 Posting Virtuoso Team Colleague

Ok, I think I am beginning to understand, finally...let me get this straight...

When I write string s3; , the default constrcutor is called and so s3 is empty.

Then when s3 = s1 + s2; only assignment occurs, no copy constrcutor involved, that will be bit-by-bit I think. So no memory allocation, s3 will still be empty.

On the other hand, in case I write string s3 = s1 + s2 , copy constructor is called, so no problems there.:mrgreen:

Is that it?:?:

Yes.

One more thing...when an object is passed by value, then copy constructor is called, and also while returning an object, I hope...:idea:

Yes. That is why it is discouraged to pass large objects by value.

WolfPack 491 Posting Virtuoso Team Colleague

where do i copy if im on xp...

To the Windows\System32 Folder.

6. Copy glut*.lib to Program Files\ MS Visual Studio\VC98\lib\.
7. Copy glut.h to Program Files\ MS Visual Studio\VC98\include\gl.

Ifyou are using devCpp, you will have to change the above instructions to

[LEFT]6. Copy glut*.lib to DevCPP4\Lib\
7. Copy glut.h to DevCPP4\Include\

[/LEFT]

WolfPack 491 Posting Virtuoso Team Colleague

then the moral of the story would be to always overload the = operator if you overload the copy constructor.

Yes. I think so too. Both the implicitly declared operator=() and copy constructor does a member-wise copy of each data elements in the class. If we do not like this default behaviour ( like for classes with dynamically maintained members) we have to overload them. If we don't like a member-wise copy behaviour for one method, I don't think we will like the default for the other. So effectively both should be overloaded if used.

I think the reason Wolfpacks code works with the default assignment operator provided by the compiler whereas vicky_devs does not when this:

s3 = s1 + s2;

is tried has more to do with the default version of the assignment operator rather than the code of the overloaded + operator.

Most probably. I created my version by changing vicky's program a little.

Anyway I think the original problem was about
why does

STRING s3 = s1 + s2;

call the copy constructor and
why does NOT

STRING s3;
s3 = s1 + s2 ;

call the copy constructor. I think the reason should be clear by now.

STRING s3 = s1 + s2;

is equivalent to

STRING s3(s1+s2);

where as

STRING s3;
s3 = s1 + s2 ;

is a default construction and assignment. No copy constructors involved here.

I think this link …

WolfPack 491 Posting Virtuoso Team Colleague
WolfPack 491 Posting Virtuoso Team Colleague

Since there is no need to see the contents I used the following main function.

int main( )
{
    STRING s1("String1");
    STRING s2("String2");
    cout << "Creating s3 using copy constructor\n";
    STRING s3 = s1+s2;
    cout << "Created s3 using copy constructor\n";
    return 0;
}

This is the output

*** String constructor called. ***
*** String constructor called. ***
Creating s3 using copy constructor
*** Default Constructor called. *** // This one for res
*** Copy constructor called. *** // Here s3 is initialized with (s1 + s2 )
*** Destuctor called. *** // This one for res
Created s3 using copy constructor
*** Destuctor called. ***
*** Destuctor called. ***
*** Destuctor called. ***

For the seperate one, the main function was this.

int main( )
{
    STRING s1("String1");
    STRING s2("String2");
    cout << "Creating s3 using default constructor\n";
    STRING s3;
    cout << "Created s3 using default constructor\n";
    cout << "Assigning s1 + s2 to  s3\n";
    s3 = s1+s2;
    cout << "Assigned s1 + s2 to  s3\n";
    return 0;
}

The output was

*** String constructor called. ***
*** String constructor called. ***
Creating s3 using default constructor
*** Default Constructor called. ***
Created s3 using default constructor
Assigning s1 + s2 to s3
*** Default Constructor called. *** // For res
*** Copy constructor called. *** // Most probable for a temporary variable created by the compiler. Not sure.
*** Destuctor …

WolfPack 491 Posting Virtuoso Team Colleague

try

char response = 'N';

.

WolfPack 491 Posting Virtuoso Team Colleague

A copy constructor is really needed in this case because we are allocating space dynamically. If you let the default copy constructor do the job, it'll mess up the whole thing when you pass parameters by value.
Here's why :

string a("Hello");
string b = a;

The default constructor provided does a bit-by-bit copy. So both a and b will point to the same location. If one of them is destroyed ( say by a destructor when returning from the function ), the other will point to an invalid location.

Yes. That is true. What I meant was that it is not necessary to define it if the default behaviour is what you expect. But yes, I get the run time error when the explicit copy constructor is commented out. This fact is clearly explained in stroustrup - The C++ Programming Langauge section 10.4.4.1 . I guess I should have explained a bitmore.

WolfPack 491 Posting Virtuoso Team Colleague

I just don't get it...its clearly mentioned in many books that if a copy constructor is explicitly defined it'll be called. In this program we have done that...and it creates a new object...then why this problem??

One more thing, the program I've originally written works if we make this modification:

STRING s3 = s1 + s2;  // STRING s3; s3 = s1 + s2

Any ideas, any body...??:confused:

The problem here is you are confusing the copy constructor and assignment.

STRING s3;

You are creating s3 in this line. This is default construction. So the default constructor will be called.

s3 = s1 + s2;

You are assigning the result of s1 + s2 to s3. There is no objects created here. There is only assignment. So no constructors will be called here.

STRING s3 = s1 + s2;

In this line, you are using the copy construction. The result will be a copy of (s1 + s2). So here the copy constructor will be called.

Does this agree with your observations?

WolfPack 491 Posting Virtuoso Team Colleague

I guess you can get the char* + string behaviour if you implement the following friend function.

friend STRING operator+( const char* s, const STRING& s1 );
STRING operator+( const char* s, const STRING& s1 )
{
    char*  tempstr;
    int len = s1.len + strlen(s);
    tempstr = new char[len+1];
    strcpy( tempstr,  s);
    strcat( tempstr, s1.str );
    return STRING(tempstr);
}
WolfPack 491 Posting Virtuoso Team Colleague

So how would you ammend your code to allow for such cases:-

STRING b("there");
STRING a = "hello" + b;
int main( )
{
    STRING s1;
    s1 = "hello";
    cout << s1;
    cin.get();
    return 0;
}
STRING& STRING::operator+( const STRING &s )
{
    len = len + s.len;
    char* temp = new char[len+1];
    strcpy( temp, str );
    strcat( temp, s.str );
    if (str )
        delete []str;
    str = temp;
    return (*this);
}

STRING& STRING::operator+( const char* s )
{
    len = len + strlen(s);
    char* temp = new char[len+1];
    strcpy( temp, str );
    strcat( temp, s );
    if (str )
        delete []str;
    str = temp;
    return (*this);
}

STRING& STRING::operator=( const STRING &s )
{
    len = s.len;
    char* temp = new char[len+1];
    strcpy( temp, s.str );
    if (str )
        delete []str;
    str = temp;
    return (*this);
}

STRING& STRING::operator=( const char* s )
{
    len = strlen(s);
    char* temp = new char[len+1];
    strcpy( temp, s );
    if (str )
        delete []str;
    str = temp;
    return (*this);
}

The above code should allow you to do s + "hello" and the such. But as I said not the "hello" + s; part. I will have to lookup further.