mitrmkar 1,056 Posting Virtuoso

Your duplicate() methods are consistently wrong. Instead of passing an address of an object on stack like you do, allocate the object from the heap using new and return the pointer to it.

For example ..

shape *duplicate()
{
  return new square(length);
}

And once you've done with the duplicated objects, remember to delete them.

the main area of concern is line 152:

shapelib.push_back(new prism(d,*shapelib[n-1]));

Note that n - 1 must not result in a negative value.

mitrmkar 1,056 Posting Virtuoso

You don't want to have the vector ( mydouble ) declared inside the for-loop, because that gives you a fresh re-constructed vector on every iteration of the loop. So declare the vector in another scope, outside the loop.

mitrmkar 1,056 Posting Virtuoso

If you'd be allowed to do that, it would make const quite meaningless, since you'd get a non-const iterator, through which you could modify the (const) vector.

mitrmkar 1,056 Posting Virtuoso

You cannot a pass an iterator to at(). Read up about its parameter.

mitrmkar 1,056 Posting Virtuoso

what the heck is going on?

cout << "Your score is: " + finalPoints_

You want to change the '+' there to something else.

Salem commented: Nice catch! +20
mitrmkar 1,056 Posting Virtuoso

the output program at the end is blank.

The input file is not opened? You might use .is_open() to check that.

mitrmkar 1,056 Posting Virtuoso

SOLVED IT!:

added fflush(stdin);

Oh no, perhaps reconsider and find another solution to the problem, read e.g. Things to Avoid in C/C++ -- fflush(stdin)

mitrmkar 1,056 Posting Virtuoso

In the derived constructors, you want to to pass the pID to the base class.

mitrmkar 1,056 Posting Virtuoso

I think the trouble is due to const vector<double> &GetData() .

mitrmkar 1,056 Posting Virtuoso

Introduce a new scope {} inside the case, like so ..

switch (service) {
case 'D' :
  { // new scope begins ..
   ... your code here as is ...
  } // .. and ends
case ...
mitrmkar 1,056 Posting Virtuoso

By leaving the nullified pointers in the vectors, the second time around you iterate over the vectors, a crash is more than likely to happen (assuming these indices remain NULL pointers). Also you may want to check what the destructors are doing.

[EDIT]
Oh, and please read What are code tags

mitrmkar 1,056 Posting Virtuoso

The print() method in the Book.h is lacking a '}'.

mitrmkar 1,056 Posting Virtuoso

Line #115, should it rather read

case 3: ofBB.push_back(rowb[jb]);break;
mitrmkar 1,056 Posting Virtuoso

I don't know much about SDL programs' behaviour and possibilities, but I noticed that you do a exit(0); there (line #50). In practice, no code that you have below that line will get executed, see exit().

Perhaps you should find a SDL forum for SDL-specific questions.

mitrmkar 1,056 Posting Virtuoso

You might use a stringstream for that purpose. This has been answered probably a gazillion times, here we go again ..

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::string filename ("data");
  std::string extension (".txt");
  std::stringstream sstrm;

  for(int X=1;X<11;X++)
  {
    sstrm << filename << X << extension;
    std::cout << sstrm.str().c_str() << std::endl;
    // Get rid of the current content ..
    sstrm.str("");    
  }

  return 0;
}
mitrmkar 1,056 Posting Virtuoso

Perhaps you should consult your professor?

mitrmkar 1,056 Posting Virtuoso

OK, after all, it looks like you probably want to flush the (buffered) output, in order for it to show like you are expecting, even under the debugger. In other words, you may want to try e.g.

// Use 'endl' to have a newline inserted + output buffer flushed
cout << "Can you see me?" << endl;
// Use embedded '\n' and then 'flush'
cout << "Can you see me?\n" << flush;
mitrmkar 1,056 Posting Virtuoso

It seems as if there is something I dont understand here with Sleep. If I use that function it will freeeze at the last cin.

So, you are saying that with Sleep() enabled, the program freezes at line #132 (i.e. 'last cin'). So at that point you'll be unable to input anything and likewise you are not seeing any further output either?
How does the program terminate? Do you have to kill it via Task Manager or something alike?

mitrmkar 1,056 Posting Virtuoso

Some more suggestions for you to check: Fatal Error C1004.

jonsca commented: It ended up being something from your link +4
mitrmkar 1,056 Posting Virtuoso

i come up with a additional set of numbers, but i don't know why.

Line #44 needs rewriting

while(inFile)

Change it to ..

while(inFile >> studentName >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
  // rest of the code ...
}

Your while(inFile) is a variant to the theme of using .eof() in a loop control. Read Avoid Loop Control Using eof() courtesy of Dave Sinkula.

mitrmkar 1,056 Posting Virtuoso

The code you've posted will not even compile (snippet #1/line #11). So, maybe post the exact code that you are actually using.

mitrmkar 1,056 Posting Virtuoso

That was why I suggested a Truth Table... :icon_wink:

I know you did, but it just so much seems that the point still needs to be made, so to speak...

mitrmkar 1,056 Posting Virtuoso

Basically, a thing that you need to understand is the fact that

while( ans != 'n' || ans != 'y' )

is effectively the same as writing

while( true )

And there is no way around that. :)

