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

The function that callers ConvertToChar() must delete the memory when done with it. That's a common requirement of programming. Of course you could also return std::string instead of char* which would eliminate the need for allocation -- but if this is for eVC++ compiler you won't have that option. A third option is to make the calling function pass in a pointer to the buffer.

char* ConvertToChar(const CString &s, char* pAnsiString, int bufsize)
{
 wcstombs(pAnsiString,(LPCTSTR) s, bufsize);
 return pAnsiString;
}

Also, you might as well delete that memset line, no point wasting cpu cycles clearing memory and immediately copying something else into that same memory.

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

just be aware that your program may or may not compile with your instructor's compiler (assuming your instructor compiles student's work). Find out what compiler your instructor uses.

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

>>id what i have so far look right
does it do what you want it to do? If yes, then it is correct.

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

>>to determine when the farmer will have to seperate the bunnies i

when full capacity is reached. If the capacity is 1,000 rabbits then that is when the males have to be separated from the females, or when you have to casterate all the males.

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

is it possible to have 5 stars but a rep power of only one green square thingy?

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

>> Do I need to put a table or can I get the results I want without one?

Don't know -- since the problem doesn't say one way or the other I suppose you can do it however you want.

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

Then why did you start a new thread?

start here.

#include <stdio.h>

int main()
{
  // put your code here
  return 0;
}

You may want to work through the program math on paper before starting to code so that you understand the math part.

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

>> cin << math_operator;

you used the output operator, not input. You meant this:
cin >> math_operator;

>> case 's': main()
Not allowed to call function main() in either c or c++. You will have to think of a different way to restart the program.

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

You will probably get NO useful responses by spamming this board with your homework assignments. Since you did not as a question I suppose you expect somebody to do the work for you.

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

>>Just wanted to know wat is the meanig of this stmt
it means that you coded two variables using the same name, when you do that only the variable closest to the satatement in which it is used will be visible to the program. Look up the term scope for a more in-depth explaination. You correctly changed your program in your last post to fix that problem.

>> for learing the string legth funcitons.

Most programs use standard C strlen() function that is in string.h -- the only reason to code your own function is for educational purposes. Most real programs do not do that.

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

>>But when we end the program the memory is automatically returned to the OS for its use

Not necessarily. Some operating systems do not do that. There are some resources even in MS-Windows that will be permanently lost until the next reboot of the computer if the program that allocated them failes to free them.

>> it was about to use so why the realloc instead of malloc.
I already mentioned that realloc() expands existing memory, malloc() does not. If your program runs long enough it will eventually run out of memory and malloc() will return a NULL pointer.

>> while ((inputBuffer = getchar()) != '/n')

The '/n' should be '\n'

>> char *input = (char*) realloc (input, index + 2);
remove the 'char *' -- it is hiding the other variable
input = (char*) realloc (input, index + 2);

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

>>is it possbile
Yes

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

>>meaning of memory leak
it occurs when you use one of the functions that allocate memory, such as malloc() then do not free the memory before using the pointer again. In the code you posted, pointer variable s was allocated over and over without ever freeing the memory. You should have called realloc() instead of malloc() so that the memory block would have been expanded.

>>scribbling on the memory
that means the program is writing outside the allocated memory, and it is not know where the program will write the data, could be almost anywhere. In the code you posted, the program could very easly be writing into other variable's memory address, destroying whatever value was legatimately there. Its something like giving a little kid a crayola and a color book -- watch the child scribble all over the page not paying any attention at all to the lines that define the picture. That's what your program was doing.

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

>>Can you please modify the code so ...

No, I don't do homework for anyone.

>> s = malloc(sizeof(char));
This just allocates one character, causing a 1 byte memory leak on every loop iteration.
>> s[index] = inputBuffer;
Since the malloc allocates only one character, this line will just scribble all over memory and most likely cause your program to crash big-time.

How to fix the above problems: use realloc and pass the number of bytes to allocate
s = realloc(s,index+1);

Tip: sizeof(char) is ALWAYS 1 regardless of computer, operating system, or compiler.

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

Unfortunately there is no standard c or c++ way to accomplish that. So you have to resort to some non-standard functions that you compiler may support, such as those in conio.h. I believe the functions in that header file were originally developed by Borland for their Turbo C compiler, but other compilers have picked up many of those functions. Check to see if you compiler has conio.h and if it does then look in that file for kbhit() (which checks to see of a key has been pressed) and getche().

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

since you have MainFrm.cpp I appears that the original program was MFC. You don't want to create a win32 Console application for that, but create an MFC application just like you did the original program, but give it the new program name. After that you should be able to delete all files from the project, copy the files from the old project directory into the new directory, then add the files you copied into the new project -- menu Project --> Add To Project --> Files.

You will probably want to keep the <project name>.cpp and <project name>.h files that the wizard created for the new project.

Here is a utility program that is supposed to rename the project. I have never used it so use it at your own risk. You will want to read the comments at the end of the article.

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

