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

Starting a new thread with the correct date is not going to help you get anyone to do your homework for you. Nobody is going to symaphize with someone who makes NO effort to do it yourself.

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

I don't know what [BookD].authorsurname is either -- not a valid statement. What are you trying to do with that line? Did YOU write that code? Apparently not if you have to ask what it means.

My guess is that the function is displaying the values of one instance of the class. Somewhere else is an array of classes.

void Book_D::showall()
{  cout<<"\n"<<authorsurname<<"\t\t"<<authorforename<<"\t\t\t\t\t"<<bookname<<
               << "\t\t\t\t\t"<<.genre<<"\t"<<booknumber<<"\n";
}
int main()
{
     Book_D array[80; // an array of 80 Book_D objects
   ...
     for(int i = 0; i < 80; i++)
          array[i].showall();
   ...
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1. Array() by itself if not a valid statement. If you want to create an object of type Array, then give it a name. The code snippet below instantiates an object of class Array and names is obj.

Array obj;

After you have named the object you can use its methods by stating the object name followed by a dot and then by the method name. Pay close attention to the function parameters -- their type must match the number of parameters and data type in the class description.

Array obj;
...
int aptr;
obj.values(arrayA, arrayB, &aptr);

Most of the errors you posted seem to be pretty clear about what is wrong. The error gives you the line number and what is wrong. Look at the code and you should be able to determine the cause of the error. Youre compiler's documentation will also normally give you a more elaborate explaination of the error number.

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

Also what is meant by the terms 'Programming Time' and 'Execution Time' in relation to algorithms.?

how long time it takes for the algorithm to execute. For example, if testing a sort algorithm, how long does it take the alogithm to sort the data. That is normally measured by getting current system time before starting the algorithm, again after and subtacting the two.

clock_t t1 = clock(); // get start time
// do algorithm
clock_t t2 = clock();// get end time

clock_t delta_time = t2 - t1; // difference
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

first, you need to write main() function

#include <stdio.h>

int main()
{

   // put your code here
}

next declare a character array that will hold the name that you input from the keyboard

#include <stdio.h>

int main()

{
   char name[80]; // allow up to 79 characters plus null terminator
   // put your code here
}

call printf() to display the prompt, and fgets() to get the input from the keyboard.

print("... < text goes here > ... ");
fgets(name,sizeof(name),stdin);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
void main()

That is the first problem -- it must be

int main()

Please identifiy other errors you are getting so that we don't have to compile your code. List some of the errors.

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

can you tell me what i am doing wrong? thank you.

Can't say until you tell us what problems you are having. compiler errors? yes, what are they. runtime errors? name them too. don't be afraid to elaborate a little bit -- band width is not very expensive :)

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

.is there any way of specifying the design of the window in the resource file in the way you can design your dialog box and menus etc ?

use CFormView instead of CView and the view will look and act like a dialog box -- and you can design it like one too.

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

during the window's initialization, use the control's Create function, in which you specify the parent HWND, RECT containing the location and height, and the control's ID, among other things. The instance object of the control must NOT be local to a function, but global in the c++ class or program file in which it is used because it must stay in scope during the lifetime of the window.

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

How's it going:
How do you use fopen() to establish a stream to console I/O (keyboard)? What I needed to do is read in data from the console I/O and then send it elsewhere using fputs. I just don't know what the first argument for fopen("here", r) would be. Oh, and this is an event driven program (chatting); and the fopen will be part of a read event handler. I am supposed to read data from the console using fgets and then send it out to the other recipient using fputs. Thanks.

Its not necessary to use fopen to open the console keyboard -- its already open when the program starts. You can use stdout, stdin or stderr.

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

i do not know how to compare the date the customer enter with the local time

use the struct tm and associated functions that are in time.h. get local time in time_t (an unsigned int) -- time() function returns that. Then convert the time string the user enters into struct tm, you will have to parse the string and apply its individual parts to the structure. After you have done that, call mktime() to convert struct tm into time_t. Now all you have to do is compare the two time_t objects using normal integer comparison operators.

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

cout << Name->nm.dat

assuming nm has been previously allocated with new operator

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

functions are just small sections of code that perform a specific task. For example, you might have a function called menu() that will display a menu, prompt the user for the menu item and return the menu item number that was selected. Below is an illustration of how to write the menu and use it in main().

int menu()
{
   int menu_item = 0;
   // display menu is not shown here
   printf("Enter menu item number");
   scanf("%d", &menu_item);
   return menu_item;
}

int main()
{
   int menu_item;
   menu_item = menu();
   // blabla

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

I had to split the lines to make it work

#include <stdio.h>

#define PI 3.14159265358979323846
int main(int argc, char* argv[])
{

	double unrounded = PI;
	double rounded = (int)((unrounded*1000)+.5);
	rounded /= 1000;

	printf("%.8f\n%.8f\n", unrounded,rounded);
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

BTW, your program's half C, half C++.

Nope. its all c++. Afterall c++ includes all of the standard c functions, turning them into c++ functions with the same name. :eek:

I originally wrote the program using cout instead of printf(), but printf() is easier to code when you need specific number of decimals.

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

it won't work in your program as I wrote it. You have to make appropriate changes to the structure. If you are using a compiler that has a debugger, learn to use the debugger so that you can single-step through the program and see what is going on.

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

that only rounds to the nearest whole number and discards all deimal places.

#include <iostream>
using namespace std;

#define PI 3.14159265358979323846
int main(int argc, char* argv[])
{

	double unrounded = PI;
	double rounded = (double)((int)((unrounded*1000)+.5)/1000);

	printf("%f\n%f\n", unrounded,rounded);
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

C99:

int main(void) {
    _Bool b;
    return 0;
}

what is _Bool ? never seen that data type. should it be bool ?

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

It took a little while to work out the algorithm, but below is one way to sort the array indirectly using another integer array. First, initialze an int array with values 0, 1, 2, .... During the sort, swap these numbers instead of members of the original structure array. The algorithm was taken from here which may have been the same one you used.

#include <iostream>
using namespace std;

struct str
{
	char name[80];
};

str s[] = {
	{"six"},
	{"seven"},
	{"eight"},
	{"Filex"},
	{"Max"},
	{"Albert"},
};


void q_sort(str array[], int numbers[], int left, int right)
{
  int pivot, l_hold, r_hold;

  l_hold = left;
  r_hold = right;
  pivot = numbers[left];
  while (left < right)
  {
    while (( strcmp(array[numbers[right]].name, array[pivot].name) >= 0) && (left < right))
      right--;
    if (left != right)
    {
      numbers[left] = numbers[right];
      left++;
    }
    while ((strcmp(array[numbers[left]].name,array[pivot].name) <= 0) && (left < right))
      left++;
    if (left != right)
    {
      numbers[right] = numbers[left];
      right--;
    }
  }
  numbers[left] = pivot;
  pivot = left;
  left = l_hold;
  right = r_hold;
  if (left < pivot)
    q_sort(array,numbers, left, pivot-1);
  if (right > pivot)
    q_sort(array,numbers, pivot+1, right);
}

void quickSort(str array[], int numbers[], int array_size)
{
  q_sort(array, numbers, 0, array_size - 1);
}

int main(int argc, char* argv[])
{
	int i;
	int array[] = {0,1,2,3,4,5,6};
	quickSort(s,array, 6);
	// duplicate the s array so that we can rearrange it
	// according to the order of integers in array[].
	struct str s1[6];
	memcpy(s1,s, sizeof(s1));
	for(i = 0; i < 6; i++)
	{
		s[i] = s1[array[i]]; …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

there are lots of them.

int main()
{
   char *ptr = malloc(10);
  return 0;
}

The above will produce an error when compiled with c++ compilers.

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

The >> operator strips the spaces. If you want to retain them use read() function.

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

>If yes, then when do we need to make a call to setvbuf
>with 'buf=NULL' since the 'C' compiler always assigns a buffer automatically(according to u)?
Well, if you want to specify buffering but don't want to manage your own buffer, you would pass a null pointer and allow setvbuf to handle it.

calling setbuf with buf=NULL makes the i/o UNBUFFERED, which is not the same as allowing setbuf to handle it. The default (not calling setbuf or setvbuf) is buffered i/o.

Except for the lack of a return value, the setbuf() function
is exactly equivalent to the call

setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);

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

>Some thingthat we can do in C and Not in C++
Compile on certain embedded systems. :)

Yea, but those embdded systems are not ansi C compliant either. There is a different C standard for embedded systems, such is a subset of standard C and embedded systems don't have to use that standard either. Many embedded systems don't support C at all.

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

I would probably make a duplicate copy of the string, convert it to all upper (or lower) case. Then instead of using the Replace method you show, find the position of the first occurrence of "<BR>" then replace it in both copies of the string. I don't have enough experience with VC++.NET to show an example. But that class probably has a find function that returns the starting position of a substring.

or you could write your own function that will do case-insensitive search and replaces.

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

its probably because what you see in the ascii editor are not spaces at all. Many editors, such as Notepad will display non-printable characters as spaces. What makes you think that file contains spaces? binary files contains all sorts of stuff that are meaningless to ascii edits. If you realy want to see what is in the file, and you are on MS-Windows os, you can use debug.exe that is in the MS-Windows installation directory -- that program has been distrubuted with every M$ operating system since MS-DOS 1.0 over 20 years ago. It has a split screen. On the left side is the hex values of every byte in the file. The right side contains the ascii values where non-printable bytes are displayed as a '.' (period).

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

C++ can to everything that C can do plus a lot more. There is nothing in C that can't be done by c++.

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

short answer -- no, you can't round the internal representation of floats and doubles. That's because many numbers cannot be represented exactly, there are mathametical reasons for that, which is over my head :) but has something to do with powers of 2.

However, you can display them rounded using printf("%3.4f") will print only the first 4 decimals.

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

What makes you think it can't be done in C? win32 api has quite a few functions that require callback functions from the application program. I suspect (but don't know) that *nix does too. callback functions like that are often used in serial communications, such as data arriving on COM1 or COM2 ports, and socket programming.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
input (emp_n,emp_w,emp_r,emp_u)
int *emp_n,*emp_w;
int *emp_r;
char *emp_u;

OMG! what book are you reading anyway. We haven't coded like that in over 20 years, since K&R. Your compiler will like you a lot better if you code it like below.

input (int *emp_n,int *emp_w,int *emp_r,char *emp_u)

what do you mean "it failed" ? what compiler are you using? what were the errors. Post the code that failed.

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

2) Is there any special reason of- Why most C compilers include most of the header files
implicitly, but in C++ we have to include them explicitly?

I don't think that is the case. You have to include header files in C also. Many (most) system headers include other headers.

3) Can a global variable in C be declared anywhere in the program(outside all functions)?
ex-

    int i;
  main()
  {  
  /*body*/
  }
        int j;
  test()
  {  
  /*body*/
  }
        int k;

