mitrmkar 1,056 Posting Virtuoso

OK, then try to figure out how to pass the switches to the compiler. I don't have Dev-C++, but generally these options are found via something like;
Project / Settings / Compiler / Compiler Options. Can you find anything similar in Dev-C++?

Here is a link to the GCC Options to Request or Suppress Warnings
As you can see, there are quite a lot of them, the link is to gcc v.3.3.6 online docs (that might be close to the version that you have). At the very minimum, you want to pass the -Wall option to the compiler (enables all warnings).

[EDIT]
You can find out your compiler version by issueing gcc --version in a command prompt.

mitrmkar 1,056 Posting Virtuoso

I assume you have GCC as your compiler? (Dev-C++ is only an IDE).
What compiler switches are you passing to GCC i.e. what is the compiler command line?

mitrmkar 1,056 Posting Virtuoso

What compiler are you using?

mitrmkar 1,056 Posting Virtuoso

What is this ACM 10082 problem exactly. You might provide a link to it in case anyone is interested in having a look at it.

mitrmkar 1,056 Posting Virtuoso

The variable is almost certainly initalized. I'm guessing that maybe I'm not getting the right variable.

Shouldn't you be able to locate ALL uninitialized variables and fix the code? (just by going through the code, that is).

>> So my question is: when I have an unitialized boolean variable
>> what can be the value of it?
Depending on your compiler, the value may be initialized to a certain value, aiming to be helpful in detecting uninitialized variables. And depending on the compiler the value may differ between release/debug builds.

>> I heard they are stored in one byte of memory. sizeof(bool) will tell you the size on any platform.

>> How are they stored and how are they read?
>> How does the compiler interprete a sequence of bits when it supposes it's a boolean thing?
The question goes so deep that I'd suggest you to have your compiler generate assembler output and check what that tells you :)

mitrmkar 1,056 Posting Virtuoso

I was asking how to rewrite this code in C++.

I see, since that specific line was included in the code you were having problems with, I thought that it was there as part of the program (unnecessarily so). But anyhow, I think you are after the following construct

ifstream ifs("some file here");
// redirect ...
streambuf * cin_rdbuf = cin.rdbuf(ifs.rdbuf());

// do something with cin ...

// then restore cin
cin.rdbuf(cin_rdbuf);

As to the loop problem, either use your own (workaround) solution or accept the commonly used one, choice is yours. Since you did not understand WaltP's explanation, maybe step through the original code with your debugger and keep an eye on cin 's state (hint: cin.eof() )

mitrmkar 1,056 Posting Virtuoso

How is it possible that my program asked to cout a bool variable writes 104 on the display? Even if the variable wasn't initialized, it should display 0 or 1 I thought?

Well, you thought wrong there. Don't assume anything about uninitialized variables and their possible values at any given time. Instead initialize them to a known value/state as soon as possible.

mitrmkar 1,056 Posting Virtuoso

>> Why is it creating the empty file?
I'd suggest debugging the program and try to see where it fails and why.

You could use
_stat() to check both the file's existence and its size in one go.

mitrmkar 1,056 Posting Virtuoso

and this I am writing well yes?

You do have some kind of MySQL query tool (with a GUI), right?? If you don't, then get one. Use that tool to exercise your queries when you are uncertain about a given query's syntax (for example)*.

mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25),password INT)");

it should create 2 columns name and password but it doesn't do anything

Oh well, there might be many reasons for that. My only advice now is that you do:

// run the query
mysql_query(conn, "CREATE TABLE writers(name 
VARCHAR(25),password INT)");
// and immediately after that...
MessageBox(NULL, mysql_error(conn), "Some information", MB_ICONINFORMATION);

*I just noticed that Daniweb has a forum dedicated to MySQL, check it out -> MySQL Forum. Probably a good place to post questions strictly about MySQL (including SQL syntax and such).

mitrmkar 1,056 Posting Virtuoso

But it is printing the last line twice.

while(cin)
    {
        getline(cin, str);
        s_v.push_back(str);
    }

The condition you have in the while() is not working. Instead you have to ..

while(getline(cin, str))
{
    s_v.push_back(str);
}

>> also, i wanna get ride of this line freopen("in.txt", "r", stdin) ;

I think you can just delete that line, your program will work without it. I.e. a command such as

yourprog.exe < input.txt

will work.

mitrmkar 1,056 Posting Virtuoso

>> I've looked around in my code, and I can't find any variables that have been assigned that address(0xccccccd0).