mitrmkar 1,056 Posting Virtuoso

my question is still there that how can i place check on the input
that user can enter only 'y' or 'n'

Consider using logical AND (&&) ..

do
{
  prompt the user ..
  get the input ..
}
while(ans != 'y' && ans != 'n');

or logical OR (||) ..

do
{
  prompt the user ..
  get the input ..
}
while((ans == 'y' || ans == 'n') == false);

Try to think through your original if-statement to understand why it will not work.

mitrmkar 1,056 Posting Virtuoso

You need to use the KEY_ENUMERATE_SUB_KEYS flag when opening the key ..

// ..
if(RegOpenKeyEx(
  HKEY_CURRENT_USER , "Software" , 0 ,
  KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
  &hkey) == ERROR_SUCCESS) {
// ..

Note that a successful RegEnumKeyEx() call will change the value of the size parameter to be the length of the subkey. So, you need to ensure that you pass in the real size of the name buffer upon each call to RegEnumKeyEx() .

mitrmkar 1,056 Posting Virtuoso

Actually, to be more specific, it appears to happen immediately before the free(cur) would be executed...which seems even odder to me.

Likely you are writing to already freed memory, outside this delete_from_list() function. The debug memory management facilities kick in upon the attempt to free() , catching the aforementioned error (-> runtime error message).

A minimal example that you could try out to see how it happens, would be along the lines of..

int main()
{
  /*  Allocate a node and initialize its examNumber to 1 */
  struct node * p1 = alloc_node(1);

  /*  Allocate a node and initialize its examNumber to 2 */
  struct node * p2 = alloc_node(2);

  /*  Make a list .. */
  struct node * list = p1;
  p1->next = p2;
  p2->next = NULL;

  /*  free p1 .. */
  list = delete_from_list(list, 1);

  /*  p1 still is accessible here, pointing to the freed memory, 
   *  now write to that memory ..
   */
  p1->next = NULL;

  /*  Try to free p2 .. */
  list = delete_from_list(list, 2);

  return 0;
}

For little more information on how this error gets caught, you might read Magic debug values.

[EDIT]
Also check that you are not trying to free anything twice/nor writing out-of-bounds.

mitrmkar 1,056 Posting Virtuoso

The code above would be possible if i want to paint all the 200 edit control the same colour right..?

Yes.

>> What if I want to paint them different colour..?
Then you have to arrange so that the colour/control mappings are stored somewhere. Just an idea, a std::map might be handy ..

// A map ..
std::map<int /* control ID */, COLORREF /* the colour */> id_to_color;

// Then in the handler ..
HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  // Should we change the attribute(s)?
  if( < the condition here > )
  {
    // Get the edit control's id ..
    int control_id = pWnd->GetDlgCtrlID();
    
    // Use the current mapped colour ..
    pDC->SetTextColor(id_to_color[control_id]);
  }

  return hbr;
}

Perhaps start by trying out something that simple to get some insight.

>> Is it possible to do the following..?
Don't store anything that is passed into the handler, those are pointers to temporary objects i.e. for a given edit control, the passed-in CDC * will vary/change.

PS. Are you serious about having 200 edit controls? I think the maximum number of any controls on a dialog is 255, so watch out for that limit.

mitrmkar 1,056 Posting Virtuoso

Here's a version with some assert() added. Run it in the debugger and you'll probably be spotting errors quite quickly.

#include <cassert>

