mitrmkar 1,056 Posting Virtuoso

There seems to be an extraneous period there .. cout << setw[B].[/B]((text.length() + lineLength) /2) << text << endl;

mitrmkar 1,056 Posting Virtuoso

There are two glaring errors, namely

delete[] square[i];
// and
free(square);

Simply delete those lines because square is not dynamically allocated -- doing that may fix the program.

Perhaps see Freestore management.

mitrmkar 1,056 Posting Virtuoso

I'm not quite sure what it is that you are exactly asking, but you may get some ideas from the following. Since you already include <windows.h>, you can use the Sleep() Windows API.

#include <stdio.h>
#include <windows.h>

void print_welcome(void)
{
  printf
  (
    "\n\n\n\n"
    "\t\t   ***************************************\n"
    "\t\t   ***************************************\n"
    "\t\t   ***                                 ***\n"
    "\t\t   ***           WELCOME TO            ***\n"
    "\t\t   ***    CLASS ATTENDANCE SYSTEM      ***\n"
    "\t\t   ***                                 ***\n"
    "\t\t   ***************************************\n"
    "\t\t   ***************************************\n"
    "\n\n"
  );
}

/* Note: It is int main(), NOT void main()! */ 
int main(void)
{
  int i;

  for(i=0; i<5; i++)
  {
    system("cls");
    print_welcome();

    /* Wait for a second */
    Sleep(1000);
  }

  return 0;
}
mitrmkar 1,056 Posting Virtuoso

>> I'm getting a "ambiguous call to overloaded function" from the extPersonType myExt

I think you may have been trying to write two default constructors which you cannot do, perhaps see http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.4.

The following is what I suggested you could do in order to make the constructors work properly ..

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

struct A
{
  string a, b;

  // A default constructor
  A(const string & a_ = "a", const string & b_ = "b")
  : a(a_)
  , b(b_)
  {
    cout << "A() a: " << a << "\tb: " << b << endl;
  }

  //// Since a default constructor already exists,
  //// you cannot have the following constructor
  // A() : a(""), a("") {}
};

struct B : A
{
  string c, d;

  // A default constructor
  B(const string & c_ = "c", const string & d_ = "d")
  : A()
  , c(c_)
  , d(d_)
  {
    cout << "B() c: " << c << "\td: " << d << endl;
  }

  //// Since a default constructor already exists,
  //// you cannot have the following constructor
  // B() : c(""), d("") {}
};

int main()
{
  A a;
  B b;
}
mitrmkar 1,056 Posting Virtuoso

You might apply the changes described above to the extPersonType() constructor, and then reduce your main() to the following, just to make sure that the constructors are working as intended.

#include <fstream>
#include <iostream>
#include <string>
#include "personType.h"
#include "addressType.h"
#include "extPersonType.h"
#include "dateType.h"
using namespace std;

int main()
{
  try
  {
    // Try to instantiate an extPersonType
    extPersonType myExt;
  }
  catch(const exception & e)
  {
    cout << "exception: " << e.what() << endl;
  }
  catch(...)
  {
    cout << "Unknown exception." << endl;
  }
}
mitrmkar 1,056 Posting Virtuoso

The problem is in the extPersonType() constructor where you pass the uninitialized member variables firstName and lastName to the base class constructor, i.e. trying to initialize uninitialized variables with copies of themselves.

Since the base class constructor has default parameters, the fix is easy, i.e.

extPersonType::extPersonType(string extRelation, string extPhone)
: personType(/* using the default arguments */)
{
	relation = extRelation;
	phone = extPhone;
}

In general,
-- to avoid making unnecessary copies of function arguments, pass by const reference
-- use initialization lists instead of assignment

So this constructor could turn into ..

extPersonType::extPersonType(const string & extRelation, const string & extPhone)
:	personType(/* using the default arguments */)
, 	relation(extRelation)
, 	phone(extPhone)
{
  // Nothing to do in the body
}
mitrmkar 1,056 Posting Virtuoso

Nowhere are you modifying the two matrices declared in main() -- they remain zero-initialized throughout the program. Remedy this by deleting the local int C[3][3] matrix in the MatrixConverter() and modify the passed-in x matrix instead.

mitrmkar 1,056 Posting Virtuoso

No, he doesnt need to

Yes he does -- it works for you because you include the header in one source file only.

mitrmkar 1,056 Posting Virtuoso

You need to move the definition to a source file (.cpp), e.g.

// file: prereq.cpp
#include "prereq.h"

int UtilFunc::PositionMap[MAP_WIDTH*MAP_HEIGHT] = {0};
mitrmkar 1,056 Posting Virtuoso

>> problem is with error handling.. where in the program should not engage in
an endless loop if for example a a letter instead of a number was entered.