In MS VS debug builds, if you don't explicitly initialize a pointer that is on the stack, it gets initialized with the value 0xcccccccc. So make sure you don't use uninitialized pointers and watch out for pointers having the value 0xcccccccc rather than 0xccccccd0.

0xccccccd0 is rather a side-effect of the root cause (access through uninitialized pointer) i.e. most likely you won't be finding pointers with that value.

Salem commented: Nice +19
mitrmkar 1,056 Posting Virtuoso

tried to neaten up my code to make it easier to follow.

Most modern IDEs come with a decent auto-formatting/beautifier feature, meaning that you'll get a decent formatting at a click of a button or so. Maybe you have such a feature but don't know about it?

You might want to increase your compiler's warning level to the maximum, in other words, just let the compiler spot the mistakes. If you are using GCC, then compile the code with the -Wall option set (=enable all warnings).

mitrmkar 1,056 Posting Virtuoso

Maybe study the sprintf() reference.

Nick Evan commented: What's this word "study" ? ;) +12
mitrmkar 1,056 Posting Virtuoso
sprintf(query, "INSERT INTO writers(name,password) VALUES('%s','%i')", t);

sprintf() expects there two arguments but you are passing only one ( t ). Could that be the reason?

[EDIT]
BTW, use the code tags when you post code.

mitrmkar 1,056 Posting Virtuoso

I tried to call memcpy twice but it stored the 1st copy of 4 seconds correctly but not the other copy. I was assuming that once data is copied in the buffer, its memory address automatically points to the next memory block

The pointers don't automatically 'adjust' themselves, no matter what you do. You simply have to specify the memory location where the write is to take place. So, in the second memcpy() , adjust the destination pointer so that it points to the first byte past the first block you've written.

mitrmkar 1,056 Posting Virtuoso

i am facing problems of destructor in the following code.destructor is not working

The memory allocation is one byte off, you need to allocate an additional byte for the null terminator placed there by strcpy() , so

len = strlen(str) + 1;

FYI, sizeof(char) is always one (1), so you can drop sizeof(char) altogether.

In the constructor, len is rather 1 than 0, though this does not affect the program as of now.

Then last but not least, it's a good practice to check whether the memory re/allocation succeeds. If they don't, the program crashes.

[EDIT]
Since this is C++, maybe rewrite the program using new/delete or std::string .

mitrmkar 1,056 Posting Virtuoso

so is this method convenient for long time pauses like 2-3 days?
because tstart needs to be large number?

In terms of being sufficient, the time_t data type is OK. Check out how many bits your time_t actually is, (probably 64).

Then again, this is the same construct that you already tried and you stated; "man this works BUT IT USES 100 CPU", so will it be convenient to use ~100% CPU for a couple of days?

mitrmkar 1,056 Posting Virtuoso

>> Environment variables would probably work too but I cannot edit them here (My Computer-> Properties->Advanced)

Mmm, what I was sort of saying was that modify the necessary batch files (or copies of the originals actually), which re-configure all these related settings i.e. no need to modify computer-wide settings whatsoever. Well, anyway a good thing that you got it to work.

mitrmkar 1,056 Posting Virtuoso

I tried to move the entire visio contents:
/VC/bin/
to the same folder

I suppose you mean Visual Studio instead of 'visio', right?

Anyway, consider that upon install, the VS tools do modify the system by modifying environment variables (and being MS tools, maybe system registry etc.) in order to locate things at run-time. So, moving the tools might not be a good idea in the first place.

Then again, Visual Studio comes with a batch file vcvarsall.bat, which configures the environment (via other .bats) so that the binaries find what's needed. So maybe locate this vcvarsall.bat, which is usually in the VS root install dir, and see what you can do in terms of re-configuring the paths and such. It may even work.

mitrmkar 1,056 Posting Virtuoso

Here is what I have so far. Suggestions??

Compile the code and start working through the errors/warnings from top to down. Once you think you have solved/fixed a specific error, re-compile and see the results, i.e. don't try to fix all errors at once.

Ancient Dragon commented: Good suggestions. +26
mitrmkar 1,056 Posting Virtuoso

I'd suggest that you post this question on Microsoft's dedicated
Visual C++ General forum. They probably know the answer to this one more or less off-the-shelf.

mitrmkar 1,056 Posting Virtuoso
class coord
{
private:
	int xx;
	int yy;
public:
	int getxx(void);  
	int getyy(void);

Hmm, xx and yy are private member variables, whereas getxx() and getyy() are not. So you have to use these public getters that are there, so you ...

if(asteroid1.getxx( ) == asteroid2.getxx( ) && asteroid1.getyy( ) == asteroid2.getyy( ))
// ..

:)

