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

The program in post #9 works ok for me after making one small change. You are calling the sort function twice, and not displaying the results after sort is done.

cout << "Here is the sorted values\n";
    for(int i = 0; i < MONTHS; i++)
        cout << values[i] << " ";
	//selectionSort(values, name, MONTHS);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you forgot the case keyword. Your program contains millions of other errors that you need to correct -- if your compiler doesn't report them then toss out that crappy compiler and get one that actually works right.

int area(shape *s)
{
 switch(s->st)
    {
        case RECTANGLE:
            return (s->shape_u.rect.side1) * ( s->shape_u.rect.side2);
        case SQUARE:
            return (s->shape_u.squa.side) * (s->shape_u.squa.side);
        case CIRCLE:
            return (int)(s->shape_u.circ.radius * 2 * PI);
        default:
            return -1;
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The extern keyword must be used in header files like global.h to avoid duplicate declaration errors when you include the header file in two or more .c files.

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

Question 1:

main()
{int i=-3,j=2,k=0,m;
m=++i||++j&&++k;
printf("\n%d%d%d%d",i,j,k,m);
}

ans is -2201 how?

There are no spaces in that printf() statement to separate the numbers.

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

What operating system? If MS-Windows then use win32 api mouse functions.

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

>>In the case of 'c++' the VM (Virtual Memory) and PM (Physical Memory) becomes larger every minute.

Memory leak. Check your program for memory allocations that were not freed before a function ends. Quite possibly in pPaymentAcsPtr->CheckCardBefore() since your program is calling that function 150,000 times :icon_eek:

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

Functions know nothing about variables declared in other functions. If you want selectionSort() to use variables in another function then those variables have to be passed to selectionSort() as parameters.

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

>>Mm..I'm not sure how to edit my post?
There is a time limit. You have 30 minutes to edit a post.

>>I still can't figure out how to read 24 lines at a time
Just put getline() in a loop. Note that testing end-of-file with eof() is not necessary nor even desirable because eof() will let your program loop once too many times.

for(int i = 0; i < 24 && file.getline(fileName, SIZE); i++)
   ;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What you are already doing should be ok. A semiphore is just another way of doing it.

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

If you intend to modify Andy's contents from different threads then you need to synchronize the threads so that access to Andy is limited to one thread at a time. The most common way is via semiphores.

For platform independece -- you might check out the Boost libraries to see if it contains something to create and synchronize threads.

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

The is setting up the function's stack, allocating local variable space on the stack, and initializing them with 0x0c for debug purposes (helps to detect buffer overflows). Similar code is at the beginning of every function.

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

Hint: See the example program at the end of this article

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

wtf??? FoxPro and VS are two completly different things. FoxPro is a database, VS is a compiler.

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

Posting an up vote is not going to erase that down vote. You will still have the down vote.

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

No. color must be a pointer.

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

use std::string's c_str() method

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

I don't know about other languages, but in c++ I think it is always possible to remove it, because there is such a thing as namespaces and function overloading. Often an "ambiguous" error message means there is a programming error in the parameters (such as mis-matched data types) and the compiler doesn't know which of several overloaded functions/methods to call.

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

Probably the most efficient method that I could think of (in terms of line of code) would probably be something like this:

the replace() function may make your code smaller, but efficiency is probably the same. Somebody has to loop through the string one character at a time. Whether you do it or replace() doesn't really change the efficiency of the program at all.

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

The Seek() method returns a long long which is the length of the file

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

There are several sort algorithms you could use, the bubble sort is ok for small arrays like yours.

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

What you need is an array of character pointers. When you enter the number of strings then allocate that many character pointers

char** array = 0;
int numNames = 0;
printf("How many names will you be entering?\n");
scanf("%i", &numNames);
if( numNames > 0)
{
    array = malloc(numNames * sizeof(char*));
}

Now when you enter each of the names

int i;
char name[80];
for(i = 0; i < numNames; i++)
{
    printf("Enter a name\n");
    fgets(name, sizeof(name), stdin);
    if( name[strlen(name)-1] == '\n')
        name[strlen(name)-1] = 0;
    array[i] = malloc( strlen(name) + 1 );
    strapy(array[i], name);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is another way to do it.

string space2underscore(string text)
{
    for(int i = 0; i < text.length(); i++)
    {
           if( isspace(text[i]) )
                text[i] = '_';
    }
    return text;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can't draw to a text file. I don't know what your game looks like, but if it contains a 2d array called a Board

char Board[5][5]; 

// write to a file
ofstream out("save1.bin", ios::binary);
out.write(Board, sizeof(Board));

// read from file
ifstream in("save1.bin", ios::binary);
out.read(Board, sizeof(Board));
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>int OptionMenu(int);

Since that function is returning the option selected there is no need to pass any parameters to it. Then in main() just write menunumber = OpenMenu();

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

Now is when you need to learn how to use your compiler's debugger so that you can single-step through the program one line at a time. I already told you why your program doesn't work. The problem is in that Clear() method. Your program works ok for me when I made that change.

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

The general way to get the file size is to seek to the end of the file then get the current file position. I don't know how you would do that with CLR/C++ (managed code), but I'm certain if you read the available methods in FileStream you will find it.

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

The program doesn't work because the Clear() method destroys the value of MaxBytes by setting it to 0, which makes that Pack() return false when checking if size > MaxBytes.

void LengthTextBuffer::Init(int maxBytes)
{
	if(maxBytes<0)
	{
		maxBytes=0;
	}
	MaxBytes=maxBytes;
	Buffer=new char[MaxBytes];
	Clear();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Sorry, this was a typo - should be: double color[3] = {1,2,3}; // ... assign ptr .... meant do something like ptr = GetAValidPointer() The part I was concerned with is the following. I was just trying to set the stage (which I apparently did worse than poorly!):

*ptr = *color++;
ptr++;

Even if ptr were properly allocated, *color++ is an illegal statement because color is not a pointer.

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

What is going on in that code is called undfined behavior and will not work. Why? Because ptr is an uninitialized pointer, and dereferencing it like *ptr = (put anything here) is illegal and will most likely crash the program.


The second problem with that code snippet is *color++; on line 6. Variable color is not a pointer so it can not be incremented. The compiler should produce an error on that line.


line 1 is an error because a single integer can not be initialized with multiple values. color progably needs to be an array, like this: int color[] = {1,2,3};

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

You don't have to read the data back into a account class. You can do anything you want with the data, such as read it back into individual char arrays if you want to. The data file is not tied to any class or structure.

If you made the individual fields of the account class std::string instead of character arrays, then you would need to pack the data into fixed-length fields before writing them to the file. But as it is, the account class already contains fixed-length fields, such packing is not necessary. When you write a class to a data file only the data objects are written, not any of the methods.

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

create a union

// declare types
enum data_types {
    undefined_type = 0,
    bool_type,
    char_type,
    short_type,
    ushort_type,
    int_type,
    uint_type,
    // etc for each type you want to support
};

    

struct data
{
    data_types valid_type;
    union types
    {
         bool b;
         char c;
         short s;
         unsigned short us;
         int i;
         unsigned int ui;
         long l;
         unsigned long ul;
         long long ll;
        float f;
        double d;
   };
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is no need for the pack() functionality. The account class is already packed (fixed-length). Just write the account object to a binary file.

void write(Account& obj)
{
    ofstream out("filename.dat", ios::binary | ios:: app);
    out.write( (char*)&obj, sizeof(Account));
    out.close();
}

similar to read it

void read(vector<Account>& accountList)
{
    Account obj;
    ifstream in("filename.dat", ios::binary);
    while( in.read((char *)&obj, sizeof(Account))
    {
         accountList.push_back(obj);
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I'm assuming English is a second language for you, which may be why you have trouble asking for help.

The first thing I would do is to design a structure to hold the information for one student. Since there needs to be a linked list that structure will have to have a pointer to the next node in the list.

struct student
{
    struct student* next; 
    // Add the rest of the student information here
};

Then you will have to add prompts so that you can enter the data from the keyboard. Or put the data in a file, then use ifstream to read the data from the file.

That's enough to get you started. Now go to your computer and start writing code. Write only a small bit at a time, compile, fix errors, then write some more. Don't try to write the whole program at one time without compiling because you will get to many errors that it will be difficult to correct.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
char direction[4]; // in or out
char type[8]; // such as int, char, etc
char name[25]; // variable name
char value[10];
char filler; // just ignore the = symbol

while( in >> direction >> type >> name >> filler >> value )
{
   // do something with this information

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

Sorting becomes a lot easier if you use a structure to hold each person's information instead of individual arrays. Something like this:

struct person
{
    char name[MAX+1];
    vector<float> sales;
}

Note that amount needs to be either an int or float, but never char. Assumes you are allowed to use std::vector. If not then make it a standard array float sales[MAX];

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

1) delete lines 10-36 because they are redundent. The array counts was already populated with the correct values in line 9 and there is no point doing it all over again.


2) line 44 -- that's almost an infinite loop. On every loop iteration the value in counts changes. When i is 0, counts[0] is 97. When i = 1, counts[1] = 98, ... when i is 25 counts[25] = 122, from there on the counts is undefined because counts only has 26 elements.

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

post code. Works ok for me

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

int main()
{
    int c;
    while( (c = getch()) != EOF)
        cout << (char)c << " ";
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You don't really do it in c++ any more than you did on *nix. Here are some links to programs that will image the hard drive.

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

line 42: void main() main always always returns an int -- never void. The only acceptable way to code it is int main() or int main(int argc, char* argv[]).

line 115: The way you call a function is by putting variable names as the two parameters. What you have posted is a function prototype, and all it does is tell the compiler what type of parameters the function should have. Look at line 157, the actual function. That will tell you what parameters to pass on line 115.

If you are still confused about how to pass parameters then read one of the many tutorials on the net, for example this one.

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

I have to admit I preferred it when MFF was on every page, but speaking for myself it's no biggie having to click back to the homepage to get at it to be honest.

I don't bother going back to the main page -- just use the menu like I always did before there was MFF.

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

You need to read the answers given on other forums before starting new threads. See this link if you forgot.

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

The file was read on lines 28-30, why are you trying to read it again on lines 34-37? And line 37 will just read all the numbers into the same element of Array.

My guess is that on lines 34-37 you need to just display the contents of the array on the console screen ??? Use printf(), not scanf() to do that, and you don't need the file pointer at all there.

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

The first thing you should do is initialize BlockDataType array with all 0s before doing anything else with it -- that will clear the array of all random data. Now lin line 14 fwrite() will write 0s for all unused bytes in that array. fwrite() will write blocksize number of bytes to the file, regardless of what BlockDataByte contains. Note that the contents of binary files can not be read by text editors such as Notepad.

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

line 41: variable list needs to be an array of pointers. What you have coded is a simple character array. Declare it like this: char *list[10]; line 42: variable line needs to be a character array, not an integer. Declare it like this: char line[80]; .


line 48: There are two versions of getline() -- one for std::string and another for character arrays. What you have coded is the one for std::string. Here's how to code it for character arrays: ifile.getline(line, sizeof(line)); line 51: You can't insert a string into the array like that -- that is the std::string way of doing it. In C style strings you have to do it the hard way by first allocating memory for the string and then calling strcpy() to copy it into the array.

line 51: Variable number is undeclared.

line 69: Your use of sizeof will produce the wrong result. There are 10 strings in the array, but sizeof(*list) will return a value of 4, which is the size of a pointer. To get the number of elements in the array you need sizeof(list)/sizeof(list[0]) which is the size of the array divided by the size of one element of the array.

And why are you adding +1 to that sizeof ? That will make the loop iterate once too many times.

line 13: You need 10 there, not 5.

lines 15, 16 and 17: There are quite a few sort algorithms, but I …

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

4. Anything related? I recommend using Lint

I don't like Lint -- I tried it a couple times 10+ years ago and it produced so many warnings that I couldn't tell the good from the bad (meaning the warnings that were superfluous). IMO modern compilers do a better job of finding mistakes.

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

First of all, you only have ONE birthday -- the day you were born. After that they are the Anniversary of your Birthday.

My family (wife and two children) celebrates the anniversary of our birthdays all at the same time because we are so close (within 30 days). We have a small get-together, dinner, then cake & ice cream. There is only one candle on the cake because we don't want to burn the house down :) The only birthday presents we give any more is just ourselves. We give each of our two grandchildren $100.00 USD on the anniversary of their birthday.

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

line 48: you must specify a data type, such as int Array[ARRAY_SIZE]; line 50: You need to specify data type here too.

line 55: remove that semicolon at the end of the line.

line 56: Need opening brace {. Add closing } at the end of the loop.

line 61 and 62: Delete them because they are not needed.

line 64: That is not calling the function -- that is just a function prototype.

There are probably other problems, such fix the above, recompile, and report if you still have questions.

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

Favorite IDE: VC++ 2008. Code::Blocks close second. M$ has the best debugger on MS-Windows I've ever seen. When on *nix I use VI and gdb, recently started using Code::Blocks on *nix.

Graphics library: ON MS-Windows I love MFC. It's difficult to learn, about a year's learning curve. I've dabbled with wxWidgets, but not much.

Other libraries: DataReel. I know it fairly well, and have even ported it to WinCE a few years ago.

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

If you want to upgrade an existing machine from XP to Win7 then you need to run the compatibility program -- get it here. That program will tell you if you have any driver issues.