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

>>Would "sizeof int" solve the problem some ho
No. And neither will typecasting.

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

@Mike: All that loop you posted will do is store each digit of the character array in an element of the int array. That isn't what he wants. Each integer in the character array has to be converted from ascii representation to int and the result stored in an array.

There are at least two ways to do it

  1. I already posted one method using stringstream and will not repeat it.
  2. Something like what Mike posted but use atol() or strtol() to convert from ascii to int.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

as I said before you can use streamstring class to do that

#include <sstream>
...
int main()
{
   vector<int> nums;
   int n;
   unsigned char buf[] = "12 23 45 67 -12"; // read from a file
   stringstream str;
   str << buf; 
   while( str >> n )
      nums.push_back(n);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

To read it directly into an int array

#include <vector>
#include <fstream>
using std::vector;
using std::ifstream;

int main()
{
   vector<int> ay;
   ifstream in("file.txt");
   if( in.is_open() )
   {
     int n;
     while( in >> n )
        ay.push_back(n);
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You will have to post some of the contents of the file before we can tell you how to convert it. If its somethng like this:

123
456
789
10
20

You can use std::stringstream class to convert it from unsigned char* to an int array after its been read into one huge buffer. Or you could read it directly into the int array.

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

Too bad for you. And too late as well because I don't need you to work for me now. Pitty.

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

>>tmp += 2;
The first one doesn't work because the program keeps changing the value of temp on every loop iteration. Delete that line and the program will work.

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

>>let these extra things be discussed later

Nope. You get it after you do the work. Highland, Illnois, USA. Or, deposit $1,000.00 in my paypal account within the next 30 minutes.

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

And have a hand extend out of the monitor and slap him silly. I've seen an avatar something like that but don't remember whose it was.

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

Ok, got it done and tested. But be warned that if I give it to you your teacher will know that you didn't write it and you will probably fail the course.

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

>>i will be very thankfull to you if you can post the complete source code which can be executed..


I will do your homework for you if you come over to where I work and work 8 hours for me at WalMart store.

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

A little pseudocode

beginning of loop
   display "Enter your password\n";
   get user keyboard input
   if password is correct then exit this loop
   if this is the third attempt then exit this program,
end of loop
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Microsoft name mangling is a very complex topic. After a littled testing of the code in the original post to this thread I've come to the conclusion that Microsoft does include the return type as part of the mangled name. The compiler allows both versions of the function because names are mangled at compile time, not link time. Since the two classes are in two different *.cpp files the compiler has no way of knowing that the two functions differ only by return type. If you put both functions in the same *.cpp file the compiler generates the error message error C2556: 'void A::printText(void)' : overloaded function differs only by return type from 'int A::printText(void) .

To further complicate things, add a contructor to each version of the class and print out a different message in each constructor. Only one of the two contructors is called for both clA and clB objects.

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

Its not absolutely required that each class is coded in its own file -- usually the coding standards you follow will dictate that. For example if that's the way your teacher wants it then you much follow that rule. When you start real programming you will find out that related classes are often placed in the same heder file.

If you define a structure inside a class then that structure is really only useful in the context of the structure

class A
{
public:
   struct x
   {
      // blabla
   }
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Use your head for something other than a hat rack.


declare the structure only one time, not three times. If you declare the three classes in their own *.h files then put the structure in its own *.h file and include it in the header files that use it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
struct x
{
  // blabla
};

class A
{
   // blabla
};
etc.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Declare the struct before any of the classes.

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

Ok, but shouldn't it results in compilation time error,

No because as I said before the compiler only works on one *.c or *.cpp file at a time (unless one is included in another, which is a horrible thing to do).

After all, after linking there are two methods with the same signature, but different return types...

Return types don't differentiate methods or functions. Parameters are what makes then different.

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

If you compile Test.cpp and NewFile.cpp separately, then attempt to link then the linker should complain about multiple occurrences of A::printText() function. If it doesn't then your compiler may be just silently merging them.

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

>>So why we can overcome this and create class definition in more than one file?

Put the class declaration in a header file with *.h extension and include the header file in all *.cpp files that use it. The implementation code goes only in one *.cpp file.


>>Summarize: why it is legal to do so?
The compiler processes only one *.cpp file at a time. So if you add the class implementation code in multiple files the compiler won't know it until the linker attempts to put everything together. Then it will tell you that you have included the implementation code in multiple files.

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

functions in *.c files can't use c++ classes. And functions in *.cpp file are mangled unless extern "C" is used.

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

Only if the DLL will be used by to C and C++ programs.

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

Very similar to the way you would do it with printf(), but use sprintf() to format the string in a character array, then send the character array to MessageBox() or whatever win32 api function you want. Note that if your program is compiled for UNICODE then you will have to use swprintf()

In c++ you could also use stringstream instead of sprintf().

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

how is pArray declared? I thought it was unsigned char* pArray; >> Is this bad practise?
No, but if you had null-terminated the string then cout << would have worked too.

splitting that string up into its individual itegers will require stingstream class and vector

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

>>pArray = new unsigned char *;

Remove the star. You want to allocate an array of characters, not pointers

You need anoter seekp() to reset the file descriptor back to beginning of file before read.

after the read() you will want to null-terminate the string because read() doesn't do that for you.

You could do just cout << pArray << '\n';

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

That will not get you the number of bytes you need to allocate for the character array. What that does get you is the number of integers in the file. Those are two different things.

Each digit in the file will take up one character in the unsigned character array. So the number 123 till require 4 bytes (3 for the digits plus 1 for a whitespace between them).

Since you have to read them into an unsigned character array you can have to treat them as normal characters, not integers. All you have to do is use seekg() to locate end of file, tellg() to get file size, allocate the unsigned char array that size + 1, then call read() to read the entire file into memory at one shot.

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

You declare it globally in one, and only one *.cpp file. Other *.cpp files that use that header do not have to do that.

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

static class objects also have to be declared globally in a *.cpp file

// Node.cpp
#include "Nodes.h"

#include <list>

// declare the static object globally
std::list<Node> NodeManager::node_list;

void generateNodes() {
	Node n1(1, 346.0, 26.5, -470.0);
	n1.children.push_back(2);
	n1.children.push_back(4);
	n1.children.push_back(5);
	NodeManager::node_list.push_back(n1);

	// defining more nodes
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The way I do it is to first call time() and localtime() to get the data as struct tm. Then use sprintf() to format the date into a character array

char date[40];
time_t now = time(0);
struct tm* tm = localtime(&now);
// now format the date -- I put it in yyyymmdd format so that the files
// can be easily sorted.
sprintf(date,"%04d%02d%02d", tm->tm_year+1900,tm->tm_mon+1, tm->tm_mday);
ofstream out(date);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes I know that problem -- and I already told you what it is. Your program has other problems too that you will discover after you learn how to debug your program. Asking us to debug your program for you is a huge waste of your time.

Basic debugging isn't that difficult. Set a break point (where execution of the program will stop) then step through it one line at a time. In VC++ 2010 Express there is a Debug menu that will have options to do that. You can probably learn the basics in less than an hour.

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

You need to learn how to use your compiler's debugger so that you can single-step the execution of the program and see for yourself what is causing the problem.

Hint: line 89 is in the wrong place.

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

What does get_line() do? Does that function also read a line from the file? If it does then your program is reading two lines at a time before doing anything with them.

void readData ()
{
    FILE * pFile;
    NoOfRecordsRead = 0;
    char buffer [Line_Char_Buffer_Size];

    pFile = fopen (INPUT_FILE_NAME , "r");
   
    If (pFile == NULL) 
         perror ("Error opening file 'Countries.txt' !");
    else
    {
      while ( fgets(buffer, Line_Char_Buffer_Size,pFile) != NULL)
      {
         printf ("%d] aLine => %s\n", NoOfRecordsRead, buffer);
         globalCountryDataArray [NoOfRecordsRead++] = createCountryRecordaLine);
      }
      fclose (pFile);
    }
			
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Since you are using precompiled headers, stdafx.h must be the first include file in stdafx.cpp and all other *.cpp files of the project. Other *.h files can be included after stdafx.h and will not be included in the *.pch precompiled header file.

I don't think that is the problem though because the compiler would have given a different error message. So the only other possibility is that test.h is not located in the directory you think it is. It should be in the same folder as the other project .cpp files.

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

I don't see SHOWERROR anywhere in the code you posted.

SAFE_DELETE is not really needed because delete and delete[] work correctly when the pointer is a NULL pointer. So there is no need for those two macros.

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

Of course there are other solutions. And the one you suggested will work too. Just create another buffer and copy the contents of sCmdInt into it.

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

String literals can not be changed because most compilers put them into read-only memory. How to change that? Notice that you have to make each buffer large enough to hold whatever it is that DIAG_ReadMem32() wants to copy into it. If you don't know exactly how big to make it, take a guess then double it.

char sCmdInt[8][255] = {0};
strcpy(sCmdInt[0],"123");
strcpy(sCmdInt[1],"234");
DIAG_ReadMem32(sCmdInt[0], sCmdInt[1]);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>@AD did you edit by any chance?

Yes. My statement was incorrect. C99 allows const, previous versions did not. You have to be careful about what versions of c standards people are talking about. The new Cx00 (or whatever it will be called) allows a lot of other stuff that is new to the C language. such as this

int x = 100;
int array[x];
nbaztec commented: You could've said No & make me look stupid; but you didn't :) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There should have been a * in that statement wchar_t *a = L"ab"; or like this wchar_t a[] = L"ab"; In any event you can't put two characters in single quotes and stuff them in a single byte of wchar_t.

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

>>He says: wchar_t or Wide Character stores 2 bytes instead of 1 so we can feed 2 chars

Again you probably misunderstood him. Yes, wchar_t is more than 1 byte (sometimes 2, sometimes 4 or more depending on the operating system). That doesn't mean it stores two or more characters in just one wchar_t. wchar_t someWCHAR = L"ab"; Like all strings it has to be surrounded with double quotes, not single.

The reason wchar_t is more than 1 byte per character is because an alphabetical character in some languages can not be represented in just one byte.

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

>>Well he dictated that for that matter,
That is a different story altogether. You mis-quoted him in your original statement. There is a difference between telling you he doesn't want your programs to do that then it is telling you the c standards say you can't do that. Its called "coding standards", which you will encounter throughout your programming career.

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

>>it's nowhere written that C expects VOID MAIN!
compilers are (and do) allowed to support that.

>> mean if we can just use const in C, then why use #define, off the top of my head, is it due to this? :

Both C and C++ supprot macros and const. macros are used for other things which const can not.

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

Some of those questions may be true or false depending on the version of c and c++ standards. But current c/c++ standards are like this:

>>In C program , the MAIN() should NOT RETURN a value
That's a lie, and has always been false. Either you misunderstood him, or it has no clue what he is talking about.

>>He stated I'm wrong.
Stop taking that course because all he will do is tell you more lies and mis-information.

>>C does NOT have REFERENCE variables.
True -- C has pointers

>>C++ ,REFERENCE variables are USED in functions
Obviously true. Reference variables could be declared globally but not used there.

>>C does NOT SUPPORT DEFAULT arguments.
True

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

There is no reason why this should not work, unless the function DIAG_ReadMem32 is attempting to change the contents of either of those pointers.

char *sCmdInt[8] = {0};
sCmdInt[0] = "123";
sCmdInt[1] = "234";
DIAG_ReadMem32(sCmdInt[0], sCmdInt[1]);

If the values in the array are hex numbers, such as "0x100", then you can use strtol() to convert them to integers

int x;
char str[] = "0x123";
char *ptr = 0;
x = strtol(str,&ptr, 16);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

And goto might get you a failing grade on your program.

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

You need to modify function parameters to pass them by reference instead of by value so that the changes made by the function can be seen by the whoever called it. void readData(ifstream &fin, double &sidea, double &sideb, double &sidec) Now do something similar with the other functions.

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

named semaphore is here for *nix. MS-Windows example.

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

>>can we use the continue statement in if-else statement?
No

>>i know the continue statement only can use under for loop right?
No. Can also be used on do and while loops.

pseudocode

set loop counter to 0
beginning of loop
   increment (or decrement) loop counter
   if loop counter matches some condition
      exit the loop
   do something here
end of loop
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The operator >> isn't quite right. A common problem of using eof() incorrectly. The way you have it coded the function will read the last line twice and insert the same number twice into the list

istream& operator >> (istream &sourceFile, LinkedList &list)
{
   elementType nextElt;
//   sourceFile >> nextElt;
//   while (!sourceFile.eof())
//   {
//      list.insert(nextElt);
//      sourceFile >> nextElt;
//   }
   while( sourceFile >> nextElt )
       list.insert(nextElt);
   return sourceFile;
}

Next: insert

bool LinkedList::insert(elementType elt)
{
    nodePtr newNode = getNode(elt);
    if (newNode == NULL)
        return false;
    if (head == NULL)
        head = newNode;
    else
    {
        nodePtr currPtr = head;
        nodePtr prevPtr = NULL;
        while (currPtr && currPtr->item < newNode->item)
	    {
            prevPtr = currPtr;
            currPtr = currPtr->next;
        }
        // 
        if( currPtr == NULL)
        // If at end of list, just add the new node
        // to the tail of the list
        {
            prevPtr->next = newNode;
        }
        else
        {
		    newNode->next = currPtr;
            if( prevPtr == NULL)
                // If at the head of the list,
                // add the new node to the head
                // of the list
                head = newNode;
            else
                // insert the new node
                // somewhere in the middle of the list
                prevPtr->next = newNode;
        }
    }
    return true;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Console::Read() only reads a single key from the keyboard buffer. When you type 'Y' followed by a <Enter> key there are two keys, not one, in the keyboard buffer. You have to remove the '\n' so that Console::Readline() can work properly.

One way to fix that is to use Console::Readline(), not Read() to get the 'Y' or 'N' response. Another way is to flush the keyboard buffer of all keys by calling Console::Read() in a loop until it returns '\n',