Suicidal_tool commented: Fantastic Help..Thank you! +1
mitrmkar 1,056 Posting Virtuoso

Are these getxx and getyy member variables or perhaps functions? If the latter, then you need

if(asteroid1.getxx() == asteroid2.getxx() && asteroid1.getyy() == asteroid2.getyy())
	{
		cout<<endl;
		cout<<"Collision"<<endl;
	}

>> if i was to move this and just make a comparason function, could i send the object and its elements to another cpp?
*Clarify* passing the co-ordinates of both objects to a function, in the cpp with all my class details.

Most probably yes, as far as I understood the question.

mitrmkar 1,056 Posting Virtuoso

In Visual Studio, open

Project properties / Configuration properties / Linker / Input / Additional Dependencies

and add Advapi32.lib there.

mitrmkar 1,056 Posting Virtuoso

It's part of the windows.h library I think?

Yes it is, but you need to be sure that you link with Advapi32.lib.

mitrmkar 1,056 Posting Virtuoso

Use the extern keyword to introduce the variables inside the header (.h) files. Define these variables in their counterpart .cpp files.

For example, your header could be

#ifndef OBJECT_H_DEFINED
#define OBJECT_H_DEFINED

// file: object.h

// std::vector is needed in this header file
#include <vector>

// Introduce what is exposed by the 'var' namespace 
namespace var
{
    // You cannot have variables defined in a header file, which
    // is included by multiple .cpp files. So they need to be extern ...
    extern std::vector<game_object*> _objects_;
    extern std::vector<SDL_Surface*> _img_;
    extern std::vector<TTF_Font*> _font_;
    extern bool quit;
    extern bool pause;
    extern int sound_volume;
    // etc ...

    // An ordinary function declaration, no need for extern
    void ordinary_function();

    // A class declaration
    class some_class
    {
        // ctor
        some_class();
    };
}
#endif // ndef OBJECT_H_DEFINED

Then the .cpp file would be

// file: object.cpp

#include "object.h"
namespace var
{
    std::vector<game_object*> _objects_;
    std::vector<SDL_Surface*> _img_;
    std::vector<TTF_Font*> _font_;

    bool quit = 0;
    bool pause = 0;
    int sound_volume = 100;

    void ordinary_function()
    {
         // something here
    }

    // class definition ...
    some_class::some_class()
    {}
}

[EDIT]
The above is to demonstrate the basic idea of putting these things together, so given that there are e.g. SDL_Surface * and such, you also need the related headers for those included in proper places. But I hope that you get the basic idea though.

mitrmkar 1,056 Posting Virtuoso

>> I want to use gcount but I cant figure out the correct formatting.
Since you are having two string s there, you can get a string's length using e.g. .length() I.e.

string test = "abc";
// the following would print 3 
cout << test.length() << endl;

But then again, you don't need actually that, because you can check whether two strings are identical by

string test1 = "abc";
string test2 = "abc";

// a case sensitive comparison ...
if(test1 == test2)
{
    cout << "strings are identical" << endl;
}

// or ...
if(test1 != test2)
{
    cout << "strings are not identical" << endl;
}

Then last but not least, you probably want to handle the case where the files don't contain the same number of lines. As of now, your program would not detect that.

mitrmkar 1,056 Posting Virtuoso

>>the prototype is in the header file...

That is not enough, you cannot leave the functions' return type out of the function definitions.

Then you use the comma operator along with a return statement, that is plain wrong. Though it compiles, only the latter value is returned.

// Decide which one you want to return ...
return HourlyTemperatures[0], HourlyTemperatures[23];
mitrmkar 1,056 Posting Virtuoso

>> Use std::copy. If not then create your own.

Additionally a simple alternative is

vector<string> src;
  vector<string> dest; 
  
  src.push_back("abc");
  src.push_back("def");
  src.push_back("ghi");

  // make an exact copy of src, resizing dest
  dest = src;
mitrmkar 1,056 Posting Virtuoso

>> I am basically creating everything by hand here (dialogs and all)
Doing all that manually, might also result in increased permanent loss of hair, hence I'd suggest that you check out; ResEdit / free Resource Editor for Win32 programs. It's a quite serious application in it's own right.

mitrmkar 1,056 Posting Virtuoso

