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

OMG! what did you do in lines 10 thru 35? Delete all that crap and use arrays of structures as your assignment told you to do and as I explained in my previous post.

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

after line 65 you should clear the prson array by setting everything to 0 -- you can use memset() to do that very quickly

memset(prson,0, sizeof(prson));

to search for a given person you will, of course, first have to ask for the person's first and last names. Then create a loop and look at each prson object to compare last and first names in the structure with what you entered. you can use strcmp() to do that. In this code snippet assume i is the loop counter, lname is the last name you entered from the keyboard and fname is the first name you entered from the keyboard. Since strcmp() returns 0 when the two strings are identical you can test like this:

if( strcmp(lname,prson[i].lname) == 0 && strcmp(fname,prson[i].fname) == 0)
{
   // blabla
}

To delete an element from the array just memset() it to 0 again

memset(&prson[i], 0, sizeof(struct person));

Use of gets(): such as in line 31. Use fgets() instead because gets() will allow you to enter more keys than the buffer can hold. For example if you enter 30 character for last name then the program will crash because gets() will scribble all over memory with the extra characters. fgets() does not have that problem because you tell it the maximum number of characters that the buffer will hold.

Line 30: you are using variable i before it has been initialized with anything. All it contains at that point is some random …

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

>>“Write a C++ program (use multi-dimensional arrays) that uses an array structures

Where have you declared the structure ? and where did you declare the array? You need a structure something like this:

struct company
{
    std::string company_name;
    long Quarter_sales[4];
    long Total_annual_sales;
    long Average_quarterly_salesl
};

lines 45 and 46: Divisions and Quarters should be combined into one structure something similar to what I posted above.

lines 148-155: delete those variables. The calculations should be stored in the structure members not in simple individual int objects.

lines 193-199: That should be sorting the array of the structures similar to what I posted above. Sorting an array that only contains the averages will not give you what you want because it doesn't keep the other information in the same order. Putting everything in a structure similar to what I posted previously and sorting that array solves that problem very easily.

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

you have to use os-specific functions. What compiler and what operating system ?

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

MSDN is free at www.microsoft.com and can not be downloaded. If you need a local version you have to buy it for about a thousand $$$.

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

maybe you need to get the newest version of that compiler ??? I don't use it, so don't know.

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

maybe this ?

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

Compiled without errors/warnings with my compiler -- VC++ 2005 Express as both a C and a C++ program. What compiler are you using ? Does your compiler complain about the below ?

char* foo1()
{
    return (char *)malloc(255);
}

int foo(void **vpSec00)
{
    
    *vpSec00 = foo1();
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

in the command prompt window type the name of the program you want to run and press <Enter> key. You may have to change directories to where the program is located if it is not in the PATH list.

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

did you check the return value of bitio_o_open() ? Maybe its returning NULL (or 0) ? If that is ok, then my bet is the problem is somewhere else. Does bitio_o_append() work correctly? How about the function that calls CreateHVFESection0().

Does your program use structures? Are they declared correctly in all program units ? Also check for array bounds errors and buffer overflows, which will both cause wierd problems in unexpected places.

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

>>What's the difference between sending malloced pointer address and sending variable address?
Efficiency mainly. malloc() requires a lot of time and some overhead.

>> Then, when do we need mallocs?
When you don't know the size of something at compile time. For example if you need an array of structures but don't know how many until the program is executed. Also when the size of an object is just too big to be allocated on the stack.

>>size_t nbytes = NULL
No, NULL is used for pointers, not integers. size_t nbytes = 0; >> bitio_o_append(hSec0,0,24);
That line is wrong, the 3d parameter should be sizeof(LONG). Never assume a LONG is a specific size -- always use the sizeof operator to make your program more portable and correct. And the sizeof(LONG) is not 24 in either 32-bit or 64-bit systems, so I don't know where you get the value of 24 from.

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

some questions:
Function CreateHVFESection0().

1. Why is vpSec0 a parameter? It could just as easily be declared locally in the function since it is never used for anything.

2. Why do you bother to malloc nbytes since sizeof(size_t) is only 4 bytes in 32-bit compilers ? Just declare it normally and used the address operator & when used near the bottom of the function.

3. Your function has memory leak because it does not free the memory location returned by bitio_o_close(), as indicated that it needs to do in the function description that follows. You need to add another line free(vpSec0); 4. Since we know nothing about function bitio_o_append() could you please post its function prototype like you did with bitio_o_close()? Maybe the parameters you are sending it are incorrect of wrong type.

I think something has screwed up the stack before calling bitio_o_close(). I would start by commenting out all the calls to bitio_o_append() then rerun and see if the bad pointer problem is corrected. If not then most likely the problem is somewhere else.

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

I didn't get any problems after fixing up the floats, doubles and shorts problems. Also note that main() ALWAYS returns an int. Declaring main() as void may be acceptable to some compilers but it is non-standard.

int main(void)
{

	float net_pay[MAXA] = { 44.0F, 22.0F, 45.88F, 333.467F, 64.77F },
		  integer_part = 0.0F, fractional_part = 0.0F;
    short int x = (short)net_pay[4], Val=0, i=1;
	int cents;

	printf("The Sum ");
	
	fractional_part = modf(net_pay[4], &integer_part);

	cents = (int)(fractional_part * 100.0);
	if( (fractional_part * 100.0) >= (float)cents + 0.5F)
	{
       cents++;
	}
	printf("and %d/100 Dollars\n\n", cents);
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> Could you explain what F means in 0.5F?

F = Float. Without it the default is double and some compilers will produce warning that attempting to assign a double to a float.

>> compiler complains only about integer_part variable
what are the compiler errors/warnings ?

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

1) post code. I can't see your monitor from where I sit :)

2) check the 3d decimal place greater than 5. This will probably work, although tested only with the Vista Calculator program.

