You don't need a switch statement for that.
objectclass* objects = new objectclass[numberfobjects];
or use a vector of objects
vector<objectclass> objects;
objects.resize(numberofobjects);
You don't need a switch statement for that.
objectclass* objects = new objectclass[numberfobjects];
or use a vector of objects
vector<objectclass> objects;
objects.resize(numberofobjects);
just include <list> header file. See this link
#include <list>
using std::list
int main()
{
list<int> MyList;
}
Walt is correct, you are leaving those files open. That's won't happen if you would remove the parameters and declare the streams inside the functions that use them, as I suggested over 4 hours ago.
Then maybe you should be using something else. OpenGL maybe? But I don't know if it will be any faster.
But I read 99.9 or 100 into the studentGPA array, it should've stopped iterating right? It just keeps giving me 0's. The first line is supposed to be the last name, then the GPA is supposed to be on the next line.
No because your logic is backwards, as I previously explained. Your program reads a value, increments the counter then expects to find the value that was just read in the previous slot of the array. You have to check for 99 before incrementing the counter, not after.
There are several ways to allocate arrays, here is just one of them.
int rows = 10;
int cols = 2;
int **arry = new int*[rows]
for(int i = 0; i < rows; i++)
arry[i] = new int[cols];
If you need all the memory to be in one congiguous block of memory, then allocate like this and calculate the offsets to any given row and col.
int rows = 10;
int cols = 2;
int *arry = new int[rows*cols]
Maybe you are doing it wrong.
DirectDraw is used to render graphics in applications where top performance is important
I don't use DirectDraw so I don't know if that's really true or not. Here's the source of that quote.
Thank ye' dragon of ancientness.. i think the main problem I ran into is forgetting to pass fstream objects by reference. aside from that, a few typos and I be good to go.
No, that is not all the problems. Read the rest of my post where I mentioned opening the files in two different places. You can't do that.
line 18 initializes all array elments to 0. Therefore, line 28 can never find one whose value is anything other than 0. When the loop is first entered, the value of counter is 0 and the value of studentGPA[0] is also 0. The next iteration of the loop the value of counter is 1, so the value of studentGPA[1] is 0.
I think the solution to your program is to use a do loop, not a while loop, so that the test is at the bottom of the loop instead of at the top.
When you pull the rug out from under an executing program there is no guarentee that the operating system can kill the process cleanly. Using that taskkill.exe should be a last resort.
We have no idea what you are trying to do with all that memory so we can't really help you very much. You're going to have to post some valid code and explain what the program is doing if you really expect any better help than what you've already received. Afterall, we can't see your monitor or read your mind. But be careful not to post anything you will later regret because DaniWeb will not delete it once posted.
line 92: isdigit is a macro that returns a bool value (true or false, 1 or 0) You can not compare it with a value as you have tried to do in that line.
Other errors I found are just careless errors, such as misspelled function names. Check the error messages better and you will be able to correct them.
ifstream and ofstream objects have to be passed by reference, not by value, like this line: void load(ifstream& infile);
But that is also going to get you into deep trouble because the streams were already opened in main() and your program is trying to open them again in load() and save(). Do that is just one of the two places, but not both. Since the streams declared in main() are not used for anything just don't pass any parameters to load() and save(), and declare the stream objects in those functions.
So, your program is trying to make 31,536,000 memory allocations (86400 X 365) ???
how much memory is being allocated at one time? Is any of the memory ever released?
Great :) Thanks.
I have no clue. Where did you see that? Quote some of the surrounding text in which that appeared.
I tried to write up a test program that uses std::copy and istream_iterator to copy one text file to another. But it doesn't work because the '\r' and '\n' in the file are lost. I've tried opening the input and output files in text mode and I've tried opening them in binary mode. The results are the same. Anyone know why this doesn't work correctly?
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;
bool ReadFile(const char* filename)
{
ifstream in(filename, ios::binary);
if( !in.is_open() )
{
return false;
}
istream_iterator<char> begin(in);
istream_iterator<char> end;
ofstream out("out.txt", ios::binary);
if( !out.is_open() )
{
in.close();
return false;
}
ostream_iterator<char> begin2(out);
copy(begin, end, begin2);
return true;
}
int main()
{
ReadFile("test2.cpp");
}
Here is what the output file looks like
//test2.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"#include<Windows.h>#include<iostream>#include<fstream>#include<iterator>usingnamespacestd;//typedefvoid(WINAPI*PGNSI)(constchar*);boolReadFile(constchar*filename){ifstreamin(filename,ios::binary);if(!in.is_open()){returnfalse;}istream_iterator<char>begin(in);istream_iterator<char>end;ofstreamout("out.txt",ios::binary);if(!out.is_open()){in.close();returnfalse;}ostream_iterator<char>begin2(out);copy(begin,end,begin2);returntrue;}intmain(){ReadFile("test2.cpp");}#if0intmain(){charbuf[255]={0};DWORDdwError=0;HMODULEh=LoadLibrary("C:\\dvlp\\testdll\\Debug\\testdll.dll");if(h==NULL){dwError=GetLastError();FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);cout<<"LoadLibrary()failed\n";cout<<buf<<'\n';return1;}PGNSIpGNSI=(PGNSI)GetProcAddress(h,"?ReadFile@@YG_NPBD@Z");if(pGNSI!=NULL){pGNSI("test2.cpp");}else{dwError=GetLastError();FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);cout<<"GetProcAccress()failed\n";cout<<buf<<'\n';}}#endif
>>it never creates the file and writes to it
You should have put some error checking into the program. I suspect either LoadLibrary() failed or GetProcAddress() failed. I had a similar problem until I passed GetProcAccress() the mangled function name.
Here is a dll that I just wrote and it works ok with the driver *.exe program that calls the function using LoadLibrary() and GetProcAddress(). The second parameter to GetProcAddress(), which is the name of the function to be called, as derived by using dumpbin.exe on the dll then copying the function's mangled name into the second parameter. In my example the mangled name was "?ReadFile@@YG_NPBD@Z"
This is the dll function.
#include <fstream>
#include <Windows.h>
#include "testdll.h"
using std::ifstream;
using std::ofstream;
TESTDLL_API bool WINAPI ReadFile(const char* filename)
{
char c;
ifstream in(filename);
if( !in.is_open() )
{
return false;
}
ofstream out("out.txt");
if( !out.is_open() )
{
in.close();
return false;
}
while( in.get(c) )
{
out << c;
}
return true;
}
Here is the driver program. I just hard-coded the input file name.
#include <Windows.h>
#include <iostream>
using std::cin;
using std::cout;
typedef void (WINAPI *PGNSI)(const char*);
int main()
{
char buf[255]= {0};
DWORD dwError = 0;
HMODULE h = LoadLibrary("C:\\dvlp\\testdll\\Debug\\testdll.dll");
if( h == NULL)
{
dwError = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
cout << "LoadLibrary() failed\n";
cout << buf << '\n';
return 1;
}
PGNSI pGNSI = (PGNSI) GetProcAddress(h,"?ReadFile@@YG_NPBD@Z");
if( pGNSI != NULL)
{
pGNSI("stdafx.h");
}
else
{
dwError = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
cout << "GetProcAccress() failed\n";
cout << buf …
You need to post some code that illustrates the problem. What compiler are you using?
your example is wrong. What about the two Ns in Bunny and the s's in Mississippi?
DID THE BUNY GET THE MISISIPI MAPS FROM B
One problem with that is there is no way to recreate the original sentence. In any event, you will have to show is what you have attempted, such as post some code or pseudo code.
class A
{
public:
A() {x = 0;}
protected:
int x;
/*Variables required by A, B, C and D */
/*functions required by B and C*/
};
class B : public A
{
};
class C : public A
{
};
class D : public B, public C
{
};
The simplest way to write that program would be to use strtok() function. But if you are not permitted to use that, then you will just have to write your own.
>>char *ptr[count+1];
C99 will not allow you to do that, but vuture versions of C may. For now you need to call malloc() to allocate count+1 bytes: char* ptr = malloc(count+1);
>> ptr[count]=*t;
ptr is just a one-dimensional array of characters. That line is treating it as a two dimensional array. If that's what you want to do then you have to redefine ptr like this: char **ptr = malloc((count+1) * sizeof(char*));
Then you will have to allocate space for each line in the array. Before you can copy characters into the array you have to know how many characters are going to be copied so that you can allocate the appropriate amount of memory.
Your split function has to be made a little smarter. First find out how many characters there are in the current segment, malloc() space to hold that many characters, then finally copy the characters. After that go on to the next segment and do the same thing again.
[edit]Didn't see Edward's ^^^^ post, but he has presented another way to accomplish that.
When you say "injecting" do you mean LoadLibrary()? If not, then what exactly do you mean.
The error message says it all -- you are trying to link together two *.obj files that contain the same function.
Come on, this is reality.
Oh :-O When was the last time you saw an alien?
One impression is like those in the TV miniseries named V. The aliens came to earch to harvest humans as food.
seriously, who cares to believe if a word exist or not.
English teachers, and others who want to express themselves correctly so that other people can understand them. Its similar to using leek speak here on DaniWeb forums -- forbidden.
If the data item is a pointer, then again all you have to do is swap pointer values, no need to actually copy the data that the pointers point to. The same goes for other objects as well.
Haven't you seen the commercials -- "Join the Army and see the World"??
Yes, those are the two statements I had in mind. Some new programmers might find the logic easier to follow if they calculate number^2 in a separate statement.
If you want to redirect input from a file you can also use cout and redirect output to a file when the program is run from the command line, script or batch file (depending on the operating system). In that case you don't have to know a thing about ifstream or ofstream.
I just answered the question how to turn "00 00 57 8B CB 89 5D" into an array of bytes that contain this: {0x00, 0x00, 0x57, 0x8B, 0xCB, 0x89, 0x5D}.
How to use that array of bytes is a different question that I can not answer.
You mean something like this?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#pragma warning(disable: 4996)
int main()
{
char line[] = "00 00 57 8B CB 89 5D";
unsigned char bytes[40] = {0};
char* ptr = strtok(line," ");
int index = 0, i;
char* end = 0;
while(ptr != NULL)
{
bytes[index++] = (unsigned char)strtol(ptr,&end,16);
ptr = strtok(NULL," ");
}
for(i = 0; i < index; i++)
printf("0x%x ", bytes[i]);
printf("\n");
}
All you have to do is swap the data item of the two nodes -- just like you would swap any two integers.
And please use code tags the next time you post code so that people can read it.
This is a very simple program that should give you a little practice with reading and writing simple text files. You need to use ifstream for input file, and ofstream for output file. Then in a loop, read a number, square it, then write it out to the output file. That loop is only three statements long (while, square, write) If it takes you more than three statements then you are probably not coding it correctly. Actually, now that I think about it you could do it in fewer than 3 statements, but that may be pressing it a bit for you right now.
That makes three of us, almost. I live about 30 miles from that town.
Yes, there is that potential problem, which is why I mentioned it in my post. But you will find many many threads which have contributions by several people. Its not just a two-sided two-people threads.
You can contribute to threads even if you don't know the answer to the original question. If you find a thread where someone answers it and you don't understand the answer, then go ahead and ask for more clarification about what you don't understand. But be careful here not to hijack the thread and take it in some other direction. If you don't understand some answers then chances are there are other readers who don't either. That kind of dialog is one way for everyone to learn something new that day :)
>> if(buffer, L"FireFox") {
It didn't compile because of the above line. How is buffer declared? Where is buffer declared? And that if statement is just constructed wrong.
You need something like this: if( strstr(buffer,"FireFox") != NULL)
or if you are compiling for UNICODE then use _tcsstr (it is for Microsoft compilers) instead of strstr()
scanf("%s" ...) stops reading keyboard input at the first space. So if you type "John Doe" all scanf() will return is "John". The word "Doe" will be left in the keyboard buffer, so the next time scanf() or gets() is called it will grab "Doe" and not let you type anything.
If you want the user to enter two or more words, like "John Doe", then use fgets() instead of either scanf() or gets(). Note that gets() is never recommended because it can cause buffer overflow (entering more characters than the buffer can hold.)
If you only know part of the string then use enum windows, then in the callback function call GetWindowText.
Best place to ask those questions is on the ITK's mailing list. Read the *.pdf file from this link. They are the experts, not us.
I didn't realize there was a daily limit, but I doube that I've reached it.
Well, yes he is right about that. This is an employer's market, meaning that programmer's today are a dime a dozen. If you don't know how to market yourself you will be flipping hamburgers ar McDonalds or bussing tables at restaurants for many years. That deree you get from college is no guarentee you will get gainful employment in your chosen field of study. I know people with Masters and Ph.D. who are out of work.
>>None.
That link doesn't work for me.
>>there is a pointer passed there.
The problem is not the pointer -- the problem is typecasting int* into char* when passting its value to printf(). int* is four bytes of memory, while char* is only one. So *(char*)p1
tells the compiler to pass only the first byte of that four-byte integer to printf(). It may actually seem to work when storing the value of 10 to the integer. But store the value of 123456 to the integer and see what you get.
I've seen this happen a couple of times recently, when I give positive rep the member's rep count seems to either not change or go down. See this post for example. Then look at the reps give in his profile page. The one I just gave him isn't there. Also see the line chart -- the line for rep count went down. Does that indicate he got some negative rep?
Aha..
If I may say, that would be stupid.. :)
My point exactly.
>> Sorry for annoyance. Is VC++ 2010 Express compliant?
Yes. What compiler are you using?
>>I had an impression we're not talking pointers in general
That was referring to the printf() line, where the format specifer string uses %d yet only one of the 4 bytes of the integer is in the parameter list.
As for the malloc() line -- it had notthing to do with endianness.
The code you posted doesn't have any typcasting problems. Try this and see what gcc does: long* pl1 = (char *)p1;
That's the kind of typcast problem I'm talking about.
>>Now I am really confused. Which snippet? int* x = (char *)malloc(sizeof(int));
>>Care to elaborate? (TM)
Do I really need to explain what's wrong with the above code snippet? Put that line in a C program and try to compile it. VC++ 2010 Express gives warning: warning C4133: 'initializing' : incompatible types - from 'char *' to 'int *'
>>It will be getting int all right.The pointer is very well aligned. On a little-endian machine it would be a right int, surprisingly.
Not really a pointer problem. Try storing a large number in the integer (someing greater than 255) then see what you get. It takes 4 bytes (on 32-bit compilers) to store an integer. All that is being sent is one of those four bytes.
See his 24 Nov 2009 post -- with -2 rep. TheDude also got bad reped twice in this thread. Don't know if those are new or old reps.
See the code snippet I posted for clarification of what I meant. Of course int* can be typecast to char* under some circumstances. But not when typecasting the return value of malloc. Another place where such typecasts won't work correctly is line 6 of the original post in this thread. In that case its not a compiler error, but a logic error because, as someone already pointed out, printf() will be expecting an integer but only getting a char which will be upgraded to int when its passed to printf().