But why didn't it work last time it gave me two errors (shame I'll probably never know) :(

It would have been better to have posted the actual error messages (as given by the compiler) along with the code that wasn't compiling. Otherwise people here can only guess what might be wrong.

mitrmkar 1,056 Posting Virtuoso

I just need to know how I might go about loading up a dialog that I have created in my .rc (that is, getting it to pop up when I click a button)

You are doing that very thing already, i.e. using DialogBox(...).
So,
- define a new dialog box resource in the resource files
- write the code for the new dialog procedure
and use it like ...

case IDC_SETSBTN:
/* --> Load Sets Dialog <-- */
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_SETTINGS), 
        hwndDlg, Settings_DialogProc);
mitrmkar 1,056 Posting Virtuoso

You might want to take a look at SetConsoleCtrlHandler()

mitrmkar 1,056 Posting Virtuoso

I was not trying to trash anything.

I didn't mean to say/imply that you were intentionally trying to trash anything just to see what happens, you misunderstood me somewhat there.

>> 1. What is wrong with leaving the size of a2 undefined
You must be in full control of the memory you allocate at all times, i.e. you have to know the size of a given block of memory you've allocated and keep track of it (what do you think was the size of the block allocated for a2??). Otherwise you'll end up writing to memory, which may be reserved/used for something else. In this case it was and produced the nasty side-effects. The pointers don't automatically adjust the size of the memory block pointed to.

>> How could this have affected output streams in an apparently unrelated function?
Again, you simply trashed the memory ( pointer2[i] = pointer1[i] )

>> 2.And what is the proper way of copying structs?
One way is to allocate enough memory and do a memberwise copy. Also you might be interested in checking out overloading the assignment operator (=), which would be more suitable for non-POD types (Plain Old Data).

mitrmkar 1,056 Posting Virtuoso

The fix is simple,

// a1 comes with 10 elements ->
// a2 also comes with 10 elements (at minimum)
a2=new abc[ 10 ];

So, you were simply trashing memory. What was the main idea of doing the a2 allocation like you did? Were you just trying out C++ 'things'?

mitrmkar 1,056 Posting Virtuoso

Why are you doing a thing like listSongs.push_back(NULL); there? Are you sure that you are not doing e.g. a listSongs.at( [I]index of a NULL pointer[/I] )->getSongName() causing the seg fault?

mitrmkar 1,056 Posting Virtuoso

where can I get a list of these WINAPI macros and their meaning?

Oh c'mon, haven't you tried Windows Data Types??

Depending on which IDE you have, you might be able to quickly locate these things by placing the cursor on a given typedef/define and do a "go to declaration/definition" from the IDE's context menu. Check whether your IDE supports that, however you may find the all the #ifdef/#else blocks in the headers a bit confusing.

mitrmkar 1,056 Posting Virtuoso

>> And were do i put the while loop in my code? i presume here
Sorry, but I don't know what you are trying to achieve with an infinite loop, so it's impossible for me to tell where to put one.

But if you can describe in detail what you are planning to do with this code snippet, then you might get good answers.

mitrmkar 1,056 Posting Virtuoso

how do i make a infinate loop?

For example

for ( ; ; )
{
    // your code here
}

// or ...

while( true )
{
    // your code here
}
mitrmkar 1,056 Posting Virtuoso

here are some of the warnings:
warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

This warning C4996 is pretty self-explanatory i.e. it is saying that you should consider using the suggested function instead, which is supposed to be more secure/robust. But it is up to you which one you use, perhaps lookup fopen_s() and its TCHAR-aware counterpart _tfopen_s().

Furthermore, you can suppress this warning (C4996) by adding the _CRT_SECURE_NO_WARNINGS preprocessor definition in your project's properties.

So, these warnings are only suggestions to use safer alternatives, hence I guess that the run-time crash occurs because there is something seriously wrong with your code.


[EDIT]
The linker's warning LNK4076 is something that you can safely ignore.

mitrmkar 1,056 Posting Virtuoso

tell me which is equivalent function with TCHAR for wcscpy(),for copying strings,and the others I will find in google?

Forget the Google, instead look up the particular function in MSDN, for example wcscpy(). Then, on that page scroll down to where you see "TCHAR.H routine", there you'll find the equivalent _tcs-prefixed routine name (in this case it is: _tcscpy).

mitrmkar 1,056 Posting Virtuoso

MITRMKAR
here is the project:

What I was trying to say in the post #4 above, is that you have to learn to use the MySQL API functions correctly. The API is well documented in C API Function Overview (that's for version 5.5, use the documentation that's relevant to your library).

