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

>>if(str1==str2)

Can not compare two strings like that. use strcmp() if( strcmp(str1,str2) == 0)

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

hmm, in trying that, I discovered this:
but instead I see nothing! Why would that be? I was also seeing nothing with the \r, but I bet it's the same reason.

Thanks,
Dave

You wrote the wrong test program. Assuming you are writing this under MS-Windows os. If *nix then delete windows.h and replace Sleep() with sleep().

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    int counter;
	for(counter = 1000; counter < 10000; counter++)
	{
		cout << "\rhi " << counter; cout.flush();
		Sleep(250); // 1/4 second sleep
	}
    cout << "\ncounter = " << counter << "\n";
return 0;
}
daviddoria commented: Ancient Dragon has all the answers! +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Has anyone seen anything that does something like this?
just add '\r' to the print statement

int counter;
for(counter = 0; counter < 1000; counter++)
{
    cout << "\rcounter";
    Sleep(500); // 1/2 second delay
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Well Yeah.....20 errors in total.....which includes an include file error??

Nope -- here are the errors I get. As you can see, the first error is about undefined symbol "printMenu". Fix that up and recompile. I'm not going to fix all the errors in this program for you -- that is for you to do.

1>c:\dvlp\tmp\lib_test\lib_test\login.h(31) : error C3861: 'printMenu': identifier not found
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(99) : error C2084: function 'int login(void)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\login.h(9) : see previous definition of 'login'
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(138) : error C2084: function 'void printMenu(void)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\menu.h(7) : see previous definition of 'printMenu'
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(159) : error C3861: 'login': identifier not found
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(179) : error C2084: function 'void viewFile(void)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\view.h(7) : see previous definition of 'viewFile'
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(186) : error C2084: function 'void options(int)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\menu.h(7) : see previous definition of 'options'
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(196) : error C3861: 'viewFile': identifier not found
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(208) : error C2084: function 'void createFile(void)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\create.h(9) : see previous definition of 'createFile'
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(233) : error C2084: function 'void readFile(void)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\read.h(6) : see previous definition of 'readFile'
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(249) : error C2084: function 'void exitScreen(void)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\exit.h(8) : see previous definition of 'exitScreen'
1>c:\dvlp\tmp\lib_test\lib_test\lib_test.cpp(258) : error C2084: function 'void editFile(void)' already has a body
1> c:\dvlp\tmp\lib_test\lib_test\edit.h(10) : see previous definition of 'editFile'

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

Its not possible to write to an executable file while it is executing -- the operating system will forbid it.

tux4life commented: That was an answer right to the point ! +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1) in login.cpp change #include <login> to #include "login.h" 2) in include.h, change #include <login.h> to #include "login.h" When surrounded by angle brackets < and > the compiler does not look in the project directory for include files. You have to put it in quotes to do that.

After you fix that you will get a lot of other errors.

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

>>but it closed after I input the random number.

Because you programmed it to do that. As ^^^ said you have to put something before the return statement to make the program stop and wait for you to enter something from the keyboard.

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

The general method of using multiple *.c files is to put the function prototypes in a header file, then include that header file in all the *.c files that need the functions. If you look at a couple standard C header file that are supplied by your compiler you will see that they only contain #defines, typedefs, and function prototypes.

Here is a simple, brief example. You need to compile both *.c programs then link the two object files in order to get the executable program. Exactly how to do that depends on the compiler you are using.

// foo.h
extern void foo();
// foo.c
#include <stdio.h>
#include "foo.h"

void foo()
{
   printf("Hello World\n");
}
// main.c
#include "foo.h"
int main()
{
    foo();
}
tux4life commented: Nice explanation ! +1
Salem commented: Good job +29
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

zip up your entire project folder and attach it so that I can see what the problem is.

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

So -- where is <login> header file? Is that the same file you posted previously but as login.h? If it is, it belongs in the same folder as the other *.cpp files in the project.

>>Now, Do you suggest to change all my .h file to .cpp?'
Not at all -- only the ones with executable code.

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