void Array::merge (int first, int last)
{
  const int arr_size = last-first+1;

  int* merged = new int[arr_size];
  int mid = (first + last) / 2;
  int secList = mid + 1; // beginning of second list
  int firstList = first; //beginning of first list
  int j = first; // used for array index assignment

  while ( (firstList <= mid) && (secList <= last) )// both lists #1 & #2 are not exhausted
  {
      if (data[firstList] < data[secList])// data in list #1 < data in list #2
      {
          // check j ...
          assert(j < arr_size);
          merged[j] = data[firstList];// <- data in list #1
          firstList++;
      }   
      else
      {
          // check j ...
          assert(j < arr_size);
          merged[j] = data[secList]; // <- data in list #2
          secList++;
      }
      j++;
  } // end while
    
  if (firstList > mid)
  {
      for (int q = secList; q <= last; q++){
          // check j ...
          assert(j < arr_size);
          merged[j] = data[q];
          j++;
      }
  }
  else
  {
      for (int r = firstList; r <= mid; r++)
      {
        // check j ...
        assert(j < arr_size);
        merged[j] = data[r];
        j++;
      }
  }

  for (int i = first; i <= last; i++)
  {
      data[i] = merged[i];// overwrite original positions with "merged"
  }

   delete [ ] merged; //commented out due to segmentation fault
   merged = NULL;
}

I'd suggest you to check all your code for out-of-bound writes.

mitrmkar 1,056 Posting Virtuoso

char text[10]

You need to increase the size of that array by one, if you intend to copy a literal such as "0x00000000" into it (you must account for the terminating '\0' character). Otherwise, you'll be writing out of bounds, which is not allowed.

PS. Maybe consider using std::string s instead of char arrays.

jonsca commented: Good catch +3
mitrmkar 1,056 Posting Virtuoso

If I use OnCtrlColour handler, when is the function called..? Is it everytime the the dialog is reload/refresh or is it continuously called independent of what I do..?

MSDN says;
"The framework calls this member function when a child control is about to be drawn." In practice, this means that it gets called very frequently.

>> How do i pass the required parameters (CDC* pDC, CWnd* pWnd, UINT nCtlColor) to the function..?
You don't need to do anything about that, MFC handles the parameters for you automatically.

The nCtlColor parameters specifies what kind of control needs to painted, so if you want to paint edit controls only, then ..

HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  // Are we painting an edit control?
  if(nCtlColor == CTLCOLOR_EDIT)
  {
    // Yes ...
    // Set the text color to red
    pDC->SetTextColor(RGB(255, 0, 0));
    ...
  }

  return hbr;
}
mitrmkar 1,056 Posting Virtuoso

where this is coming from?

The runtime code calls abort() (or perhaps even your own code?).

Which compiler/IDE/OS you are using?

mitrmkar 1,056 Posting Virtuoso

-the new project is now running so I choose File, New, File and then choose C++File in the menu, then ok

You still need to add the new file to the project, do that via Project / Add Existing Item and select your new file. Or alternatively you may drag/drop the file onto the Solution Explorer.

In general, you probably want to add new files/classes via the Project menu.

mitrmkar 1,056 Posting Virtuoso

I think this is what you were after, note that you need an instance of the class through which you make the call ( pB = new B; below)

#include <iostream>
struct B;
struct A
{
  B * pB;

  A();
  ~A();

  void func(int i, const char * p) 
  {
    std::cout << i << std::endl << p << std::endl;
  }
};

struct B
{
  void (A::* funcptr)(int, const char *);
};

A::A()
{
  // a B is needed
  pB = new B;
}

A::~A()
{
  delete pB;
}

int main()
{
  A * pA = new A;

  pA->pB->funcptr = &A::func;

  (pA->*(pA->pB->funcptr))(2, "Lol");

  delete pA;

  return 0;
}
mitrmkar 1,056 Posting Virtuoso