if( (fractional_part * 100.0) >= (float)cents + 0.5F)
          cents++;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use modf() to extract signed integral and fractional values, then multiply the fractional part by 100 and assign it to an integer

#include <math.h>

<snip>
float fractional_part;
float integer_part;
float number = 999.99;

fractional_part = modf(number, &integer_part);
int cents = (int)(fractional_part * 100.0);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

look at lines 31 and 33 -- delete that semicolon at the end of those two lines.

>>If I were to do the following, why does it only show the information for Kenez from my inFile?
Only displaying the last line of the file because of the problem with the semicolon on line 33.

You really start to be more careful about where you place those semicolons.

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

I don't have a problem any more prettying-up people's code. But I'll be darned if I'm going to fix all those color and font tags that are in some people's posts. The A button in the upper-left corner of he message box only works when there are matching close tags for each open tag. I would like Dani to have a script that forbits color and font tags, or just quietly remove them when posted.

iamthwee commented: Ever heard of regex, use it to create a generic expression to remove font tags etc? -2
~s.o.s~ commented: I am sure AD has seen more books than the regexes you have crafted Iamthwee... +25
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A nice PM does indeed work wonders -- at least it did for one member. Some people, like him, just didn't understand how code tags worked. The use of warnings and infractures would probably have been counterproductive. Now his most recent post this morning contains correct code tags.

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

. Looks like the mods are at least trying now. Now why didn't they do this sooner? :)

we have -- maybe you just were not paying attention.

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

>>So I don't see what the problem is?
The problem is that I don't want to manually space out all those lines.

But I found a happy solution Here is an on-line code beautifier that seems to do the trick quickly and effortlessly. Just past the code into the edit box and press Submit button.

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

>>The moderators instead should be more diligent, and take the time to not only add code tags but also properly indent it for others to read

Remember we all do this for free -- none of the mods are paid employees. I normally don't add code tags to code that has not been indented. I first hit the Edit button then look at the code. If its intented then I add the code tags, otherwise I see no point in using code tags. I think its asking to much of us to reformat the code according to OUR formatting standards, and we all probably have different code standards. Then I can see somebody suggesting to Dani that we need standard code formatting rules. No thanks.

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

But can't you just remove formatting with the top-left button in the edit window that looks like a slanted 'A'?

It did nothing to the following example I cut out of another earlier post.

from datetime import date

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

>>And why would they when there are plenty of lap-dogs to clean up after them

:angry: I resent being called a "lap dog"

>>Unless you put some serious inconvenience in the way, they're just not going to learn

Learn what? how to use code tags? Yes, I've given out a couple warnings about it, but I don't think it was worth the effort. I would rather just quietly add code tags (or correct them) rather than lose a DaniWeb member. Failure to use code tags is not really all that big a deal. What IS a big deal are people who use font and/or color tags on every line of their code. I don't bother to correct them -- don't have the ambition to edit all those lines.

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

Maybe -- depends on what api functions you use. When unsure what version of the os a function supports, look it up in MSDN and it will tell you. There are many functions that are either unsupported on older versions of Windows or does not exist at all. For example, UNICODE is not supported in Win95/98 so if you compiled your program for UNICODE it will not run on either of those versions of MS-Windows.

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

Here is a popular tutorial. win32 api is C, not C++. You can create c++ programs that call win32 api but it isn't necessary. MFC and wxWidgets are two c++ gui libraries. wxWidgets is free and works with most compilers and operating systems. MFC is not free and restricted to MS-Windows.

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

what you are doing is attempting to print the binary value of the number. It has to be converted to ASCII before it can be printed. If you look at an ascii chart you will see that the ascii value for '0' is 48 decimal, or 30H. So if you want to print the 0 digit you have to add 48 to make it printable on the screen.

using a loop your program needs to split the value of sum up into its individual digits and add 30H to the digit before printing it.

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

You don't need OnMenuSelect(). Just use ClassWizard to implement event handlers just as you do the menu items in the main menu.

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

>> why the pointer value is allocated only 4 Bytes
Not necessarily -- depends on the operating system and the compiler. In the days of 16-bit MS-DOS pointers were 2 bytes. On 64-bit MS-Windows pointers are 8 bytes.