just as well you got that error because NEVER EVERY UNDER NO CIRCUMSTANCES put executable code in a header file (inline functions are the only exception.) Take that function out of that *.h file and put it in a *.cpp file. Then compile the two *.cpp files separately and link the object files together. Exactly how to do that will depend on the compiler you are using.

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

Would an if and else-if statement work as well??

No because you need a loop to look at all the items in the array. You will need an if statement inside the loop. Something like this:

int j;
bool found = false;
for(j = 0; j < i; j++)
{
   if( array[j] == random_integer)
   {
         found = true;
         break;
   }
}
if( found == false)
{
    // add new item to the array
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

before adding the new number to the array you need to search the array to see if its already there. A simple for loop will suffice.

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

The smallest possible array size would be 19 because the last number read does not need to be placed in the array.

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

you can not pass a single character to that function because the function expects a string. Change the function parameter from std::string to char if you want to pass it a single character.

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

you will have to post the function converter() because we have no idea what it does or what it returns.

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

Also, when does the value of j ever get reset to 0? j needs to be incremented from 0 to maxj and then reset back to 0. In the meantime the value of i should not change. Something like this:

for(i = 0; i < maxi; i++)
{
    for(j = 0; j < maxj; j++)
    {
         fscanf(...);
    }
}

Another way to do it is to use fscanf() as the loop controller

int i = j = 0;
while( fscanf("%f", &arr[i][j]) > 0)
{
    j++;
    if( j == maxj)
    {
        j = 0;
        i++;
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 18: C language requires all variables have to be declared at the top of the block. So you need to move the declaration of arr up to about line 8.

arr = malloc( maxi * sizeof(float*) );
       for ( i = 0 ; i < maxi ; i++ ) {
       arr[i] = malloc( maxj * sizeof(float) );

There are other ways to do the above, but this is the one I find most convenient.

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

You may have physically placed the libraries in those directories, but did you tell your compiler that it needs to look for those libraries ? I don't use Borland compilers so I don't know how to do that. Maybe in project setting somewhere.

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

>>I think the problem is I am adding it to a Vector of StaffMember objects. Staff Member is the base class, so would case the derived classes into StaffMember objects?

Simple:

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

class Base
{
public:
    Base() {x = 0;}
    virtual void Display()= 0;
protected:
    int x;
};

class Derived1 : public Base
{
public:
    Derived1() { x = 1;}
    virtual void Display() { cout << "This is Derived1\n";}
};

class Derived2 : public Base
{
public:
    Derived2() {x = 2;}
    virtual void Display() { cout << "This is Derived2\n";}
};

int main () {
    vector<Base*> theList;
    Derived1 * pD1 = new Derived1;
    theList.push_back(pD1);
    Derived2 * pD2 = new Derived2;
    theList.push_back(pD2);

    vector<Base*>::iterator it;
    for( it = theList.begin(); it != theList.end(); it++)
       (*it)->Display();

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

>>Is this safe to assume its the same on all c++ compilers?
No. See limits.h for size limitations.

>>Why does 'printf("size of size_t: %d\n",sizeof(size_t ));' make a warning?

Probably because size_t is unsigned long and %d is signed int. Try %u and see if the warning persists.

>>The size_t is a unsigned long int, so apparantly it uses 8 bytes,
No, its only 4 bytes on a 32-bit compiler. See the sizeof operator.

>>It seems that the max value of a size_t on my 64bit ubuntu system is
18446744073709551615

It's the compiler's limits in limits.h. If you are using a 32-bit compiler than the size of size_t is still 32-bits even though you may be running the compiler on a 64-bit operating system.

Considering the output you received from your program I would guess that g++ is compiling as a 64-bit compiler.

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

This works great for me, using VC++ 2008 Express

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main(int argc, char* argv[])
{
        std::vector<int> truth_table;
        std::vector<vector<int> > truth_table_col;

        double no_of_twos = 7;
        double base_two = 2;
        
        cout << "No of 2's = " << no_of_twos << "\n";
        double combi = pow(base_two,no_of_twos)-1;

        for(int hfh=0; hfh<=(int)combi; hfh++)
        {
                for(int ghg=0; ghg<=(int)no_of_twos; ghg++)
                {
                        truth_table.push_back(0);
                }
                truth_table_col.push_back(truth_table);
                truth_table.clear();
        }
        for(size_t i = 0; i < truth_table_col.size(); i++)
        {
            vector<int> &table = truth_table_col[i];
            for(size_t j = 0; j < table.size(); j++)
                cout << table[j] << " ";
            cout << "\n";
        }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In C languge you do not use the c++ reference & operator, but pointers

void join(queue *q,int elem)
{
        q->items[q->rear]=elem;
        q->rear++;
}

And invoke it like this: join( &que_write,1);

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

Is this supposed to be C, not C++? I question this because of int isempty(queue &q) -- the parameter looks like a c++ reference.

Is that supposed to be circular queue where the front and back variables wrap around to 0 then the buffer items is filled up? If yes or no you need to add code to the join() function to wrap the front variable when it reached MAX, or not allow any more items in the queue when (front+1) > rear.

You need to firm up your understanding of the front and back variables.
front: This is where the next item will be inserted into the queue. When an item is inserted (see join function) this is the variable that should get incremented.

rear: This is where the next item will be removed from the queue. When an item is removed from the queue (see leave function) this is the variable that will be incremented.

You need checks in join() that front+1 does not exceed rear. And you need checks in leave() that rear+1 does not exceed front.

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

Temp is an unitialized and unallocated pointer. See the two lines I changed below

void Deque::Addfront(int n)
{
    Temp = new node; // <<<< Here
	Temp->value=n;
	if (front==NULL)
	{
		Temp->link=front;
		front=back=Temp;
	}
	else
	{
		Temp->link=front;
		front = Temp; // <<<< here
	}
return;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need some { and }s.

while (indata)
    {
    if (letter >='a' && letter <='d')
    {
       pass++;
       all++;
       }
      else    (this is where the error is)
       if(letter >='d' || letter <='f')
      {
       fail++;
       
       valid=(pass+fail);
       }     
     else
    {
       outdata<<letter<<endl;
       all++;
       indata>>letter;
       }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I stopped smoking in Sep 2001 after roughly 40 years. Stopping is not easy and took many attempts. At 2 packs a day guy, and $4.00 USD/pack (cost when I quit), I figure I have saved at least 5,840 packs or $23,360.00 USD. Cigs are now about $6.00USD/pack where I live. I can buy a really nice new car for that much :)

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

You could check before adding it to the vector.

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

Yes, I erased a parentheses when fixing the color manually.
It should be: while((c = fgetc(fp)) != EOF)

Ohhh I see my mistake now -- wasn't the parantheses, but the fgets() function instead of fgetc(). Thank you for the correction. :)

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

He really meant while((c = fgetc(fp) != EOF)

Nope. You have mis-matched parentheses. Count them.

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

1) c must be of type int, not char, because EOF won't fit in a char.

2) Open the file in binary mode and read it one character at a time. The function readFile() is very dangerous because you have no clue how much memory was allocated for the string!

int spot = 0;
int c;
while( (c = fgets(fp) ) != EOF)
{
    string[spot++] = c;
}
string[spot] = 0; // null-terminate the string
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>//Shifting should happen here as far as i know

As long as the original string and the new string have the same length there is no need for shifting. If however the original values are not the same it is not safe to change the size of the original *.exe file because you will change the location byte offset of its program variables and other symbols.

Also, you might corrupt the program's stack and other data areas by changing string lengths. Example: originally a program might be like this:

int main()
{
    char ip[] = "127.0.0.1";
}

Now if the program you are writing changes the text to "255.255.255.255" you will be changing the length of the string that the compiler had allocated for the original value. That is not good news!

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

make the arrays float instead of int. ifstream is producing an error attempting to convert that decimal point to an int.

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

Its not in the registry, but maintained on a clock cpu chip. This is how to synchronize network computers.

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

It might help understand the problem if you would post an actual example of the buffer.

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

You could shift everything after "Happy" left 5 positions to overwrite that text, which will move the empty spaces to the end of the buffer. Since its a binary buffer (I think it is anyway from your description) you can fill the remaining bytes with '\0'.
So if the buffer looks like this: "Don't Play with MemoryHappyxxxxyyyzzz" you would shift "xxyyyzzz" left to overwrite Happy so that it winds up looking like this Don't Play with Memoryxxxyyyzzz'\0''\0''\0''\0''\0'" After that you could call realloc() to resize the buffer or just leave it alone.

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

depends on your version of windows. But why would you want to do that anyway? With Vista just copy it to here: C:\Users\<your login name here>\Desktop

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

thank you all for your contributions, i need to know how to make numbers appear as asterisks e.g 123456 comes out like (******)

read this thread

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

From the code you posted in #11 above:

line 27: delete that line because it is causing the program to read twice. The data was already read on line 25 in the for loop.

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

I'm using VC++ 2008 Express and I had to change the resource.h file as shown below. Make sure there is a '\n' after the last line or the resource compiler will complain.

#ifndef APSTUDIO_INVOKED
#define IDR_MYMENU 101
#define IDI_MYICON 201

#define ID_FILE_NEW 1001
#define ID_FILE_OPEN 1002
#define ID_FILE_SAVE 1003
#define ID_FILE_EXIT 1004

#define ID_EDIT_CUT 2005
#define ID_EDIT_COPY 2006
#define ID_EDIT_PASTE 2007
#endif

Then you have a typo in the resource.rc file.

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

you can assign any number you want, as long as all the numbers in the RC file are unique. Put a lot of space between control types, such as start icons with 100, menus with 200, dialogs with 1000, etc. The reason is to make it easy to add new items of the same type without having to renumber everything and still keep the numbers organized by type.

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

Read the tutorial and it will tell you how to do it!

You really should get a GUI resource editor so that you can create dialogs and associated controls graphically.

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

cin.clear() doesn't flush the input stream -- just clears some of cin's error flags which is entirely a different animal. Re-read the article, don't just glance over it or you will miss it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
NicAx64 commented: Thanks man , now i can type the question directly nica tool , need to find a exe or mozilla addon of that web application. Or somebody write a extension +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

When the error occurs you need to flush the input buffer of all remaining characters. Narue has written an excellent article about flushing the input buffer here.

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

ftell() returns a long int, and the max value is declared in limits.h. If yours is like mine then the 4 gigs is too big.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int** foo()
{
    int i;
    int** array = malloc(100 * sizeof(int *));
    for(i = 0; i < 100; i++)
        array[i] = malloc(20 * sizeof(int)); // allocate 20 integers
    return array;
}

int main()
{
    int ** array = foo();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

lines 8 and 9. Files can not be opened outside a function like you have it. Move both those lines down to within main() (line 14).

line 16: eof() will not give you the file size, it just tells you that the end-of-file has been reached and nothing more. To get the file size

// set file pointer to end-of-file
infile.seekp(0, ios::end);
// get file location
N = infile.tellg();

line 22: Not only doesn't that work with most compilers (I think it will when the newest c++ standards are implemented), but its also wrong. The value of N (see above) will not tell you the number of integers in the file, but how many characters are in the file. For example the number 100 will be counted as 4 or 5 depending on the operating system (3 digits plus '\n').

To get the actual number of integers in the file you will have to read each line and count them as you go along.

line 25: eof() doesn't work like that either because it will cause your program to process the last line twice. A better solution is like this

int i = 0;
while( infile>>P[i]>>V[i] )
{
   // blabla

   ++i;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No, you apparently lost. It's not really anything to us if we convinced you not to cheat. Had you studied like you should have then you would not have had to turn in a blank piece of paper as your assignment. You apparently have a bad case of poor time management, and after nearly 10 years in college I would have though you should have at least learned how to study and manage your time more effectively.

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

you need to explain better what you want.