Is this code valid according to C standards?

yes, but remember that the variable has to have been declared before it can be used. That means function main() cannot use variable k.

4) Is the value of NULL=0 according to C/C++ standard?

c++ yes, C no. In C it can be(void *)0, and I've even seen several other variations on that depending on the compiler.

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

I don't think the arguments are right. see function q_sort() for correct arguments, where count should be the number of valid rows in the array to be sorted.

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

In the OPs case he doesn't want to use calloc() because the array is not being dynamically allocated. You may have had problems with memset when the second parameter used sizeof(some pointer) -- which always return 4 on most 32-bit compilers.

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

Oh I think I know what is wrong. you probably have a *.c file -- mine is *.cpp. Change as shown below in red. And you don't need any of those other extraneous lines in that function.

void init_list(struct CdRecords cdDB [])
{
	memset(cdDB,0,datasize*sizeof(struct CdRecords));
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

post that function! check the name with the name of the structure at the top of the program. They must match.

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

1. check the spelling -- just copy that line that I posted and past it into your program.

2. Just do it like this. you don't need to change the sort function at all.

void sort_critical_count(struct CdRecords cdDB[],int ch)
{
     system("CLS");
	 int count = 0;
 int nRows = find_free(cdDB);
     naive_sort (cdDB,  nRows, & count);

At the end of main() change the do-while loop to exit when ch == 9 instead of 8, so that you can exit the menu.

} while(ch !=9);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1. function init_list is not clearing all the fields in the structure. There is a very easy and quick way to do that with only one programming line and no loops

void init_list(struct CdRecords cdDB [])
{
	memset(cdDB,0,datasize*sizeof(CdRecords));
}

2. The reason you see gibberish after sorting the array is because the sort alborithms attempt to sort all rows of the CdRecords, even though the array may contain just a few valid rows, however many you type in. function find_free() gets the row number of the next unused row, so all you have to do is pass that to the sort algorithm and have it sort only that many rows.

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

you will probably have to write your own conversion functions that compress those 32-bit characters into 16 or 8 bit characters. But that may not work if the data requires all (or most) 32 bits to store each character, such as needed by many of the eastern languages (Chines, Japanese, etc).

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

I got everything working, a couple of probls with file write. But ill work it out. I just need help on getting the quick sort working here...i got it written and even in my switch statement but for some reason it dont work....ne one know y...??

what "doesn't work" -- compiler errors? if yes, what are they. runtime errors? describe them.

nabil1983 commented: Thanks For All YOur Help +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are several errors in your program. The first error is in swapCust().

t1 = x[a].custid;

custid is an int array, so attempting to assign t1 like that is illegal. you have to get a specific element, like this, where i contains the element number you want.

I guess the intent of that function is to swap the entire structure. That can be easily done like below, where it is not necessary to swap the individual structure members, such swap the entire structure at one time.

void swapCust (CUSTINFO x[], int a, int b){
	   CUSTINFO temp = x[a];
	   x[a] = x[b];
	   x[b] = temp;
}

Now do the same thing with function swapTrans().

Why do those two structures contain int arrays ? Your program is attempting to use them as if they were simple ints. I think they should look like this:

typedef struct{
       int custid;
       int balance;
}CUSTINFO;

typedef struct{
       int transid;
       int custid;
       int trans;
       int amount;
}TRNSINFO;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Of course. I hadn't seen that in the cplusplus.com reference for cstring. In fact, cplusplus.com has apparently no documentation for substr at all.

substr() is not a function of cstring -- it is a method of c++ std::string class.

#include <string>

int main()
{
std::string newstr;
std::string str = "why,hello";

int pos = str.find(',');
if(pos > 0)
  mewstr = str.substr(pos+1);

return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
#include <iostream>
#include <string>

using namespace std;

typedef struct data
{
	string dat;
} DATA;

typedef struct name
{
	string na;
	string age;
//	NAME * DATA;
        DATA* nm;
}NAME;

If your intent is to have an object of type DATA inside the structure named NAME, then do as shown in red above. Avoid making object names the same as structure of class names -- a lot less confusion.

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

I don't use c++ streams for UNICODE for the reasons you describe -- its a lot easier to use C's FILE, fopen() in binary mode, fread() and fwrite(). You don't have to worry about conversion that way. That works providing you don't want to transport the file from one operating system to another and you don't want to use another editor such as Notepad.exe to read it.


If you still want to use wfstreams, you can use mbstowcs() to convert from char* to wchar_t*, or wcstombs() to convert the other direction.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int CO[0] = { 0 };

The above is not a valid array -- its an array of 0 elements. that means "CO[0] = 1;" will produce undefined behavior because CO[0] does not exist.

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

if you want a binary file, when you are reading/writing it wrong. Notice how much easer this is than writing/reading in text mode?

// write a record
fwrite(&myDB[i], 1, sizeof(MyDB[i]), fp);

// read a record
fread(&myDB[i], 1, sizeof(MyDB[i]), fp);

Does your program even compile correctly? It looks like you have functions inside functions, and possibly undeclared variables -- like cd.

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

In C and C++, arrays are stored in memory by row. That is, all the columns of the first row appear first, then all the columns of the second row, etc. If you have an int array with 2 rows and 5 columns then the location of any given row can be caluculated by this formula

int *p = array + ((row_numer-1) * NumColumns)
int array[2][5];
// set a pointer at the beginning of row 2
int* ptr = (int*)array + (1*5);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I would use getline() to read the entire line, then call string's find() method to locate the spaces.

std::string line;
while( getline(file,line) )
{
   while( line.length() > 0)
   {
      std::string word;
      int pos = line.find(' ');
      if(pos > 0)
      {
          word = line.substr(0,pos);
          line = line.substr(pos+1);
      }
      else
      {
          word = line;
           line = "";
      }
       do_something_with_word(word);
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

VC++ 6.0 is SO old that it shouldn't even be taught in school anymore. There are much better compilers for learning the C and C++ languages, such as Dev-C++.

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

how do i read all the records from the file into memory???

use normal file read functions. If you have never done that before you need to read some tutorials about file i/o. In c++ it might go something like below. After that, you will have an array (vector) of structures.

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

struct person
{
   char name[80];
   int age;  
   char address[126];
   char city[80];
   char state[3];
}

int main()
{
  vector<person> people;
   person record;
   ifstream in ("person.dat");
   if( in.is_open() )
   {
      while( in.read((char *)&record,sizeof(record)))
      {
               people.push_back(record);
      } 
      in.close();
   }

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

whenever i compile i keep getting an error saying identifier or declarator expected....for some reason i cant correct it neone know whats wrong???

Yes -- count the braces in function fileprint(). There are too many closing braces.


The easiest way to sort files is to read all the records in memory, sort them in memory, the rewrite the file in sorted order. When the file is too large to read into memory all at one time then the sort algorithm becomes very complicated. You have to sort the files in small sections, then do merge-sorts on each of the sorted sections.

Once the file has been sorted, its not all that difficult to search for specific record. If the records are fixed-length, then you can use binary sort algorithm. You can use binary sort on variable-length records too, but you will have to first construct an array of offsets to the beginning of each record.

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

Oh...found the VS 2003.NET....

Which one is the latest actually? Visual Studio 2005 or Visual Studio .NET 2003?

You REALLY REALLY need to start reading www.microsoft.com site -- they constantly give you the latest scoop on all their compilers. Now let me see -- which is more recent compiler, 2003 or 2005? If you can't figure that one out then maybe you shouldn't be using any of them :rolleyes:

And yes, Visual Studio 2005 is for .NET.