So when you use the mysql*() functions, be sure to check whether a given API call succeeded or not. If not, try to figure out the reason, the API documentation might prove helpful here. If you don't do that, you'll simply waste your time being at loss with what's wrong and what's not.

In the post #4 above, the mysql_error() is used in an attempt to provide useful error diagnostics message to you, instead of plain " MessageBox(0,"mysql error",0,0); ", which really doesn't help much. But by looking at your code, you've chosen not to use mysql_error() , I suspect that you didn't quite get the idea (?). Anyway, maybe start by trying out that simple thing, study the API and see the difference.

mitrmkar 1,056 Posting Virtuoso

>>what is ULONG equivalent of C++ or it is just ULONG?
It is typedefed via <windows.h>, and is documented in Windows Data Types
That's a good page to lookup the data types and #defines used by Windows.

>>GetLastError()); //So GetLastError retuns a sring or char*?
No, see GetLastError Function. In general, the Windows API functions are documented in MSDN, so you can use MSDN as your primary reference.

>> //what does g_szDrvMsg do and what is TCHAR type?
Debug the while() loop there step by step so you'll understand how the thing works ;)

mitrmkar 1,056 Posting Virtuoso

Read up on the fstream::open reference.

mitrmkar 1,056 Posting Virtuoso

Since the record was not edited by the program why did the price and quanitity change?

In the displayRecord() there is an extra inventory.read(...) at the start of the function. So the position is hence off by one.
And then a little error where the address of operator & has slipped in, a couple of lines later inventory.read(reinterpret_cast<char *>(&record), sizeof( & record)); Then addRecord() needs attention, it will truncate the file. So you need to figure out the proper mode for opening the file.

mitrmkar 1,056 Posting Virtuoso

Could you post your new code, that might clear things up. Also try to point out where things go wrong in the code (as closely as you can).

mitrmkar 1,056 Posting Virtuoso

>> MessageBox(0, errMsg, "query failed", 0);

It would be helpful to know what the error message says.

For the connection specified by mysql, mysql_error() returns a null-terminated string containing the error message for the most recently invoked API function that failed. If a function didn't fail, the return value of mysql_error() may be the previous error or an empty string to indicate no error.

So the error message, if there's one, should be displayed (I think).

mitrmkar 1,056 Posting Virtuoso

Duh, sorry about using printf() . Switch those printf()'s to be along the lines of

char buf[MAX_PATH];
sprintf(buf, "GetWindowText() failed, error is [%lu]", GetLastError());
MessageBox(0, buf, "error", MB_ICONSTOP);
mitrmkar 1,056 Posting Virtuoso

Maybe the following takes you a step further ...

// Note that you have to get the following window handle ('edit')
// somewhere. If you use an uninitialized handle like below,
// then GetWindowText() fails.
HWND edit;
char t[MAX_PATH];
char query[MAX_PATH];

// Try to get the window text
if( ! GetWindowText(edit,t,MAX_PATH))
{
    // What's the error?
    printf("GetWindowText() failed, error is [%lu]\n", GetLastError());
    // No use to continue ...
    return;
}

//// As Ancient Dragon said, don't create the table every time ...
// mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");
// However, you must be sure that it exists, does it exist in the db?

sprintf(query, "SELECT * FROM writers WHERE name = '%s'", t);

// How does the query look like now?
printf("Query is: [%s]\n", query);

//// You were using 'WHERE name = t', use the actual query instead
// result1 = mysql_query(conn, "SELECT * FROM writers WHERE name = t");

// Here 'conn' is used, are you sure that you have a valid
// connection?
result1 = mysql_query(conn, query);

if(result1 == 0)
{
    result = mysql_store_result(conn);

    if (mysql_num_rows(result) == 0)
    {
        MessageBox(0,"not find",0,0);
    }
    else 
    {
        MessageBox(0,"find",0,0);
    }
}
else 
{
    // Query failed, see what mysql says, use mysql_error()
    const char * errMsg = mysql_error(conn);
	
    // Check that non-null/non-zero-length string wasn't received
    if(errMsg && *errMsg)
    {
        // Some error msg received, display it
        MessageBox(0, errMsg, "query failed", 0);
    }
    else
    {
        // No luck with the mysql error msg ...
        MessageBox(0,"mysql query failed","error",0);
    }
}
mitrmkar 1,056 Posting Virtuoso

See WM_SYSKEYDOWN and WM_SYSKEYUP.