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

You need to use double pointer there so that foo() can allocate memory for the pointer in main(). You can do the same with as many other pointers as you like, it is not limited to just one pointer. This solves the problem is returning more than one string at the same time.

void foo( char ** ptr)
{
     *ptr = malloc(255); // allocate some memory

     strcpy( *ptr, "Hello World");
.
}
'

int main()

{
    char *ptr = 0;

    foo( &ptr ); // <<< pointer to a pointer

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

So let me get this straight. You have no clue about how to write a c or c++ program yet you expect to write something as complicated as that?? put this project aside for awhile until you learn the c++ language.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
bool isFree() //are we free to service new customer?
      {
	 if(free)
	    return TRUE;
	 else if(customer.done())
	    free = TRUE;
	 return free;
      }

Rewrite it like this: There is a difference between TRUE and true; bool variable requires true, not TRUE

bool isFree() //are we free to service new customer?
 {
	  if(customer.done())
	    free = true;
	 return free;
      }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I'm starting to code the main program but i got a lot of errors when i compiled the code

Fix the errors one at a time, recompile after each fix to see what's really left. Compilers like to puke up 10-20 errors for the same line.

Post the first two or three errors so we can see what they are.


One problem I see is on line 10 of that last code snippet. The function is declared to return an unsigned int while line 10 returns an int. Wrong typecast.

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

you can use sprintf() to format the string then write() to write that string to the file.

line 18: if the file can not be opened the program needs to do two things, not one.

if( id < 0)
{
   perror(...);
   return 1; // abort the program
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

how about something like

if exist target rm -f target
if exist target.exe rm -f target.exe
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

or maybe rm *.o target*.* [edit]:icon_eek: ^^^ That will also delete the source code![/edit]

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

line 26: The value of argc will not be less than 1. The first argument to the program is the name of the program itself (on most operating systems).

line 27 and 28: What are you trying to do there? cout knows nothing about %d format string. You need to study how to use cout before continuing with that program.

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

Do you know how to use the new operator to allocate an array? If yes, then just allocate an array that is twice the size as the array argument, such as size * 2 You will need a function something like this:

int* expand(int nums[], int size)
{

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

Maybe you will find sometime a little more interesting in the field of artificial intelligence and handwriting recognition.

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

A real-life project: Write a computer program that reads data from a serial port that contains UPC product codes from an external scanner device. Use that UPC code as a key to look up product information in an SQL database (such as MS-Access, MS-SQL Server, Oracle, Sybase, etc) then display the information on the screen and transmit it out another serial port to a Large Character Printer (you need not know technical specs of the printer, just send the product data.)

Shinedevil commented: Even though it means nothing, I like your post so here is some (0) rep. +0
Salem commented: You should have said "Large Hadron Collider" :D +19
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

aint array must be allocated with new operator. The way you tried to do it is not yet supported by most c++ compilers.

linesize = line.size();

  int *aint = new int[linesize];               // The arrays satisfying the condition
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I'm not getting the // screen_win.c and // screen_borland.c

You will have two *.c files. screen_win.c contains the code for your compiler, and screen_borland.c will contain the code that you turn in to your instructor. Regardless of how you do it you wil have to compile your program with that borland compiler so that your instructor will not have any problems compiling it.

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

line 9: that will not compile with most compilers because that feature is new to c99 standards and most compilers older than that. If you want to use charcter arrays then you will have to allocate them at runtime

int n;
char ** a;
cin >> n;
a = new char*[n];
for (int i=0; i<n; i++)
{
    a[i] = new char[15];
    cin.getline(a[i], 14);
}
restrictment commented: Nice code, I did not know this. =) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>so you say there's problem with header files?
No. The program didn't like that constructor for some reason.

Success!

class WCS
{
public:
    WCS(const wstring& str)
    {
        s = new wchar_t[str.length()+1];
        wcscpy_s(s,str.length()+1, str.c_str());
    }
    ~WCS()
    {
        delete[] s;
    }
	operator wchar_t*&()
    {
		return this->s;
	}
protected:
    wchar_t* s;
};

int main()
{
    WCS m(L"Hello");
    const wchar_t* s = m.operator wchar_t *&();
    wcout << s << '\n';
    std::cin.get();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I commented out everything but the constructor/destructor and it still crashes. And changing string to just s didn't help either. Replaced wchar_t* with char*t and it works.

class WCS
{
public:
    wchar_t* s;

    WCS(const std::wstring& wstr)
	{
        s = new wchar_t[(wstr.size()+1) * sizeof(wchar_t)];
        wcscpy_s(s,wstr.size(), wstr.c_str() );
	}
	~WCS()
	{
		delete [] this->s;
	}
//	operator wchar_t*&()
//    {
//		return this->s;
//	}
};

int main()
{
    std::wstring wstr(L"Hello");
        
    WCS w(wstr);
    //wchar_t* const& c = w.operator wchar_t *&();
    //std::wcout << c;
    std::cin.get();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 11: you should name that character array something else because string is declared in the header file <string>. Since you used using namespace std; there is now a name clash between std::string and the string you declared in your WCS class.

Your class would be a lot simpler if you would use std::wstring instead of wchar_t*. Afterall, that was the main objective of creating the std::string class in the first place -- to avoid all the problems with pointers.

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

I'm not about to write the program for you, but just show you how to build the list of keywords from the command line arguments.

Lets say you have a structure like this:

struct keywords
{
    std::string word;
    int count;
};

Now all you have to do is build a list of those structures from the command line arguments

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

int main(int argc, char* argv[])
{
    vector<keywords> keywordList;
    for(int i = 3; i < argc; i++)
    {
            keywords k;
            k.word = argv[i];
            k.count = 0;
            keywordList.push_back(keywords);
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A couple ways to do it
1) create a structure that contains the keyword and count of the number of times it appears. Then create an array of those structures, one element for each keyword on the command line. Then read each word of the file, for each word loop through the array of keywords and increment the count when a match is found.

2) similar to above but use a <map> instead of an array of structures.

Note: Since you posted this in c++ forum you might want to use ifstream instead of FILE*.

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

Of course there is

int a,b,c,d,e,f,g,h;
total(a,b,c,d,e,f,g,h,0);

Or you can just pass an array of integers, which doesn't require va_args at all.

const int MAX = 15;
int array[MAX];
total(array, MAX);
mrnutty commented: Happiness, eh? What does that mean? +4
Salem commented: Ah an array, so simple and clear :) +19
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I have a hunch that you are compiling the original 10 year old files.

I think you may be right. Here is the first few lines of the file what is in the tar file that he posted. Note that it's a lot different than the file he posted in the original post.

// Project: B*-trees floorplanning
// <snipped out personal information>
// Date:    7/19/2000 ~

//---------------------------------------------------------------------------
#include <stack>
#include <algorithm>
#include "btree.h"
//---------------------------------------------------------------------------
float rotate_rate = 0.3;
float swap_rate = 0.5;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't understand what you are asking. That function will sum up however many integers you want to pass to it as long as the last parameter is 0 so that the function knows when to stop.

You might want to recode that while loop because the first time through the value of arg is undefined.

while( (arg = va_arg(listpointer,int)) != 0)
{
    sum += arg;
}

int main()
{
    // Note that the lastg parameter is 0
    // which tells total() when to stop summing.
    // The first parameter is just ignored by total()
    int x = total(0,1,2,3,4,5,6,7,9,10,11,12,0);
}
sumitg979 commented: Good Reply +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I have included iostream but i still get that error.

No you didn't. Look again at btree.cc

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

system() is a c and c++ function. Otherwise you have to use os-specific functions such as the spawn family of functions on *nix, of ShellExecute() and CreateProcess() on MS-Windows.

Dave Sinkula commented: Echo? +13
Nick Evan commented: Here's some rep, because "you haven't got enough yet" :icon_wink: +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

post btree.h so that we can compile it.

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

Post the code you tried.You could probably use the same character array for all n number of strings because you didn't say there was a requirement to save all those strings.

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

>>if ( nCheck && nCheck2 && nCheck3 && nCheck4 == BST_CHECKED )

I hope you know that is the same thing as this: if ( nCheck != 0 && nCheck2 != 0 && nCheck3 != 0 && nCheck4 == BST_CHECKED )

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

Now tell us what is wrong with the code you posted? Compiler errors? Yes, then post some of them.

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

Step 1: call function time() to get current time in seconds.

Step 2: call localtime() to convert the integer returned by Step 1 into a struct tm

Step 3: Check if tm_mday is 1

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

Sorry, I can't help you because I don't know the first thing about it.

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

why don't you just use the same compiler your teacher is using? It would solve all your problems.

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

Yup, your program was doing integer division, therefore 2/5 is 0 but 2.0/5 will give you the result you desire.

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

I just found out that VC++ 2008 Express doesn't produce the assembly code that older M$ compilers did. Your function is the same as produced by VC++ 2008.

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

Looks ok to me -- just remember that srand() is executed only once during the entire lifetime of the program while rand() is executed as many times as you need it.

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

>>I've been playing with the code to get the things I need but nothing is working.

That's nice -- now post the code you tried because we are not clairvoyant.

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

That's basically the idea. On Intel based PCs the compiler might actually implement it by using assembly instructions such as the scansb instruction, which greatly speeds up the operation because there is no software loop involved.

NathanOliver commented: thanks for your help +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

re-install windows 7 and let it take over the entire hard drive.

Or you could try one of these methods

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

No while( *str++ ) size++;

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

what operating system are you doing this on?

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

thanks I can't find anything that borland defines that MINGW32 does so as both systems use windows I will use the windows one...

You could always create your own macro, such as __MINGW__ then use it like I showed in my previous post.

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

What I would do is define a macro for my compiler, then it it's not defined the compiler would be the one your teacher uses. For example if you are using vc++ 2008 then you could use the predefined macro _MSC_VER which is the compiler version

#ifdefined (_MSC_VER)
#include <windows.h>
#else
#include <conio.h>
#endif
// blabla
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You are not going to find a tutorial that does all that stuff. Learn one thing at a time. google for "ODBC tutorials" and you will find out how to access SQL databases -- you need a good understanding of C or C++ languages. Once you learn that (maybe a day or two depending on how much time you are willing to put into it) you can start learning about how to write GUI programs. There are quite a few of those tutorials too. google for "win32 api tutorials" (C tutorial) or "wxWidets tutorials" (c++ tutorials)

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

I suppose you could iterate through the map and sum up the length of all the strings and the number of bytes occupied by all those integers. Then there will be a small overhead the map class needs for each of the nodes.

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

The static_cast<> lets the c++ compiler do a better job of ensuring the cast is valid. For example: this will produce a compiler error (vc++ 2008 express)

int  main()
{
    int x = 123;
    char* p = static_cast<char*>(&x);  // << ERROR
    char* p = (char*)&x;  // << NO ERROR
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Im just using VC++ express 2008. All default settings so I couldnt tell ya

Then that's probably the problem -- turn UNICODE off! To do that go to menu item Project --> Properties (the last item on that menu) --> Configuration Properties --> General. Then on the right side of the screen change Character Set (third from the bottom) to Not Set Also remove those unnecessary typecasts.

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

>>if (*(array i) == *(array i) && &*(array k) != &*(array

You must have mis-quoted it because that makes no sense.

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

Are you compiling your program for UNICODE ?

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

I'll start you off by showing you how to compute the number of quarters

Quarters = amountleft / 25;
amountleft %= 25;

Now just do the same thing to get dimes and pennies.

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

>>Do not use nickel and half-dollar coins.

half-dollars I can understand because they are not very common. But nickels??? :icon_eek: As a customer I would not like it if the cashier gave me seven pennies when he/she had a nichel in the drawer.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
#include <string>
#include <sstring>
using std::string;
using std::stringstream;

int main()
{
   int n = 1234567890;
   stringstream str;
   // insert int into stringstream
   str << n;
   // convert to std::string
   string s = str.str();
   // now just insert commas in appropriate places
}