See e.g. http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2

mitrmkar 1,056 Posting Virtuoso

>> After each for each loop, posY should be incremented by 100. So all the boxes should be at the same x position but be 100 pixels(?) apart in the y position.

You missed the point .. incrementing posY has no practical effect in your code w.r.t. positioning the boxes. Try changing to following ..

// Initial values - outside the loop
int posX = 200;
int posY = 200;

for each (DataRow ^row in dataSet->Tables["Contact"]->Rows)
{
	TextBox ^txtBox = gcnew TextBox();
	txtBox->Location = Point(posX, posY);

	posY += 100;
	this->Controls->Add(txtBox);
				
	MessageBox::Show(row->default[0]->ToString());
}
mitrmkar 1,056 Posting Virtuoso

>> My problem is that only one textbox is created on the form. I'm not sure why 3 textboxes aren't being created (because there are 3 rows in the table "Contact").

It looks like the boxes are created on top of each other, i.e. effectively the location of each box is Point(200, 200) .

mitrmkar 1,056 Posting Virtuoso

In addition to what's already stated above, I'd like to suggest a beginner-friendly C++ Tutorial.

mitrmkar 1,056 Posting Virtuoso

Good points by Adak.

I'd like to add that, why not simply check what malloc() returns, along the lines of ..

buffer = malloc(VSN_BUF_SIZE + 1);

if(buffer == NULL)
{
  /* Display error message and perhaps exit */
  perror("malloc()");
  exit(EXIT_FAILURE);
}

In case the reason is not an allocation failure, then you might post more code. Preferably a minimal complete example which still fails.

PS. Note that sizeof(char) is guaranteed to yield 1, so using it is redundant.

mitrmkar 1,056 Posting Virtuoso

>> I'm trying to iterate backwards through a vector.

Consider using a reverse iterator, for an example, have a look at vector::rbegin().

Fbody commented: Good idea. Not something you need very often, but very useful when you do. +4
mitrmkar 1,056 Posting Virtuoso

Try asserting your indices (everywhere) ..

#include <assert.h>

while ( i < m ) {

  for (j=0;j<in;j++,l++,i++) {

    /* rows and cols are your allocated dimensions ... */

    assert(k >= 0 && k < rows);
    assert(l >= 0 && l < cols);

    a[k][l]=i;
  }
mitrmkar 1,056 Posting Virtuoso

>> How should I initiate the str_month, I thought that line #12 initiation is sufficient

strncpy() does not necessarily NULL-terminate the string -- so, you might do e.g. ..

str_month[3] = '\0';
mitrmkar 1,056 Posting Virtuoso

>> My rand() isn't very random. As a matter of fact, it's not random at all!
>> I get the same result every time on both computers: 180428383

This is by design, you want to Initialize random number generator.

mitrmkar 1,056 Posting Virtuoso

>> No I have %s in my code. Here in the code I forgot it.

*Sigh* -- then I'm assuming that you may be incrementing the value of pos too soon (i.e. before printing).

For future reference, please

  • post the actual code that seems to be failing
  • and try to explain how the code fails, i.e. what you expect vs. what happens
mitrmkar 1,056 Posting Virtuoso

>> But when I am trying to pass this value to my struct and then to print it it does not work.

You have forgotten the %s format specifier, so ..

strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);
/* %s embedded in the format string */
printf("Name = %s\n",myaccounts->allaccounts[myaccounts->pos].name);

>> And another question how can I pass value to variable id for example using parking_system ?

Sorry, but I don't understand what that means.

mitrmkar 1,056 Posting Virtuoso

>> ... tokenize my string, which is read from a file, but my output is blank, why?

There could be many reasons for no output at all - was the file opened, what's in it etc. You could get a bit creative and display what you read from the file ..

while( getline( infile2, temp2 ) )
{
  cout << "read: [" << temp2 << "]" << '\n';
  ...
}

At any rate, in your first snippet, you should remove the return 0; -- it's inside the outer loop.

Other than that, I think you are close to a working solution -- maybe consider how will you want to handle leading/trailing and consecutive space characters.

mitrmkar 1,056 Posting Virtuoso

>> 'ofstream': No such file or directory":

The C++ header files are listed in this Reference.

mitrmkar 1,056 Posting Virtuoso

You might go through a function pointer tutorial.

mitrmkar 1,056 Posting Virtuoso

>> Why doesn't it accept the filename I give it?

open() only accepts a const char * , so you have to use c_str() ..

file.open(filename.c_str());

In your original post, you are calling main() recursively - it's a poor practice and also forbidden. You could instead devise a regular do/while loop, for example.

jonsca commented: Yep +5
mitrmkar 1,056 Posting Virtuoso