put this as the very first line in stadafx.h but after include safeguards

#pragma warning(disable: 4786)

Note: NO semicolon! That warning only occurs when your program is compiled in debug mode and can safely be ignored. If you don't disable it, it will muddy up compiler complaints quite badly.

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

1. you forgot to add n as a parameter

i_FValue    =    CS_write(fp_MYFILE,n2);

2.

CS_write(fp_MYFILE, n2)
FILE    *fp_MYFILE;
int n2;
{
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

do you know anything at all about computers and programming? just add the value of n2 on the command line after the file name

./test [filename] [n2]

example:
./test myfile.txt 25 <Enter>

>>how does the program read the value n2?

It doesn't read it at all. see the example in my example above and in my previous post -- the value of n2 is in argv[2]

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

I hope this is an old program you have that you need to change because it is written in ancient original K&R style.

There are several changes you have to make -- and below may or may not be all of them
(1) change if(argc != 2) to if(argc != 3) in function main()

(2) add variable n2 to main() and pass it as a parameter to CS_write(). Then somewhere in main set value of n2 = atoi(argv[2])

(3) In function CS_write(), add a new parameter n2, delete n2 as a local variable.

(4) delete this line: memcpy(&n2,data+17,sizeof(int));

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

you can not initialize variables using calculations as shown at the beginning of your program (globals). Variables are initiailized only once when the program starts. If you want the program to do the calculations then you have to do them at the appropriate time, such as set the value of j only after the value of i is known.

Also, use meaningful variable names -- one letter variables, although the compiler will not complain, they are not very comprehensible to human eye. Instead of variable i rename it interest -- only takes a little more typing but makes your program a great deal more professional looking and easier to understand.

Adding .0 to the constants as shown below will tell the compiler to use floating point arithametic, not integer arithmetic.

cin >> i;
 j = i/(12.0 * 100.0);
m = (p*j)/(1-pow((1+j),-n));
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

File --> New --> Win32 Console Application --> Wizzard displays a dialog with 4 radio buttons, the first one is An empty project.

I have VC++ 6.0 Pro edition. I don't know if other editions (such as standard edition) have that menu item or not. If not, then just create a console application, delete all the *.cpp and *.h files, then copy from old to new project.

If you want an MFC project, then its a little more complicated, but basically the same. Create a new mfc project and copy files from old to new project, replacing any files that the new project contains that have the same filename. you may have to rename some of the files after copying.

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

1. start a new empty project and call it qcard5.

2. Copy the *.cpp and *.h files from qcard4 into qcard5 directory

3. select menu Project --> Add To Project --> Files and select the files you want to add to the project.

Mission accomplished.

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

I can't help you with the math (have no clue what a quetelet is) but you will need to add a getch() immediately following that scanf() to remove the <Enter> key from the keyboard buffer. scanf() does not do that for you.

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

>> search_nameFile.seekg( ( name - 1 ) * sizeof( phonebook) );

variable name is a std::string -- why are you attempting to treat it as an integer? I would think that function searchbyname() should seek to the beginning of the file then enter a loop to sequentially read each record until eof or the desired name is found. Your version of that function does not do that at all.

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

And here is how to change wallpaper.

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

I'm certain there is a FAQ about this. The only way to do that is to rewrite the entire file. Read the whole file into memory then write it back out the way you want it. If the file is too big to read into memory all at once you can use a temporary file.

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

use srand() to see the random number generator

int main(){
   srand(time(0));
    cout << getRand();
    cin.ignore(2);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

dev-c++ doesn't like strstream header file and claims it is deprecated. you should consider tossing it out of your program and using either iostream to display stuff on the console window or fstream to read/write to a disk file.

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

>> fwrite (fp,i*sizeof(char),1,buff);

there is no need for sizeof(char) because it is guaranteed by the language to always be 1 regardless of platform or compiler. Just makes less typing and fewer characters to read :mrgreen:

fwrite (fp,i,1,buff);

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

just a few coding style changes -- I like to make one-line functions inline -- saves keyboard typing :cheesy:

// Store creation for practicing Classes in C++
// Woobag

float TOTAL_PRICE = 0.0;

class cFood {
protected:
    string type;
    float price;

public:
    void setvalues(string, float) {type = i; price = f;}
    string gettype(void) {return type;}
    float getprice(void) {return price;}
};


// end of class declarations and implementations

inline float cashReg(float money) {
    return money - TOTAL_PRICE;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

See Microsoft's Enroll Tutorial and here for an MFC solution.

If you need to use Chinese character sets you can code for UNICODE by defining the macro UNICODE and _UNICODE in the project c++ settings. I don't know how that will affect the MFC database code that is presented in the tutorial because I never tried it.

Of course you can always use ODBC C++ generic classes.

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

for ex:
here is my input file ack=100 , and i want 2 read the four chars ack= and then after reading the input file will b as shown 100

If I understand you, you want to put the value 100 into an integer variable:?: If that is true, you have a couple options:
(1) use sscanf(), or (2) pointer parsing. There are other options in c++ using stringstream class.

char buf[20];
int n;
// read a line into buf is not shown
//buf[] = "ack=100"; after read
//
sscanf(buf,"ack=%d",&n); // now n == 100

(2) parse with a pointer

char buf[20];
char* ptr;
int n;
// read a line into buf is not shown
//buf[] = "ack=100"; after read
//
ptr = strchr(buf,'='); // locate the '=' symbol
n = atoi(ptr+1); // convert remainder to int
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think you need a clearer definition of what you want. characters don't just "disappear" -- unless your a magician.

Post an example of what you want. For example, if the word "hello" is read, do you want to delete the two 'l's and make it "heo"?

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

implementation is normally in either a *.lib or *.dll and may or may not have the same name as the header file. You will probably not have the source code for system libraries and dlls. normally will NOT have the same name because libraries and DLLs contain implementation for many many header files. Look in your compiler's lib directory for these files.

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

try Dev-C++ at www.bloodshet.net

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

If MSData is a c++ class then its destructor should delete all dynamically allocated class objects.

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

Sorry to be boring but I want to count the number of strings only in the first line.
I mean a string each group of one or more caracters limited by a space...

Can you help me? I tried to use the code you indicated but without sucess...

Those are called words, not strings. A string is just a colection of one or more characters of any length, such as

"Hello World This Is a String".

When learning to write C or C++ programs, it is important to also learn the correct terminology to avoid ambiguity.

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

what operating system and compiler are you using? Some of the code appears to be MS-Windows 32 api functions, but I don't know what the rest is -- gotoxy() is a TurboC MS-DOS function, there is no such thing for MS-Windows GUI programs.

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

Can u let me know the bugs in my code Ancient Dragon ?
Except the line "file://int elements[] ={0}; //ur code".
coz i want to be perfect like u .... :lol:

For starters:

1. variable integer is being used without having been initialized. It will just contain some random value -- whatever happens to be on the stack at the time -- including -1.

2. There are no bounds check to insure that counter does not exceed the number of elements allocated to the array. I can type in as many numbers as I wish.

3. The value of ArraySize after the end of the previous loop may exceed the size of the array due to #2 above.

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

post code. Since you use fopen() I assume this is a C and not C++. After fopen, just use fprintf() then fclose().

char strp[] = "Hello World";
FILE* fp = fopen("myfile.txt","w");
fprintf(fp,"%s\n",strp);
fclose(fp);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>disp.c:29:9: empty character constant

>> if(c<''||c>0x7e)

there is no character between the single quotes. you probably meant to put a space there??

Another bug:
>>}while(c!=EOF);

the above is not needed because its not a do-while loop.

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

download this brief example program. and look at tic2a.c. It uses non-standard functions but might work with your compiler if you include conio.h.

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

>>These lines r from an .asm file.
what makes you think you can compile a file written in assembly language with a c compiler? It will never happen and you are just wasting your time trying to make it compile. There are lots of tutorials and books about how to write c programs, I suggest you start reading them.

>>It is also giving the error unable to start .exe of that application n System cannot find the file specified. Why I am getting this error

because your compiler did not create the .exe file -- you have to fixe all the compiler errors first. There is no point in creating an executable file if it does not contain all the code you were supposed to write.

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

Are you losing your eyesight already. I did not think you were that old Ancient Dragon? ;)

No -- but the code posted by Atul also has lots of problems/bugs. But then of course I'm perfect -- I was only wrong once in my life and that's when I thought I was wrong, but I was wrong about that.:mrgreen:

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

Hi ,

Try the code below.
I have modified your code so that it should run properly.
I have commented the problematic code of yours and added mine.

I think this will solve your purpose

So now its the blind leading the blind:mrgreen:

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

when the DWORD rolls over, we don't really care what the previous count was -- just need to add ULONG_MAX to current return from GetTickCount() to get the 64bit tick count. So the value of fudge after roll-over should be just ULONG_MAX. Example: suppose ULONG_MAX == 10, previous value was 9 and current value is 1. Then the return value from that function should be ULONG_MAX + 1 == 11. When it rolls over the second time, the return value is (ULONG_MAX*2) + new value or (10 * 2) = 20 + new value. This continues until fudge variable rolls-over, at which time the function stops working and results become unpredictable.

fudge += ULONG_MAX;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

maybe this will get you started. iostream.h is obsolete -- if you are uisng a recently new compiler then use iostream (without the .h extension)

#include <iostream>
#include <ctime>
using namespace std;
int recursiveMinimum (int [] , int);
int main()
{
 int i,smallest, counter = 0;
 int ArraySize = 10;
 int elements[ArraySize];
  // fill the array with some random numbers
  srand(time(0));
  for(i = 0; i < ArraySize; i++)
       elements[i] = rand();  
  smallest = recursiveMinimum (elements, ArraySize);
 // remainder of program not shown