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

Here is one way to do it.

And here is another method

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

hello???
can you give me a complete program about a fraction calculator???

No, use your brain and code it yourself. That's a lot more fun than plagiarism.

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

maybe you want npc.erase(iter); When you do that I have found that you need to start the loop all over again because the value of iter is invalidated.

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

>>I was just asking about aslo showing the correct option alongwith the msgbox "INCORRECT". e.g. if option A is correct & player clicked option B, then game should display msgbox "INCORRECT" and also msgbox "Correct option is B".

Well, just format a string to say "INCORRECT. The correct option is A" How you know whether the correct option is A or B will depend on other variables in your program.

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

Its better to just attach the source code to your post here than to post links to some jpeg file. To do that, just click the "Go Advanced" button then on the next screen "Manage Attachments".

If you want to use the code I posted then delete aht loop just above it. Either that, or ignore the code I posted and just add infile >> stuff; after reading the last integer so that your program also reads the string at the end of each line.

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

just as you did with structure menber int ident you have to create an object of type NPC_det

struct NPC
{
	string name;
	int ident;
	struct NPC_det
	{
		int health;
		int attack;
		int def;
	} det;//end 2nd struct
}//end 1st struct
int main()
{
    NPC n;
    n.det.health = 1;
}
Suicidal_tool commented: Thanks for the quick reply! +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In a loop only display a certain section of the string at one time. Like this, where str is std::string with the contents you want to display

for(size_t i = 0; i < str.length()-9; i++)
    {
        cout << '\r' << setw(10) << str.substr(i, 9);
        Sleep(100);
    }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

This thread seems to be a good discussion of that topic.

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

First step is to just write a program that asks you to enter a value, then use cin to get the value from the keyboard. Once you have that going (compiled without error and runs correctly) you can add the other stuff to it, whic to write a function that contains that formula and returns the result. The function needs one parameter -- the value of x.

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

How to put keys into the keyboard buffer depends on the operating system.

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

I was looking forward to find some help in this regard from the expert guys but I think it's difficult to get an idea here.

The answer to the second question is "Yes" -- but there's a catch. YOU have to code your program to do that because there's nothing in MFC or c++ that will do it for you.

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

The only time I've seen that to be a problem is with MFC programs. Otherwise you should be able to debug your own code without a problem.

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

Are those your DLLs? Or do you have the source code so that you can recompile them with debug information?

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

I would move line 18 down after line 20 so that the sum is only printed after you enter -999. That's where you calculate and print the average too.

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

Drop the course and take C++ Programming 101 course.

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

>>int sum(x);

That is just initializing a variable named sum to be the same as the value of variable x. That is not what you want. sum needs to be the total of all previous values plus the value of variable x. sum was already declared at the beginning of your program so you don't have to declare it again.

That line should be like this: sum = sum + x; , or another way of putting it sum += x; . Either way is correct, so you can choose either way of coding it.

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

<deleted>

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

The last column in that file is a string which also must be read

int i = 0;
string stuff;
while( infile >> oldcases[i][0])
{
    for(int j = 1; j < 6; j++)
          infile >> oldCases[i][j];
    infile >> stuff; // read the strings at the end of each line
    ++i;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1) Simple 4th grade (probably) math. Just add up all the values in the array and divide by the total number of values.

2) use cout to display then one at a time inside a loop.

Exactly how to write the code for the above is up to you. The solution to 2) is only one like of code.

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

Also, you don't need that x++; line -- just delete it.

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

thanks it worked. now i just needed to know how do i go about stopping the program if the user inputs -999 do i need and if else statement or can i change something in the loop that will make it stop if x=-999.

Look at that while statement -- when someone changes the value of x to -999 then the while statement will stop and program execution will jump to the next line after the end of the while loop.

secondly how do i create a function to calculate the sum, average, count, smallest and largest numbers entered?
thank you

Its not a function. Just keep track of the sum, count, smallest and largest numbers inside the loop, just after the cin >> x; line. The average is calculated after the loop finishes, which is average = (float)sum / count;

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

The easiest way to read the numbers into an array is like this:

float gradeData[12] = {0.0F};
int i; // loop counter
ifstream myfile ("rainfall.txt");
if( myfile.is_open() )
{
    i = 0;
    while( i < 12 && myfile >> gradeData[i] )
            i++;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Apparently Win7 is easier to play than previous versions. Here is my current stats

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

The folks at the NSA will pick your super secret password in less than a second. .

I doubt NSA will be interested in looking into my computer, so that's not any threat to me. If you have information that is super secret or sensitive then put it on an external hard drive and unplug it when not needed (which is what US military used to do when I was on active duty). If you are using a business computer then of course that is a more serious and different problem.

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

You might try to compile that with another compiler that supports template specialization. The last I know Microsoft compilers did not support it, but that could have changed with VC++ 2008.

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

Don't know if there is a managed code to do it or not, but here is how to do it with normal win32 api functions.

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

>>while(x!=-999);
Remove the semicolon at the end of that line.

Next, when there are multiple statements in a loop the loop must be enclosed in { and } brackets

while(x!=-999)
{
   // blabla
}

Also, you will need to initialize all those variables to -0 so that they do not contain just random values. For example int sum = 0;

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

Get it as a string, then parse the string to find out what kind of data it contains.

[edit]Oops! Too late. Like ^^^ said.

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

Hi Mr.Ancient Dragon,
I want seriously give you some thing.. read my private message!
And that its nothing if we compare it with your help :)

As I said in my PM to you, I do not accept payment for any problems I solve here at DaniWeb. But thank you anyway.

Aia commented: Here's some payment in proper currency +8
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can't just simply swap nodes (see lines 24-31 of the code you posted) of a linked list because all the pointers will be destroyed (invalidated). A better way to swap nodes is to swap the data, leaving all the pointers in the node structure unchanged.

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

delete lines 18 and 33 -- they serve no purpose.

For leap years, read this thread

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

Start here. But don't expect you will be productive for a few years.

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

plus windows 7 is to bulk'y

Bulky compared to what? If the computer has sufficient ram and hd space who cares how bulky it is? My PC has 5 GIG RAM and 64-bit Win7 runs just fine with it.

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

Code below should at least partially solve the problem. There are several ways in which a UNICODE file can be encoded, and the first 2 to 4 bytes of the file will describe which kind of unicode encoding it is. Winipedia has a good article that describes all of them. The code below only implements the UTF-8 encoding, which is really just standard ascii characters. You will have to write the others if you want to implement them.

Unlike my previous attempt this version works ok then the file is opened in text mode.

Note that you will not see "Yes" printed on the screen because the file does not contain any blank lines.

As it turns out, the file you posted does contain the BOM but is otherwise just a normal ascii text file, which is the definition of UTF-8.

void ReadUTF8(FILE* fp)
{
    unsigned char iobuf[255] = {0};
    while( fgets((char*)iobuf, sizeof(iobuf), fp) )
    {
            size_t len = strlen((char *)iobuf);
            if(len > 1 &&  iobuf[len-1] == '\n')
                iobuf[len-1] = 0;
            len = strlen((char *)iobuf);
            printf("(%d) \"%s\"  ", len, iobuf);
            if( iobuf[0] == '\n' )
                printf("Yes\n");
            else
                printf("No\n");
    }
}

void ReadUTF16BE(FILE* fp)
{
}

void ReadUTF16LE(FILE* fp)
{
}