The code you've posted seems to be OK, so if the problem still persists, go ahead and post all of the code.

mitrmkar 1,056 Posting Virtuoso

my os is windows 7, and I think it is support conio.h
So how could I use conio.h to edit my color?

Even if your compiler has <conio.h> in some form or another, I doubt you'll find anything like you want in it.

Hence I'd suggest that you forget about <conio.h> and instead use Windows API, i.e. SetConsoleTextAttribute(). Have a look at a small example.

mitrmkar 1,056 Posting Virtuoso

>> a error message is triggered which says "The operation completed successfully."

This happens because you are using FormatMessage() incorrectly.

See Retrieving the Last-Error Code. Note how the value returned by GetLastError() is passed to FormatMessage() .

mitrmkar 1,056 Posting Virtuoso

@namasee:
You must have missed the Daniweb Rules ..

Keep it Clear
* Do post in full-sentence English

About the books, there is a lengthy sticky thread --> C++ Books

mitrmkar 1,056 Posting Virtuoso

"Selected" is initially an uninitialized pointer (read: it is garbage). Unless it gets assigned inside the loops, ValidMove() receives garbage.

I think that instead of assuming that everything works as you think it should, you should verify that this actually is the case here, so how about ...

void Board::Move(char Xin,char Xto, int Yin,int Yto)
{
    // Be safer, initialize to NULL
    Piece* Selected = NULL;
	
    for(int Row = 0;Row < 9;++Row)
    {
        for(int Col = 0;Col < 9;++Col)
        {
            if (Player1Pieces[Col]->Y == Yin && Player1Pieces[Col]->X == Xin)
            {
                Selected = Player1Pieces[Col];
            }
            else if (Player2Pieces[Col]->Y == Yin && Player2Pieces[Col]->X == Xin)
            {
                Selected = Player2Pieces[Col];
            }
        }
    }

    // Now make sure that there was an assignment ...
    if(Selected == NULL)
    {
        // Uh-oh -- do something ...
    }
  ...
}

This would be minor progress towards figuring out the reason(s) for the failure.

mitrmkar 1,056 Posting Virtuoso

>> .. my head is just spinning

If the libraries seem daunting, you might check out a basic MSDN CryptoAPI example Creating an MD5 Hash from File Content.

Suzie999 commented: ace +1
mitrmkar 1,056 Posting Virtuoso

As far as I can see, Intrade is right. There are two vectors involved, of which the m_vParentPop2 vector doesn't seem to be getting content in any way, whatsoever -- neither in the code snippet nor in the attached files. I.e. it's a zero-sized vector, which is illegally accessed, hence being a real concern for the OP.

Intrade commented: Finally someone else sees it... +0
mitrmkar 1,056 Posting Virtuoso

One more issue not yet mentioned, please the comments ..

void GeneticAlgorithm::RouletteWheelSelection()
{
  int iRandomNumber, iTotalFitness;
  CalculateTotalFitness(iTotalFitness);
  CreatePie();
  for(unsigned long int i=0; i<m_iPopDensity; i++)
  {
    GenerateRandomNumber(iTotalFitness, iRandomNumber);
    for(unsigned long int j=m_iPopDensity - 1; j >= 0; j)
    {
      if(iRandomNumber > m_vPopulation2[j]->m_iCumulativeFitness)
        // Assuming j == 0 here ...
        j--;
        // ... and j being unsigned, now j == ULONG_MAX
      else
      {
        m_vParentPop2[i] = m_vPopulation2[j]->m_pChrome;
        break;
      }
    }
  }
}

This is a good candidate for getting the out_of_range exception.

mitrmkar 1,056 Posting Virtuoso

>> float CDROM::ReturnCost(void) const' : overloaded member function not found in 'CDROM'

It's saying that the function declaration/definition signatures are not matching wrt. your const usage, so you want to have ..

class CDROM
{
...
 float ReturnCost() const; // <--- const was missing
 // The same thing for the other function
...
mitrmkar 1,056 Posting Virtuoso

In addition to what nezachem stated, it looks like you are also simply exploding the stack with your buffer declaration

char buffer [INT_MAX];
mitrmkar 1,056 Posting Virtuoso

In your code the following cast is wrong

for (j=0; j<BYTES; j++) printf("[%02X]\n", (const unsigned char *) data[j]);

Could you post the complete compilable code which demonstrates the working and non-working behaviour. I think your problem eventually relates to the default char being signed.

mitrmkar 1,056 Posting Virtuoso

Basically you are pushing the left-hand side of the string onto the stack and then comparing that data with the very same left-hand side of the string. The right-hand side of the string is not involved in the comparisons.

mitrmkar 1,056 Posting Virtuoso

>> output becomes full of INF INF INF 100 times :-)