The maximum number of nodes in a linked list would be limited only by memory. True, you may not be able to count more nodes than can be stored in an unsigned long integer (on 32-bit computers) but the list could certainly hold many more nodes than that assuming there is enough memory.

The file limits.h gives the maximum values of many objects.

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

you can not use switch with strings.

>>word = getchar();
Don't use getchar() is c+++ programs. use cin.get() instead.

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

Every resource item must have a unique ID number, both the resource files in your project and resource files in other DLLs. You can use LoadMenu() to load a menu resource that is not normally part of the project. Also read the links here for how to create a shortcut menu.

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

post complete code -- did you add the lines exactly as I posted it?

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

Is this supposed to be a C program or VB? I moved this thread into the C board because the code posted in the original thread looked like C code, but maybe I was wrong. Aia's post looks like VB because that's the way it would be coded in VB. If this is about VB then just say so and I'll move it again into the correct board.

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

That's still only one level of nesting -- two levels of nesting would be something like this:

if condition 1   // first nexting level
{
   if condition 2  // second nesting level
   {
     // do something
   }
} else if condition 3  // back to 1st nesting level
{

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

Did you post the EXACT question and code because that code will not compile due to several syntax errors.

>>What will be printed if the user enters “Wingding”?
Nothing because there is nothing in the program to accept anything from the keyboard.

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

The program will not stop if there are still keystroks in the keyboard buffer, such as if you previously called scanf() to get an integer the '\n' will still be in the keyboard buffer. But you will have to post your program for anyone to give you more help with this.

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

Of course it didn't show anything because you have to add a line or two to print the value of the variables to the screen, something like this:

cout << "x = " << x << "\n";
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>what do you think will happen to the values of x, y and z?
why don't you just compile and run the program then you can find out for yourself instead of guessing.

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

You should not hardcode the size of the name array as shown on line 18 but use the sizeof operator so that if you change the size of the array you don't have to worry about changing it in more than one spot.

cin.getline(newcustomer->name, sizeof(newcustomer->name));

or like this

cin.getline(newcustomer->name, 
   sizeof((struct customerType*)0->name));
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
newcustomer=new customerType;
cout<<"Please enter a name: ";
cin.ignore('\n');
cin.getline(newcustomer->name,21,'\n');

for example of what it does say I enter "john smith" minus the quotes, the output on the screen is just the 'h' at the end of smith nothing else.

post the customerType structure -- how did you declare name? Since this is c++ why don't you use std::string instead of char*?

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

Can't you instead use gets() function?

No -- gets() is C not C++ and its terrible to use in C code too.

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

is this what you are looking for ?

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

just write a little prgram that generates 1000 numbers between 10 and 100 using rand() % 100 + 10 to generate the numbers and save them to a file. Then you can write the other program as your teacher instructed.

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

And others will give you 6, 6, 5 like it should be.

that may be the way we humans think about it, but not compilers.

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

The correct answer is that the result is undefined, or unpredictable. It depends on the compiler -- the answer by some compilers may be 5,5,5 because only the last version is stored. Yet other compilers, such as VC++ 2005 Express will give 6,5,5.

Aia commented: Thanks for explanation. It opened some good teaching time. +6
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Since gradeAvg is the average for the student just read, put that statement immediately following the while keyword -- you will have to add braces around the while loop because it will now be multiple-line controle.

while( infile >> <snip> )
{
    int gradeAvg = ...
    if(gradeAvg>=90)
    {
         <snip>
     }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

also you should use the space bar on your keyboard to format the code better so that it is easier to read, and you may also get a better grade.

And you still have those damed || conditions in the if statements :icon_eek: Your program will never work until you fix that. For example lets assume the grade is 60, your program will give him a B instead of a C. It doesn't really matter what the grade is -- any grade below 90 is a B, even a grade of 0 :-O

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

To show the total number of A's, B'c, etc you need a counter for each grade and expand those if conditions to tally up the number of people with each grade For example:

int NumA = 0;
int NumB = 0;
int NumC = 0;
int NumD = 0;
int NumF = 0;

if(gradeAvg>=90)
{
    NumA++;
    outFile<< "A";
}

now convert line 26 into a while loop and put all that inside the loop

int NumA = 0;
int NumB = 0;
int NumC = 0;
int NumD = 0;
int NumF = 0;
while( inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)
{
    if(gradeAvg>=90)
    {
        NumA++;
        outFile<< "A";
    }
    // remain if conditions go here
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The values in those if conditions (line 36) are incorrect. A grade can not be both > 90 and < 89 at the same time! And if someone gets 100% then your program will give him no grade at all. Line 36 should read simply >= 90. The conditional statements in your original post seem to be correct.

As Salem pointed out all those semicolons will cause huge problems. Edit your data file and remove them.

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

2 dimensional character arrays can be used to store any list of text items, such as a list of people's names, a list of addresses, telephone numbers, email addresses, etc. If you want a list of 3 names and each name can be a maximum of 20 characters then you would declare the array like below -- since c style strings must be terminated with a NULL character -- '\0' -- you must add one extra byte to the maximum width you want, in this example declare the width as 21 characters.

char names[3][21];