nucleon 114 Posting Pro in Training

Here's a parital fix. I fixed a couple of problems (noted in comments), and a couple of stylistic things in your first two functions. I didn't even look at the other functions, but it works better. It seems to still be missing one name.

int main()
{
    ifstream fin ("a6.txt");
    if (!fin) {
        cout << "Input file does not exist.\n\n";
        return -1;
    }

    AVLNode* head = AVLCreation (fin);
    fin.close();

    InorderTraversal (head);
    cout << "\n\n";

    PreorderTraversal (head);

    cin.sync(); cin.get(); // Will work on more systems.
}

AVLNode *AVLCreation(ifstream &InFile)
{
    AVLNode *Root = NULL;
    AVLNode *Leaf, *temp, *Scan;

/*** Moved getline into the while condition since it is
     a getline attempt that will inform you of the EOF.
     You should add a check for blank names (or all spaces). */
    string name;
    while (getline (InFile, name))
    {
        temp = Root;
        Leaf = new AVLNode;
        Leaf->Name = name;
        Leaf->Balance = 0;
        Leaf->RightChild = NULL;
        Leaf->LeftChild = NULL;

        if (!Root)
        {
            Root = Leaf;
            Scan = Root;
        }
        else
        {
            while (1)
            {
                if (Leaf->Name > temp->Name && !temp->RightChild)
                {
                    temp->RightChild = Leaf;
                    temp->Balance--;
                    break;
                }
                else if (Leaf->Name < temp->Name && !temp->LeftChild)
                {
                    temp->LeftChild = Leaf;
                    temp->Balance++;
                    break;
                }

                if (Leaf->Name > temp->Name && temp->RightChild)
                {
/*** Switched the order of the next two lines */
                    temp = temp->RightChild;
                    temp->Balance--;
                }
                else if(Leaf->Name < temp->Name && temp->LeftChild)
                {
/*** Ditto. */
                    temp = temp->LeftChild;
                    temp->Balance++;
                }
            }
            PreorderScan(Root, Scan);
        }
    }
    return Root;
}
nucleon 114 Posting Pro in Training

Nice one AD! :)
I did not spot that call tucked away in there.
getCommandId is being called with a string literal (constant),
which it tries to modify in strToUpper: getCommandId("move"));

nucleon 114 Posting Pro in Training

Do you still get the error if you remove:
* the file stuff? (replace it with a single buf load to tokenize)
* the token stuff?
Post a single file minimal version that recreates the error.

nucleon 114 Posting Pro in Training

> Whats SkipNChars?

That's it right there. No guarantees. I'm unsure about the design decision to return the str pointer (pointing to the null character) when the end of string is reached; it could return a NULL ptr instead. It should also be renamed; maybe SkipNDelims.

But forget about that one. Here's a simplified version, just for newlines.

char* SkipNewlines (char* str, size_t n) {
    while (n--)
        do {
             if (!*str) return str; /* Returns pointer to '\0' */
        } while (*str++ != '\n');
    return str; /* Returns one past nth '\n' */
}

Use it like this:

char buffer[] = "here is your text\nline after line\netc....\n";
/* Init a char pointer. */
char *pLine = buffer;
/* Skip the first 34 lines. */
pLine = SkipNewlines (pLine, 34);
/* Now pLine points to beginning of 35th line;
    or the end of the string (the '\0')
    if there were less than 35 lines. */
nucleon 114 Posting Pro in Training

You can use this as char* pLine = SkipNChars (xmlText, 34, '\n'); to skip the first 34 lines in a string buffer.

char* SkipNChars (char* str, size_t n, char c) {
    while (n--)
        do {
             if (!*str) return str; /* Returns end of string */
        } while (*str++ != c);
    return str; /* Returns one past nth char c */
}
nucleon 114 Posting Pro in Training

Got it. It was a combination of two things.
Firstly, you do need to say STEP = ....erase(STEP).
Secondly, if you do erase an element, you must not increment STEP.