I have v. 8.02 too and for me, F9 really toggles a breakpoint (I think I'm using the default settings, not sure though). Anyway, it's probably best to open the Build menu and see what key does the trick.

mitrmkar 1,056 Posting Virtuoso

I hit F9 key of my keyboard, the programme didn't run.

On my Code::Blocks, F9 toggles a breakpoint. But to compile/link and run the program, you probably want to use F12. See the Code::Blocks' Build menu.

mitrmkar 1,056 Posting Virtuoso

>> Well, those were desperate attempts to ..
Try to keep cool and go through the code making sure that you don't mess with the memory anywhere.

As to the original crash with fread() , there seems to be a malloc() , involved. Regarding that, make sure that you are allocating a reasonable memory block ( imageSize > 0 ) and that the malloc() actually succeeds.

mitrmkar 1,056 Posting Virtuoso

First thing to check in all of your code, how you are handling memory allocations/deallocations.

Now you are trying to free() variables that are on stack, that is ..

GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
GLubyte TGAcompare[12];
GLubyte header[6];
...
// Simply disastrous ...
free(TGAheader);
free(TGAcompare);
free(header);

You only free() what you have malloc()/calloc() ed, i.e. dynamic memory, nothing else.

PS. Since this is C++, you might be better of with new/new[] and delete/delete[] , respectively, if there's a need to use dynamic memory.

mitrmkar 1,056 Posting Virtuoso

I receive no warnings/errors during compile (using Dev-Cpp, WinXP)

That is too hard to believe, given that you have there ..

FILE file  = fopen(path, "rb");

So, could you verify that you are actually posting the relevant code?

mitrmkar 1,056 Posting Virtuoso

Actually in C++ you would want to be avoiding malloc() in the first place and using new instead. So ..

// No need to cast anything
 f * node = new f;

[EDIT]
If you (for some reason) use malloc() , you can still use C-style casts to some extent, (i.e. in this case (f*) ) or a C++ cast. But perhaps remember, that casts are best avoided.

mitrmkar 1,056 Posting Virtuoso

I've seen it before :)

mitrmkar 1,056 Posting Virtuoso

Appears as if you are compiling it as C++ code instead of C.

PS. When you post code snippets, try including everything relevant to the given piece of code/errors. I.e. in this case, you most likely are including <stdlib.h>, but that does not show in the snippet (even little things can make a whole lot of difference in terms of the compiler output).

[EDIT]
In case of C++ compiler, you need to typecast the pointer returned by malloc() , in C, you don't have to do that.

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

Is "copy" used by the language in some way that prohibits its use as a variable name in this situation

Yes it is, i.e. the std::copy algorithm. It gets pulled in via using namespace std; . So either change the name of the variable, or use your own namespace.

[EDIT]
PS. One more option would be to stop using the using namespace std; altogether and instead specify explicitly everything you use from the std namespace, so ..

#include <iostream>
using std::cout;
using std::cin;

and so on.

Salem commented: Agree with avoiding the using namespace catch-all. +19
mitrmkar 1,056 Posting Virtuoso

Yeah I was stupid and pressed (X) on all of the bars to the left you know the "Solution Explorer" I think and alot of more, how do I re-enable them?

Open the View menu, and select e.g. Solution Explorer, Class View and such.

mitrmkar 1,056 Posting Virtuoso

No it's not. The system in our education gives the teacher the free will to do whatever he may see as good to go, so I think it was his choice to work with this old resources and not give us the up-to-date programming methods in C++.

Somewhat hard to believe but I take your word for it.

mitrmkar 1,056 Posting Virtuoso

You mean the highlighted line that the program highlights it as an error? Well, it's line no. 13, and I don't think that it needs a semicolon.

Yes you are right, in general depending on the compiler and the error, sometimes the error message points you straight to the line that needs fixing, sometimes not. If not, then there are error(s) preceding the offending line.

The problem that there is, is that the function definition ends with a semicolon and that is plain wrong. Function declarations/prototypes need to end with a semicolon but definitions not. So, just remove the extra semicolon there (in this case line #12, I think).

mitrmkar 1,056 Posting Virtuoso

Yeah the grumpy kind of teacher, you know.

I meant to say, that this problem is in the educational system/resources, so your teacher is probably quite restricted by that.
If he's grumpy, that's another thing you have to live with.

mitrmkar 1,056 Posting Virtuoso

What's an offending line ? :?:

The source code line that gives you trouble.

mitrmkar 1,056 Posting Virtuoso

I have a weird teacher : (

Last time he saw me writing the program in Visual Studio with <iostream> and "using namespace std" instead of <iostream.h> he told me to use Turbo C++ to follow up the leasons just like he gives it to us. The old 93 way I guess.

Well, your teacher is bound to his rules and regulations and other things alike, I would assume.

mitrmkar 1,056 Posting Virtuoso

I think it goes something like this:

#include <iostream.h>
void sum( int *a, int *b, int *c);
int main()
{
int x,y,z;
x=12;
y=8;
sum(&x,&y,&z);
cout<<"sum:"<<z<<endl;
return(0);
}
void sum( int *a, int *b);
{
*a=*a+1;
*b=*b+1;
*c= *a + *b;
}

But I still have the same error.

There is one extraneous semicolon on the offending line ;)

mitrmkar 1,056 Posting Virtuoso

Yet one more thing, if/when you do strcpy(argv2, argv[1]); you want to check that there actually is an argument available, you can use argc for that.