You have to initialize the sum_* variables.

mitrmkar 1,056 Posting Virtuoso

>> I'm in the middle of installing Cygwin

If you don't yet have an IDE which plays nicely with Cygwin's GCC, then perhaps consider installing Code::Blocks too.

As for gcc and these variable length arrays (VLAs), which are a feature of C99 Standard, you'll probably want to use the -std=c99 compiler switch.

mitrmkar 1,056 Posting Virtuoso

The code has been compiled with a compiler that supports variable length arrays, Microsoft's C compiler does not and probably never will support that feature.

You can work around this with a simple #define , like so

#define NAME_OF_YOUR_CHOICE_HERE 8192
/* int n = 8192; */
...
int main() {
...
  char inc[NAME_OF_YOUR_CHOICE_HERE];
...
mitrmkar 1,056 Posting Virtuoso

It looks like it ought to work as such. However, you could check that;

  • You have the ON_WM_TIMER() macro in the dialog class' message map.
  • SetTimer() succeeds (returns a nonzero value).
  • UpdateData() succeeds (returns a nonzero value).
Jsplinter commented: Thank you so much! The bullet points made your answer even clearer! +1
mitrmkar 1,056 Posting Virtuoso

>> RNInterpreter.C:8: note: (perhaps a semicolon is missing after the definition of ‘Thousand’)

Your compiler is suggesting that Thousand.h looks like

class Thousand
{
  // All your Thousand stuff here ...

} // <--- ... but no ending semicolon after the curly bracket
mitrmkar 1,056 Posting Virtuoso

>> If you are compiling the program for UNICODE then why aren't you getting similar errors on all the other lines in your program (such as line 5)? fopen() is the non-UNICODE version, so the code is valid as such - although it is confusing. One option would be to completely stick with MFC i.e. using CStdioFile/CString for reading the file.

mitrmkar 1,056 Posting Virtuoso

Alright, then wrap the string literals with the _T() macro (from <tchar.h>), like so;

m_strLine.Replace(_T("\n"), _T("\r\n"));
mitrmkar 1,056 Posting Virtuoso

You want

m_strLine.Replace("\n", "\r\n");

>> m_strLine.Replace ( '\n' , '\r\n' );

That should give you a warning or two, are you ignoring compiler warnings?

mitrmkar 1,056 Posting Virtuoso

>> >>Why the const in the vector declaration (i.e. const T), are you sure about it?
>> Yes. I do not want the first data member's value to be changeable.

The const is likely to cause the code to break at the point where you are e.g. trying to store something in the vector. Maybe you just haven't gotten to that point yet?

A small example of it failing

#include <vector>
#include <utility>

int main()
{
  std::vector<std::pair<const int, int> > vect;  

  // This will not compile ...
  vect.push_back(std::make_pair(0,0));
}

Trying that with Comeau online gives a nice error message:

"stl_pair.h", line 37: error: implicitly generated assignment operator
cannot copy:
const member "std::pair<_T1, _T2>::first [with _T1=const int,
_T2=int]"

mitrmkar 1,056 Posting Virtuoso

Though I might be missing something here, but anyhow, I think changing the comparison functions to static would be in order here, so you'd have:

static bool Descending(const pair<const T, int> &, const pair<const T, int> &);

sort(mode.begin(), mode.end(), &Statistics::Stats<T>::Descending);

P.S. Why the const in the vector declaration (i.e. const T), are you sure about it?

mitrmkar 1,056 Posting Virtuoso

If you are on Windows, then look into FindFirstFile(). As the documentation states, you can use wildcards in the file/path name, e.g. "c:\\foo\\bar\\*.txt".

mitrmkar 1,056 Posting Virtuoso

>> it also displays the two parent directories, '.' and '..'.

Line #30 stores both "." and ".." in addition to the directory and file names, so you have to work around that.

mitrmkar 1,056 Posting Virtuoso

>> when I type 'q' into the command line it should enter that if statement, but it doesn't. fgets() stores the newline in the buffer, whenever there's enough room to do that. So, you are actually comparing strcmp("q", "q\n") , which apparently returns non-zero.

You might use e.g. strcspn() to get rid of this unwanted newline, like so ..

const char * quit = "q";
char buf[100] = "";

if(fgets(buf, sizeof(buf), stdin) != NULL) 
{ 
  /* If present, the trailing newline gets replaced with '\0' */
  buf[strcspn(buf, "\n")] = '\0';

  if(strcmp(quit, buf) == 0)
  {
    ... quit the program

P.S. fflush(stdin); may work with your current compiler, but horribly break on another one. An explanation and an alternative .. cprogramming.com FAQ

mitrmkar 1,056 Posting Virtuoso

man pages might be worth a try.