vector<int>::iterator STEP;
for (STEP=CAR_VEC[i].PASSD.begin(); STEP!=CAR_VEC[i].PASSD.end(); ) {
  if ( *STEP==ROAD_STOP[j]->NUM){
    cout <<"ok- bus:"<<i<<" pass:"<< *STEP
         <<" stop:"<<ROAD_STOP[j]->NUM<<endl;
    STEP = CAR_VEC[i].PASSD.erase(STEP);
  }
  else
    ++STEP
}
nucleon 114 Posting Pro in Training

You find the = sign just like you found the # sign. Like this: posEqual = search.find("="); If posEqual is equal to string::npos, there was no = sign. Maybe skip that line (and print an error msg).

If posEqual is equal to line.size() - 1 then the = sign is at the very end of the string, which is a special case. Basically the value is a null string, or maybe you consider this to be an error.

If posEqual < line.size() - 1 then

value = line.substr(posEqual + 1, string::npos);

You may wish to trim spaces from before and after the value thus retrieved.

kelechi96 commented: THANKS ! +1
nucleon 114 Posting Pro in Training

It should probably be:

STEP = CAR_VEC[i].PASSD.erase(STEP);
nucleon 114 Posting Pro in Training

Just put some spaces or tabs, that is, \t characters at the beginning of the menu lines.

nucleon 114 Posting Pro in Training

Yes, I forgot the following after strncpy: word[pComma - pColon] = 0;

nucleon 114 Posting Pro in Training

OP> How do i get it to parse the ":" and just leave the word sunny

/* This assumes there are no other colons or commas
       before the ones you want. */

    char word[MAX_WORD];
    char *pColon, *pComma;

    pColon = strchr (string, ':');
    if (!pColon) { /* No colon found; handle error. */ }

    pComma = strchr (string, ',');
    if (!pComma) { /* No comma found; handle error. */ }

    if (pComma < pColon) { /* Comma before colon; handle error */ }

    /* Move pColon past the colon. You may also wish to move
        pColon forward past any spaces here. */
    ++pColon;

    strncpy (word, pColon, pComma - pColon);

OP> how would i get it to find that line in a big XML file

If it is always the same line number, just eat lines up to there, then read the line you want. If it's not always on the same line number you will have to search through the text for an identifying pattern, either reading line-by-line or reading the entire text file into memory first. In this situation, with an XML or HTML file, I usually read the entire text into memory, then search that as one big string. Regular expressions are helpful here, but not necessary.

nucleon 114 Posting Pro in Training

What does "written successively in the 15th line" mean?
Do you mean starting at the 15th column?

nucleon 114 Posting Pro in Training

Another thing is that string::search() returns string::npos if the search function does not find what it was told to find, and not -1

This is an interesting point. I notice in the standard, clause 21.3 (Class template basic_string), paragraph 6, it does say that: static const size_type npos = -1; size_type is defined a little above that as: typedef typename Allocator::size_type size_type;

nucleon 114 Posting Pro in Training

Resource file:

#include <windows.h>
#include "resource.h"

IDD_CONTROLS_DLG DIALOG DISCARDABLE  0, 0, 160, 230

STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "HSBC Conversion Clock"
FONT 8, "MS Sans Serif"

BEGIN
    LTEXT           "Conversion Time: ",
                    -1,
                    20,  20, 80,   8

    EDITTEXT        IDC_CONVERT_TIME,
                    100,  20, 40,  12,    ES_RIGHT | ES_AUTOHSCROLL

    CHECKBOX        "Between 3/8 - 3/29?",
                    IDC_BETWEEN1,
                    40,  40, 100,  8,     BS_AUTOCHECKBOX

    CHECKBOX        "Between 10/25 - 11/1?"
                    IDC_BETWEEN2,
                    40,  60, 100,  8,     BS_AUTOCHECKBOX

    CONTROL         "Eastern Time",
                    IDC_EASTERN_SELECT,
                    "button",             BS_AUTORADIOBUTTON | WS_GROUP,
                    40, 80, 100, 8

    CONTROL         "UK Time",
                    IDC_UK_SELECT,
                    "button",             BS_AUTORADIOBUTTON,
                    40, 100, 100, 8

    CONTROL         "HK Time",
                    IDC_HK_SELECT,
                    "button",             BS_AUTORADIOBUTTON,
                    40, 120, 100, 8

    LTEXT           "Eastern Time: ",
                    -1,
                    20, 140,  80,  8

    EDITTEXT        IDC_EASTERN_TIME,
                    100, 140,  40, 12,    ES_RIGHT | ES_AUTOHSCROLL

    LTEXT           "UK Time: ",
                    -1,
                    20, 160, 80, 8

    EDITTEXT        IDC_UK_TIME,
                    100, 160, 40, 12,     ES_RIGHT | ES_AUTOHSCROLL

    LTEXT           "HK Time: ",
                    -1,
                    20, 180, 80, 8

    EDITTEXT        IDC_HK_TIME,
                    100, 180, 40, 12,     ES_RIGHT | ES_AUTOHSCROLL

    PUSHBUTTON      "Convert",
                    IDC_CONVERT,
                    25, 200, 30, 15

    PUSHBUTTON      "Quit",
                    IDCANCEL,
                    100, 200, 30, 15
