Except that that version is buggy.
where is the bug? It compiles ok for me and returns a value similar to strcmp().
The comparison function must return an integer less than, equal to, or greater than zero ...
Except that that version is buggy.
where is the bug? It compiles ok for me and returns a value similar to strcmp().
The comparison function must return an integer less than, equal to, or greater than zero ...
just an alternate compare. qsort() doesn't care about actual values, just cares about 0, < 0 or > 0.
int compare (const void* source, const void* dest)
{
return ((List*)source)->value - ((List*)dest)->value;
}
>>i need to point each element.
do you mean you want a pointer that connects element 1 of line 1 to element 1 of line 2? If line 1 and line 2 have correcponding element numbers then do something?
since the lines have variable number of entries fscanf() will be little use because it requires fixed length number of entries on the line. Instead, use fgets() to read the entire line into memory than parse for spaces and assign to approirate members of the structure. you can use either strtok() to check for spaces or simply strchr() which will return a pointer to the first space. strtok() is probably easier to work with because it will return a pointer to a null-terminated string, while strchr() will not (you have to do it yourself).
post your code using std::string because it does work with that. replace strstr() with the std::string's find() method, like this
for(int i=0; i<20; i++)
{
if( data[i].find(search) == string::npos )
cout << "NO MATCH" << endl;
else
{
cout << data[i] << endl;
incVar++;
}
}
use != not the == operator. But yes, C style strings always should be terminated with '\0' null terminator.
while(charbuff[i] !='\0') {.........
or it is a better way?
depends on what you want to do.
why don't you make i a local variable and function() return its value.
#includes....
int function()
{
int i = 0;
while(charbuff[i]!=' ')
{
ofstream stream("file.lng", ios::app)
stream << charbuff[i];
i++;
stream.close();
}
return i;
}
main()
{
........... some code
int i = function(); /*call function */
cout << i; /* the i is set to 0*/
........... some code
}
[rant]flow charts are evil little critters and should be banned from all educational institutions. I've never seen anyone create or use them outside the university. And most programming books don't even talk about them.[/rant]
See -- here's a good example of where to have a Delete My Post button[code deleted -- duplicate of Dave's original code] See -- here's a good example of where to have a Delete My Post button
>>Is it like below wat u said:
did you test it to verify whether it works or not ?
just use strcpy() and strcat() to create the new filename. you will have to truncate the file extension first. Now you will have to pass new_filename as another parameter to CS_write() function so that it can use it in the fopen() function instead of the hard-coded text.
char new_filename[512];
strcpy(new_filename,argv[1]);
// locate the file extension, if there is one
char *ext = strrchr(new_filename,'.');
// now truncate it
if(ext != NULL)
*ext = 0;
// add _new
strcat(new_filename,"_new");
// add the file extension
ext = strrchr(argv[1],'.');
if(ext != NULL)
strcat(new_filename,ext);
1. it is not necessary to specify ios::in for ifstream, that is what ifstream does anyway.
2. use std::string's c_str() method when passing it to the constructor
ifstream openfl(file.c_str())
3. cin >> file; This construct will work as long as there are no spaces in the filename and optional path. use getline() if it can contain spaces
getline(cin,file)
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.
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().
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;
{
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]
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));
What is this 'google' you talk of? :?:
Its a search engine for the internet. see www.google.com. type in a key word(s) and it will try to find it. Sometimes it works ok, but often it will return trillions of answers :mrgreen:
>>Now my guess is that the first block of code uses more memory at any given moment than the second one
In the second example, a good optimizing compiler will notice that it only needs to allocate space for one std::string object and reuse this memory on each block entry. All memory is allocated on the stack during function entry, not block entry. That makes the second example more memory efficient than the first. It would be the same as declaring one std::string object at the beginning of the function and re-using it in each block, something like this: (in this example you don't need the blocks at all!)
int foo()
{
std::string str;
{
str = "Hello";
// blabla
}
{
str = "By";
// blabla
}
}
>>And each mystring only has scope within the {} enclosing it right?
That is correct -- but it doesn't mean that the memory allocated for the objects are removed from the stack when the block terminates. It is only released when the function returns to whoever called it.
>>the second block of code uses more than or equal processing power than the first one
Depends on how you look at it. Overall, from the beginning to the end of the function they are both equal. But the second will consume less initial processing than the first because std::string class constructor will be called for all three strings at the same time in the first example, while …
>> But anyways, I'm on windows, and most likely they will be using windows also, maybe a different version, like win xp home vs win xp office or whatever
It will be portable across all versions of MS-Windows since Win95. You should verify by looking up the functions at www.microsoft.com -- the function descriptions tell you which versions of MS-Windows are required.
>>EDIT: but don't worry, if it gets bad I can always swallow my pride and ask my folks for money
You can always get a real job flipping hamburgers, bussing tables, washing cars, etc. :cheesy:
It won't be portable anyway -- changing colors is os-specific, ms-windows is NOT the same as *nix or MAC. If you want portability among operating systems you will have to embeed proprocessing symbols in your overloaded function, something like this:
ofstream& operator<<(ofstream& os,const string& color)
{
#if defined(_WIN32)
// blabla
#elif defined(_UNIX)
// blabla
#elif defined(_MAC
// blabla
#endif
}
The above overload probably won't link because there is already an overload function with those parameters in <string>, so you will have to think some kind of parameters.
No there is no shortcut because changing the color is an os-specific function and ansii standards try to stay clear of those issues. -- but you might be able to write your own ofstream overloaded function that will accomplish that.
In c and c++, char is sort of a misnomer -- it is meant to hold more than just characters, it is in fact a small one-byte integer and can be used just like integers of other sizes. unsigned char has a range of 0 to 255 (see limits.h for ranges on your operating system). If you look at an ascii chart such as this one you will see that standard ascii characters fall within the range of 32 to 126, all the other possible values are used for other miscellaneous things.
>>Also, can the char data type hold more than one character
In special circumstances, yes -- they are called Binary Coded Decimal (BCD) or Packed BCD. This is not very common on MS-Windows or *nix computers.
I think you're c++ program will have to change fonts before beginning to output the text. The font used by the c++ console program is not the same font that the IDE editor uses. The IDE does not use cout or other console output functions to write to the window -- it uses win32 api functions. Don't know how to do that in console program.
it was a general c++ program.
No it isn't. A c++ program does not use stdio.h, string.h and many of the other header files that are in your program. Nor does a c++ program use printf(). And rarly do they use structures. In fact, your program does not contain even ONE line of c++ code.
you still have not posted any errors
And please use code tage, not php tags. For some reason I can't copy that into my clipboard.
that is NOT a c++ program. Its a C program.
What are the errors? Post them.
And you really really need to develop a decent program coding style. Starting everything at the beginning of the line is just plain lazy and horrible. Nobody is going to read that stuff.
>>If I want to generate random numbers between -1 and 1
Why? The only number between -1 and 1 is 0. -1, 0 and 1 are all integers, there are no fractions in integer math. rand() returns an integer. So if you want a float return value, such as 0.123 then you will have to write a function that puts the return value from rand() into a float variable then divide it by RAND_MAX (defined in stdlib.h) to make it less than 1. I'm not sure how to make a random number between -1.0 and 0 other than just simply arbitrarily make the result from the above calculations negative.
>void main() is never ever acceptable
It's perfectly acceptable on a freestanding implementation. The rules we refer to when talking about main are exclusive to hosted implementations.
what do you mean by "freestanding implenentation"? some embedded systems where there may not even be a main() function ? I doubt there are may people in the whole world who program in that kind of an environment. And I don't think the c standards apply there anyway.
If you don't want to return a value in main(), declare main() as void.
void main()
main MUST ALWAYS return an int. void main() is never ever acceptable, even though some ancient compilers such as Borland's Turbo C, might not complain and despite the many examples you will see on MSDN. Current standards require main to return an int, and say it is not necessary to return 0 at the end of main().
,back to the real subject.
if the use of char(x) wasent correct then why did it work when i put it through my compiler im confused now?
I didn't say char(x) was not correct -- char(int x) is incorrect.
int x = 'A';
printf("%c", char(x)); <<< this is ok
printf("%c", (char)x); <<< and is same as this
My bad dragon. I assumed it was correct since it worked ;)
It worked because it assigned the value 1000001 decimal to the integer, not 1000001 binary, which has a completly different decimal value.
>>Everyone can be wrong
I've only been wrong once in my life -- that was when I thought I was wrong, but I was wrong about that :cheesy:
int n = 1000001 is NOT the binary representation of 1000001 Binary. Use your scienticic calculator -- 1000001 Decimal is 11110100001001000001 Binary! C and C++ cannot do auto convertion of binary numbers as it does decimal, hex and octal.
char( int n )
[edit]The above is invalid statement
that is more commonily written like this (a cast)
(char)n
>>if you convert binary to decimal the computer will have to convert decimal back to bianary so why not use bianary
No it doesn't -- what you see on the screen is not the same as how it is represented internally by the program. The program only sees binary -- doesn't know a thing about visual stuff. The number 01000B is only visual -- what you see on your monitor. The program cannot deal with that directly, it has to be converted to its internal format. 01000B occupies 5 bytes of data (just count them -- '0', '1',. '0' ... is 5 digits). That all has to be converted down to the size of an int (4 bytes on 32-bit processor).
After the conversion, if you use a debugger and look at value of the 4 bytes beginning at the address of the integer you used, you will find that their vaue (in Intell computers) is 08 00 00 00 in Hex format, which is the original converted value.
I am well aware of how the hardware works. Unfortunately C and C++ languages do not have automatic way of converting a string that contains a binary numbers such as 100011 into an integer using the assignment operator, such as
int n = "0100011";<<< WRONG
but you can use strtol()
char *p
int n = strtol("010011",&p,2); // 010011 Binary == 19 Decimal
Now, if you use a scientific calculator you will find that 'A' …
dude i have already read about that stuff that wasnt my question at all
The only way to answer your question is to convert the binary digits to decimal, as shown in those posts and other places you have probably read. Afterall, all characters, such as 'A', are nothing more than integers. 'A' = 64, 'B' = 65, etc. google for "ascii chart" and you will find them all. just simply convert the binary value to integer and you will have the answer to your question.
Yep it's pretty simple:
int main(int argc, char *argv[]) { int x = 1000001; cout << char(x) << endl; getchar(); }
That isn't right -- it just assigns the number 1000001 decimal to an integer. binary digits have to be converted.
I don't have any problems like that using FireFox 1.07.
>>whats i/o mean
A general term that means Input/Output -- such as writing to and reading from files or some other device such as sockets, keybord, serial ports, cd drives, databases, etc.
with some exseptions
and there are a lot of exceptions :cheesy: you can't even do file i/o with it. the only useful thing HTML can be used for is displaying stuff in browser windows. (I'm not an HTML programmer).
tecknickly it is a programing launguage
Narue is right --No HTML specification has ever called HTML a programming language, or anything like that. It is most commonily called a markup-language
see my previous post for example how to use the brackets. And put them on a separate line to make them easier to see -- helps understand the code better.
But, don't do that. You won't be able to show your code to very many people before they lynch you. ;)
You are absolutely right about that! I once worked on a large-team project where one of the programmers came from PASCAL -- he created almost everything in macros so that his c++ program looked just like a PASCAL program. After he left we all cursed him for that and rewrite his programs to remove those macros.
c and c++ does not use the "then" statement as some other languages do. The next statement or block following the if statement is executed when the if statement is true. Notice the use of the double '=' symbol which is the logical equal. A single '=' means assignment. Lots of people get tripped up on those differences -- even some old timers like me :o
if( g == 3282)
{
// true condition
g = k;
}
else
{
// false statement
}
Yes. After using ClassWizard to generate all the variable names, such as m_m1 through m_m10, then create your own int array and replace all those variables with your array You will need to delete (or comment out) the m_m1 ... varaibel in the .h file and in the .cpp file. Then make the change below in the DoDataExchange() method.
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest2Dlg)
DDX_Text(pDX, IDC_EDIT1, m_array[0]);
DDX_Text(pDX, IDC_EDIT2, m_array[1]);
DDX_Text(pDX, IDC_EDIT3, m_array[2]);
DDX_Text(pDX, IDC_EDIT4, m_array[3]);
DDX_Text(pDX, IDC_EDIT5, m_array[4]);
DDX_Text(pDX, IDC_EDIT6, m_array[5]);
DDX_Text(pDX, IDC_EDIT7, m_array[6]);
DDX_Text(pDX, IDC_EDIT8, m_array[7]);
DDX_Text(pDX, IDC_EDIT9, m_array[8]);
DDX_Text(pDX, IDC_EDIT10, m_array[9]);
//}}AFX_DATA_MAP
}
As he says that it worked with Visual Studio 6.0, most probably some form of that file should be in his computer already. Good to have the latest version of the PSDK though. Especially with the brand new compiler. :D
Visual Studio 6.0 is shipped with those dlls -- he doesn't need the Windows SDK for that.
And you will probably also have to download the Windows SDK free from Microsoft.
That will not be the only porting problem. VC++ 2005 Express is a lot more c++ IOS standards compliant than VC++ 6.0, so that will be a little bit of recoding required.
I find no problem in doing it. Have you done the modifications I told JoBe in a previous reply ( Reply Number 10 in this thread )? Can you make GUI Applications in Visual Studio 2005?
I don't have a problem described in reply #10, but the one in previous reply #9. And yes, I did follow the steps in that M$ link about how to set up the compiler for windows applications -- that's not my problem. But thanks anyway.
I have had the same question about how to disable UNICODE settings -- thanks WolfPack for the help. But even after that, I also get the unresolved external MessageBox error. Can't it be used in console applications?? I use it in console apps with VC++ 6.0 compiler without any problems. Maybe we just can't use in console apps with that free Express edition compiler. Otherwise, the other win32 api functions I use (FindFirstFile(), FindNextFile() etc) compile and link ok.