int main()
{
    FILE* fp = fopen("c:\\dvlp\\test_utf8.txt", "r");
    if( fp != NULL)
    {
        // see http://en.wikipedia.org/wiki/Byte-order_mark for explaination of the BOM
        // encoding
        unsigned char b[3] = {0};
        fread(b,1,2, fp);
        if( b[0] == 0xEF && b[1] == 0xBB)
        {
            fread(b,1,1,fp); // 0xBF …
Nick Evan commented: Excellent posts in this thread! +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No, but this will:

int (*function)() = &NewOpcodeInterpreter;

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

>>There are no checks against failed memory allocations.
Because its not really necessary on today's computers with lots and lots of memory, unless of course memory has been corrupt. I haven't seen malloc() return NULL since the days of 16-bit MS-DOS or Win95. If the computer doesn't have enough memory for malloc() then there are many many more problems to worry about.

>>I might suggest removing the leading underscores -- that's the implementor's namespace.

Agree -- its also a common way to indicate that a function is part of a library (*.lib or *.a)

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

>>My reasoning is, I have to replace the relative jump with an absolute jump, to the address of my own function.

Bad idea. Why? Because the address in memory will change every time the program is loaded by the operating system. Therefore you will never know the address of your function in memory. And the byte offset to the beginning of a function in the *.exe file is not really relevant because it too will change when loaded into memory.

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

The problem is how it is displaying the data. Line 23 prints the array before it has been sorted.

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

Hi there,your case is a common one; i tried your code but used int main() instead ofvoid main() and i got 123 as my output. This is why good old coders say" use int, never use void" for your main()!!

void main() has nothing to do with this problem. The problem is with the logic of the loop, not how main() was declared. But you are right -- it should be int main()

what if i have many variables to read

A couple ways to do it

while( inf >> a >> b >> c ... )
{
}

If a lot of variables

while( inf>> a)
{
    inf>> b >> c ...
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The loop is wrong. eof() is unnecessary and its causing the problem.

while( inf >> class)
{

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

GUI depends on the operating system. You can use os-independent classes such as wxWidgets or QT that work on both MS-Windows and *nix. Maybe works on MAC too, I don't know.

>>Are they hard to implement and use?
Yes.

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

>>this code will unblock the command prompt of any system that has been disabled.

Not likely! If the operating system is disabled (locket up -- stalled -- crashed) then how is your batch file going to run? The only way to unblock such a system is to reboot the computer.

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

hi is there any way by which we can write a program that displays its own source code???????:?::-/

Of course, just open the *.c file, read each line and print it on the screen.

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

Also see the code snippet I just posted in the C forum (afterall its a C program, not C++).

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

The idea for this came from another thread in the C++ forum that wanted to duplicate the _getdelim() function that is supported by GNU compilers g++. I made one major change in that this version does not allocate new memory every time it is called, which is grossly inefficient. Instead, this function only allocates new memory if the pointer to pointer passed to _getdelim() is NULL. If not NULL then it assumes the calling program has allocated the memory. The function might expand the size of the line buffer if the number of characters read exceeds the length of the line buffer.

I also removed the goto statements that are in the original GNU function and the ancient K&R style function parameter declarations.

This code snippet uses only standard C functions -- nothing compiler-specific, so it most likely will compile with any 32-bit or 64-bit compiler.

jonsca commented: Nice! +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is a version that compiles with VC++ 2008 Express, and seems to work ok with the file I tested it with (which is the source code of this program). I have no clue what the second parameter to that function is supposed to do, so this program just ignores it.

The function allocates new memory for lineptr every time it is called, which is probably a waste of time. It might be better if the calling function allocated the memory once and passed the pointer to the allocated memory. That would be a great deal more efficient.

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#pragma warning(disable: 4996)

/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
   (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
   NULL), pointing to *N characters of space.  It is realloc'd as
   necessary.  Returns the number of characters read (not including the
   null terminator), or -1 on error or EOF.  */
static __inline void __set_errno(unsigned int no)
{
    errno = no;
}

size_t __getdelim (char** lineptr, size_t* n, int terminator,FILE* stream)
{
    int c;
    size_t len = 0;
    size_t linesize = 0;
    const size_t BLOCKSIZE = 255;
    if( lineptr == NULL || stream == NULL || n == NULL)
    {
        _set_errno(EINVAL);
        return -1;
    }
    linesize = BLOCKSIZE;
    *lineptr = malloc(BLOCKSIZE);
    while( (c = fgetc(stream)) != EOF && c != terminator)
    {
        if( (len+1) == linesize)
        {
            linesize += BLOCKSIZE;
            *lineptr = realloc(*lineptr, linesize);
        }
        (*lineptr)[len++] = c;
    }
    if( …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I hav run the code specified in the link.
It is showing error as "ssize_t is an undeclared identifier"
what content shouls v write in "getdelim.h" header file?

It is misspelled it -- it should be size_t

I just tried to compile that with VC++ 2008 Express and found out that there are several g++ specific functions in it, such as __validfp() and __set_errno(). Also the FILE structure is different, g++'s version of FILE has members that VC++ 2008 Express does not. It would appear to be easier to just write your own function than try to bastardize that one.

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

@freesif: Are you sure that file contains valid data? The file's contents are as shown in the attached. It shows each byte of the file.

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

>>CRectangle * d = new CRectangle[2];

That is just allocating an array of 2 CRectangle objects. It could have been written like then: CRectangle d[2]; . But if it did that then it could not replace the 2 with something else at runtime.

>>d -> set_values(5,6);
That is the same thing as d[0].set_values(5,6) only it uses -> operator. When using an array I prefer d[0].xxx instead of the pointer operator because it more clearly describes what that code is doing.

chaienbungbu commented: Thanks. +1