END

Code file:

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "resource.h"


#define GETITEM(item) GetDlgItem(hWndDlg, item)


void convert (HWND hWndDlg) {

    char ConvertTime[8], ETime[8], UKTime[8], HKTime[8];
    char Hour[8], Min[8], AMPM[8];
    int intHour;

    // Gets the user entered time and converts it to usable format
    GetWindowText(GETITEM(IDC_CONVERT_TIME), ConvertTime, 9);
    strncpy(Hour, ConvertTime, 2);
    strncpy(Min, ConvertTime + 3, 2);
    strncpy(AMPM, ConvertTime + 6, 2);
    intHour = atoi(Hour);

    if (strchr(ConvertTime, 'A') == NULL ||
        strchr(ConvertTime, 'P') == NULL ||
        strchr(ConvertTime, 'a') == NULL …
nucleon 114 Posting Pro in Training

Try changing the relevant part of the rc file to this:

CONTROL "Eastern Time", IDC_EASTERN_SELECT,
        "button", BS_AUTORADIOBUTTON | WS_GROUP,
        40, 80, 100, 8

    CONTROL "UK Time", IDC_UK_SELECT,
        "button", BS_AUTORADIOBUTTON,
        40, 100, 100, 8

    CONTROL "HK Time", IDC_HK_SELECT,
        "button", BS_AUTORADIOBUTTON,
        40, 120, 100, 8

Set the initial radio button while handling WM_INITDIALOG:

// Init radio button group.
    CheckRadioButton (hWndDlg,
                      // First and last of consecutive list
                      // of radio button id's.
                      IDC_EASTERN_SELECT, IDC_HK_SELECT,
                      // Id to set; all others are reset.
                      IDC_EASTERN_SELECT);
nucleon 114 Posting Pro in Training

I believe you do in fact catch the exception as it is "going on". And since you can catch different exception types, you can tell which is which.

Here's a simplified version of loadFile. More ... stringy. Less ... errory.

#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>

std::string loadFile ( const char* path )
    throw (std::runtime_error) {
    std::string fileContent;
    std::ifstream fin ( path );
    if ( !fin ) throw std::runtime_error ( "Couldn't open file" );
    std::string line;
    while ( getline ( fin, line )) {
        fileContent += line;
        fileContent += '\n';
    }
    return fileContent;
}

int main ( int argc, char** argv ) {
    std::string file = loadFile ( "myfile.txt" );
    std::cout << file;
}
nucleon 114 Posting Pro in Training

This is old school indeed:

int set_keysig (s, ks, init)
  char s[];
  struct KEYSTR *ks;
  int init;
{
   /* function body */
}

Should be updated to:

int set_keysig (char s[], struct KEYSTR *ks, int init)
{
   /* function body */
}

Other than that (and the missing stdio.h header and the fact that you're not returning anything), your posted code has no errors. The lowercasing code should set all but the first character of each word to lowercase. (This is assuming that the "working code" really does "work" ... which seems unlikely given your results.)

Aia commented: Thank you for correcting me. +15
nucleon 114 Posting Pro in Training

sum must be initialized to zero.
You don't need the variable add.

nucleon 114 Posting Pro in Training

> How else can they be different?
Yep, they must be the same function.

It works if you pick a specific copy like this: mycar.racer::cruise(50); or this mycar.tank::cruise(50); But "diamond inheritance" looks like something to avoid in general. Seems strange that the language wasn't designed to include only one copy in such a case.

nucleon 114 Posting Pro in Training

I find it useful on occasion to place a prototype in a function. (pssssssssssss)

nucleon 114 Posting Pro in Training

typedef struct node{ int *datapointer; struct node *next; } NODE; You probably want int *datapointer to be int data , because of how you are using it here (you are trying to assign an integer to a pointer): temp -> datapointer = num; Otherwise you'd have to allocate space for the integer you want to assign, assign the integer to that allocated space, and store the pointer to the space in datapointer. Seems overly complicated just for an integer!

nucleon 114 Posting Pro in Training

In equal(), declare L1 and L2 as NODE*, not node.
C does not generally support bool (use int).
TRUE and FALSE need to be declared, or just use 1 and 0.
(Or perhaps your version of C supports bool, in which case you mean true and false, not TRUE and FALSE.)
You spell p_cmp_func as p_cmp_f in the return.
NEXT is not defined anywhere but is used in the return.

By the way, it is customary to mark a thread as solved when it is solved.

nucleon 114 Posting Pro in Training

You shouldn't really "BUMP" a post. At least pretend to ask another question!

regex is something you can look up yourself, but it is not necessary for your problem. Here's an example of parsing a simple config file:

/* File config.txt:
num = 123
str = hello
flt = 12.2
*/

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

struct Config {
    int    num;
    string str;
    double flt;
};

void loadConfig(Config& config) {
    ifstream fin("config.txt");
    string line;
    while (getline(fin, line)) {
        istringstream sin(line.substr(line.find("=") + 1));
        if (line.find("num") != -1)
            sin >> config.num;
        else if (line.find("str") != -1)
            sin >> config.str;
        else if (line.find("flt") != -1)
            sin >> config.flt;
    }
}

int main() {
    Config config;
    loadConfig(config);
    cout << config.num << '\n';
    cout << config.str << '\n';
    cout << config.flt << '\n';
}
nucleon 114 Posting Pro in Training

You need to use the prefix form of ++ and --. So: return( add( ++n, --m ) ) ;

nucleon 114 Posting Pro in Training

Remove the additional asterisk.
Declare table like this: Object3D* table;

nucleon 114 Posting Pro in Training

Good point !

nucleon 114 Posting Pro in Training

>You normally don't need the ignore( ) between two input
>statements of the same type.

Or when the getline() is before the cin>>.

hurbano commented: thanks for the help +1
nucleon 114 Posting Pro in Training

"Please criticize my code." My favorite question! :)

The biggest problem is that your variable names are too long. "Descriptive" does not mean "long". Here are some better names:

mathematicalOperation   expr       (or expression, if you must)
operatorSign            oper       (or operation)
getFirstDigitIndex      iDigit
operatorSignIndex       iSign
firstNumber             strNum1    notice that these go nicely
secondNumber            strNum2    with the names num1 and num2

Here are the structural changes I would make:

int main() {
    string expr;
    float num1, num2;
    char oper;
    while (1) {
        cout << "Enter an expression (-1 to exit)\n";
        cin >> expr;
        if (expr == "-1") break;
        int iDigit = ...;
        int iSign = ...;
        if (iSign == -1) {
            cout << "No operator found.\n";
            continue;
        }
        // etc.
    }
}

Get rid of the "SEE YOU LATER" at the end. (Why do beginners always do that?)

Do not put copyright notices in simple code like this (or anything you write for the next four years).

tux4life commented: Nice advices to the OP ! :) +1
nucleon 114 Posting Pro in Training

It is my understanding that the third pointer level was there to make an array of objects. But since an Object3D object represents a single object, you would make a pointer to an array of them like this: Object3D *object; The asterisk above now represents the level of indirection previously represented by an (extra) asterisk in front of each of the variables facets, vertices, nFacets, and nVertices in Object3D.

nucleon 114 Posting Pro in Training

Let's see. There are 24 places that the pieces could be. There are 15 identical pieces for each player, and only one player's pieces can be in a particular position.

The simplest data structure that fits these requirements is an array of 24 integers. Zero means the space is empty. Positive values indicate black (say) while negative values indiate white. The absolute value indicates how many pieces are in that position.

nucleon 114 Posting Pro in Training

I don't know wxWidgets, so I'm just guessing here. Try making DECLARE_EVENT_TABLE(); public instead of private.

If that doesn't work, try putting this in your code:

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
END_EVENT_TABLE()

Or, if you aren't goint to have any events, remove DECLARE_EVENT_TABLE(); altogether.

nucleon 114 Posting Pro in Training

Presumably something needs to be declared as virtual in Utility. Without code, it's hard to say exactly what.

nucleon 114 Posting Pro in Training

You're pathetic.

nucleon 114 Posting Pro in Training

Calm down, jephthah.

I am well aware of the problems with globals. I simply assumed that his project was due soon, and looking at the crazy structure of the whole thing it would not be worth his time in this particular instance to "de-globalize" his variables.

Have you even read the code? For instance, how would he pass the requisite data to display()? I assume there's a solution, but I don't feel like working it out. Are you trying to help anybody but yourself here?

What exactly is your problem?

nucleon 114 Posting Pro in Training

The full-cookie solution would be to use conditional compilation to implement code for each platform. Here is an MSDN page describing the functions in conio.h. Remember that you can simply open these headers up and look at them to see what's in them.

jephthah commented: yeah, that's it. +5
nucleon 114 Posting Pro in Training

You need to declare your object array like this:

typedef struct Object3D {
	int**   facets;
	float** vertices;
	int     nFacets;
	int     nVertices;
} Object3D;

Object3D *object; // Object array
int numObjects;   // Number of objects in object array

And the object array is accessed like this: object[nObj].nFacets At this stage I wouldn't bother de-globalizing your variables; your code-structure (such as it is) depends on them being global.

nucleon 114 Posting Pro in Training

conio.h (if available to you) has the function _getch() which is unbuffered and does not echo the character read.

nucleon 114 Posting Pro in Training

You're trying to pass a const object to a function that does not receive a const object. Change one or the other.

nucleon 114 Posting Pro in Training

You don't need to do this: planner[x][2].ClassMeet Just this: planner[x].ClassMeet And planner should not be defined like this: classPlanner planner[100][6]; but like this: classPlanner planner[100]; Also, Print and Enter don't return anything, so make them void. Get rid of the global x variable, define it in main, and change Enter to accept a reference as well as change Print and Enter to accept a classPlanner array.

Also, if the class number is in fact equal to x, then don't ask for the class number (and get rid of the entry in the structure). Just store the other information at position x in the array, like you are doing.

So here's a skeleton:

void Print (classPlanner planner[], int x) {
}

void Enter (classPlanner planner[], int & x) {
}

int main() {
    classPlanner planner[100];
    int x = 0;
    //...
    Enter (planner, x);
    //...
    Print (planner, x);
    //...
}
nucleon 114 Posting Pro in Training

You're looking for something like this:

struct Class {
    string name;
    int    num;
    Day    meet;  // For Day use whatever is appropriate
    Time   start; // Ditto for Time
    Time   end;
    string teacher;
    int n  umStudents;
};

cin >> planner[x].name;
cin >> planner[x].num;

As for making the array index equal to the class number, that depends on how the classes are numbered. You don't want a bunch of missing elements in the array wasting space.

nucleon 114 Posting Pro in Training

Filter the output with grep: ./Test "$i.txt" "$i.obj" | grep -vi '^[:alpha:]' Perl's good for this:

for $i (1..5) {
  for (`./Test $i.txt $i.obj`) {
    next if /^\s*\w/;
    print;
  }
}
nucleon 114 Posting Pro in Training

Unfortunately I cannot run your code right now, but in looking it over I noted the following.

The strangest thing (if I'm not mistaken) is the loop in polygon. Shouldn't you be passing in "i" from shape? I may just not understand what's going on here, but it seems weird to be looping through numObjects (in polygon) within a loop that is doing the same thing (in shape).

In readFile, since you're treating the whole file as a string, you should null-terminate it. So malloc an extra char's worth and be sure to set it to zero. (Or use calloc.)

The following are points that probably won't change anything but are still good practice.

You should really move the fopen() call into readFile and pass in the fileName. There's no advantage to doing it the way you're doing it. Also, you should probably read the file in text mode instead of binary mode. (Change "rb" to "r".)

In loadShape, since you've already read the entire file into fullFile before calling countObjects, you may as well pass fullFile to countObjects so it doesn't have to read the file again.

Your delimiters are better expressed like this: char * delim = " *:\t\n\r\\"; You should compare the result of strcmp to 0 (not NULL).

nucleon 114 Posting Pro in Training

You're gonna kick yourself! Instead of this for(i = 1; text_tokenized[i] != NULL; i++) You mean this: for(i = 1; i < words; i++) {

nucleon 114 Posting Pro in Training

It seems fairly self-explanatory. Remember to (of course) look the functions up on MSDN. Just include windows.h to use the code above.

If you must use ShellExecute (and there certainly can be reasons for that) then use ShellExecuteEx since it gives you the process handle.

nucleon 114 Posting Pro in Training

Assuming Windows, here's an example of waiting for an exe to finish.

STARTUPINFO si = {sizeof (STARTUPINFO)};
    PROCESS_INFORMATION pi;
    CreateProcess( "\\windows\\notepad.exe", NULL,
        0, 0, 0, 0, 0, 0, &si, &pi );
    WaitForSingleObject( pi.hProcess, INFINITE );
nucleon 114 Posting Pro in Training

Post your entire program again and I'll take a look at it.

nucleon 114 Posting Pro in Training

Is dwExtraInfo always 0? If not, try interpreting it as the keystate. That is, test it against, say, MK_CONTROL while pressing the control key. If it's just an arbitrary number, then maybe it's pointing to something ... but what? The lack of documentation on this is pitiful.

I've asked on comp.os.ms-windows.programmer.win32. I know I should have told YOU to ask, but I just did it. ;) I will post any answer back here.

nucleon 114 Posting Pro in Training

You're right. I wasn't thinking clearly with that last piece of sh..., I mean code.

Since you do not seem to need the previous data, you may as well just free the memory and allocate it again with your allocate_memory function. So just free it like this before allocating it again.

void release_memoryi(GLint*** facets, int nShapes, int nRows) {
    int i, j;
    for(i = 0; i < nShapes; ++i) {
        for(j = 0; j < nRows; ++j) free(facets[i][j]);
        free(facets[i]);
    }
    free(facets);
}
nucleon 114 Posting Pro in Training

It would seem that your only hope (besides the static idea) is to investigage what MOUSEHOOKSTRUCT's dwExtraInfo member holds.

nucleon 114 Posting Pro in Training

I only looked at allocate_memoryi and reallocate_memoryi. Any problems with them probably also exist in the "f" models. Compare the code below with the original carefully to see the differences.

GLint ***allocate_memoryi(int nShapes, int nRows, int nCols)
{
	GLint ***shapeArray;
	shapeArray = (GLint***) malloc(nShapes * sizeof(GLint**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (GLint**) malloc(nRows * sizeof(GLint*));
		for(j=0; j<nRows; ++j)
		{
			shapeArray[i][j] = (GLint*) malloc(nCols * sizeof(GLint));
		}
	}
	return shapeArray;
}

GLint ***reallocate_memoryi(int nShapes, int nRows, int nCols)
{
	GLint ***shapeArray;
	shapeArray = (GLint***) realloc(facets, nShapes * sizeof(GLint**));
	for(i=0; i<nShapes; ++i)
	{
		shapeArray[i] = (GLint**) realloc(shapeArray[i], nRows * sizeof(GLint*));
		for(j=0; j<nRows[i]; ++j)
		{
			shapeArray[i][j] = (GLint*) realloc(shapeArray[i][j], nCols * sizeof(GLint));
		}
	}
	return shapeArray;
}

Also, you really shouldn't be using all those globals. It's just lazy, and as your program gets bigger it will come back to haunt you.