1,247 Posted Topics
Re: > which one is better? usually [ICODE]const char* const OUT_STRING = "hello world" ;[/ICODE] > why? more efficient. exception: you need to pass it often to functions which expect a [ICODE]const std::string&[/ICODE] | |
Re: > My thought was to make a .h file with a namespace. Would that work? yes, provided that all the functions in the header have internal linkage. ie. they are inline or put in an anonymous namespace (or have the deprecated static linkage specifier). and you have an include guard … | |
Re: [code=c++]std::ifstream fin( "file1.txt" ) ; std::ofstream fout( "file2.txt" ) ; fin >> std::noskipws ; char ch ; while( fin >> ch ) { if( ch == '[' ) fout << 'x' ; fout << ch ; }[/code] | |
Re: > CanvasObj class is defined as a template. Could that be a problem? yes, it is a problem. but it is solved easily enough. template typedefs are not legal in c++98. (they would be in c++0x) as of now, you can not write [code=c++] template< typename T > class CanvasObj … | |
Re: [code=c++] std::ifstream fin( "file1.txt" ) ; std::ofstream fout( "file2.txt" ) ; fout << fin.rdbuf() ; [/code] | |
Re: > Im using rfind() to search backwards but for some reason it starts the search > from the beginning of the line and not from "pos" as i liked to [ICODE]rfind[/ICODE] does start the search from [ICODE]pos[/ICODE] as you expect. but it returns the position where the matching subsequence [COLOR="Red"]begins[/COLOR] … | |
Re: [code] if( !word.empty() ) { if( word does not exist in tree ) { Node node(word, page); avl.insert(node); } else { Node* node = get the existing node for this word from avl node->incPages(page) ; } word = ""; } [/code] this part of the code is truly horrible [code]if(c … | |
Re: instead of this, [CODE]#include<string> using namespace std; #include<iostream.h> [COLOR="Red"]// no such header[/COLOR][/CODE] write [CODE]#include<string> #include<iostream>[COLOR="Red"] // the correct header[/COLOR] using namespace std; [COLOR="Red"]// moved down[/COLOR] [/CODE] also, [CODE]template <class TYPE, class KTYPE> bool List<TYPE, KTYPE> :: _search (NODE<TYPE> **pPre, NODE<TYPE> **pLoc,KTYPE key,KTYPE key1) { // Statements *pPre = NULL; *pLoc … | |
Re: > Do I just need to delete the individual parts of the array seperately? the array [ICODE]geometryArray [/ICODE]itself does not have a dynamic storage duration. (it was not allocated using [ICODE]new[][/ICODE]). therefore, [ICODE]delete [] geometryArray;[/ICODE] is an error. the contents of the array are pointers to objects allocated with a … | |
Re: [ICODE]std::vector<int> row; std::vector<std::vector<int> > event(5,row); // note: every row is empty at this point for(lv = 0; lv < [COLOR="Red"]event.size()[/COLOR] ; ++lv ) { for( [COLOR="Red"]int gno=0 ; gno < event[lv].size(); ++gno[/COLOR] ) { // doing something // [COLOR="Red"]event[lv][gno] [/COLOR] } // ...[/ICODE] | |
Re: since the documents are html pages, you would need to remove the html formatting tags and all white space characters. (ie. just get the plain text of the document). this is required so that differences in fonts/character sizes/colours as well as differences in encoding of newlines etc. do not influence … | |
Re: > here my program crashes.............may be its bcoz the heap memory is less yes. to specify a larger process default heap size for your executable image, use the [ICODE]/HEAP[/ICODE] linker switch. [url]http://msdn2.microsoft.com/en-us/library/f90ybzkh.aspx[/url] to allocate 468 MB of memory, the heap is not a good choice. use [ICODE]VirtualAlloc[/ICODE] instead. [url]http://msdn2.microsoft.com/en-us/library/aa366887(VS.85).aspx[/url] | |
Re: [url]http://www.guyrutenberg.com/2007/09/03/seeding-srand/[/url] [url]http://linux.die.net/man/3/srand[/url] | |
Re: > In java I can use "object" class, because "object" class is father class of all class. C programmers use [ICODE]void*[/ICODE] where java programmers use [ICODE]object[/ICODE]. you cannot use either without a cast; a wrong cast will result in unpleasant consequences in both. in C, incorrect runtime behaviour (seg fault … | |
Re: if you want to do some spirited programming ( [url]http://www.boost.org/libs/spirit/index.html[/url] ) [code=c++]#include <boost/spirit/core.hpp> #include <boost/spirit/actor.hpp> #include <iostream> #include <string> #include <vector> using namespace std; using namespace boost::spirit; bool parse_line( const char* cstr, vector<string>& words ) { subrule<0> top ; subrule<1> word ; return parse( cstr, // begin grammar ( top … | |
Re: [url]http://msdn2.microsoft.com/en-us/library/ms997568.aspx[/url] | |
Re: [code]int main() { // ... cout << "How many messages do you want to enter? "; cin >> size; array = new messages[size]; for (int i = 0; i < size; i++) { cout << "Message " << i+1 << ": "; [COLOR="Green"]//cin >> text;[/COLOR] [COLOR="Red"]getline( cin >> ws, text … | |
Re: you could also a. make the function const-correct b. use a size_t instead of an int for the size [code=c++]size_t smallestIndex( const int arr[], size_t size) { size_t smallestIndex=0; int temp=arr[0]; for(size_t i=1;i<size;i++) { if(arr[i]<temp) { smallestIndex = i; temp=arr[i]; } } return smallestIndex; }[/code] | |
Re: [code]void SnakeSeg::kill() { if (alive) { alive = false; erase(); links = 0; if (next) { delete next; [COLOR="Red"]next = 0 ; // isn't this required?[/COLOR] } } }[/code] | |
| |
Re: when you write a program, you express C source files as text lines containing characters from the source character set. when a program executes in the target environment, it uses characters from the target character set. these character sets are related, but need not have the same encoding. this is … | |
Re: you could also use [ICODE]basic_string<>::find_first_not_of[/ICODE] and [ICODE]basic_string::find_last_not_of[/ICODE] to locate the characters at either end that are illegal in an email address. and the use [ICODE]basic_string<>::substr[/ICODE] to extract the email address. | |
Re: or you could use the non-viral [I]libcurl[/I]. [url]http://curl.haxx.se/libcurl/[/url] [code=c++]#include <curl/curl.h> #include <iostream> #include <algorithm> #include <iterator> struct stats { stats() : bytes_recd(0), ncalls(0) {} int bytes_recd ; int ncalls ; }; size_t on_data( void* buffer, size_t size, size_t nmemb, void* pv ) { size_t nbytes = size * nmemb ; … | |
Re: > How do I pass a file or filename to a constructor? write a constructor that takes a file name as the argument. > And how do I retrieve "word_list" if I can't return from a constructor? let the constructor store the words in a member variable. and provide an … | |
Re: > ... develop a program that hides program files that should be kept out of sight in order to prevent them deleting any important files. what right do you have to try to prevent users from deleting files on *their* computers? to protect users from accidental deletion or overwrite, set … | |
Re: > printing out the values to make sure they are correct they now look like this ... the initial default precision for a stream is 6. to print out more decimal digits, do something like [CODE]std::cout << std::fixed << std::setprecision(8) // 8 digits *after* decimal point << value ; [/CODE] … | |
Re: > VB will teach you a lot of things that are common to nearly all languages, > such as data types (integers, strings etc), loops, and file systems true. perhaps you could learn a language which is radically different from the mainstream ones (which are all alike in many respects). … | |
Re: #include <iostream> #include <string> using namespace std; // identifier messages causes a name clash with the facet // because of the using directive (using namespace std) struct message_t { string message; int hours; int minutes; int seconds; }; int main() { int size; message_t* array; cout << "How many numbers?"; … | |
Re: all literals (including literal strings) are immutable. they are [B]const[/B]. and therefore cannot be sorted (or modified in any way) [code]//char* a = "acbacb"; // bad programming practice [COLOR="Red"]const[/COLOR] char* a = "acbacb"; // the correct way char b[] = "acbacb"; // also ok. a literal can be used to … | |
Re: > Basically, 2 numbers are randomly generated, and are added together > to put in the variable 'result'. ...What I want to do is store every value and > take an average at the end, to get an average of the results it gives me. using [ICODE]rand()[/ICODE], each pseudo random … | |
Re: not really. try 1.0, 2.0, 3.0, 4.0, 10.0 what is required is the number with the ordinal rank of 3. using if as the *only* (control) statement? here's how you could do it for three integer numbers. [code=c++]#include <iostream> #include <algorithm> int main() { int lowest, /*low,*/ middle, /*high,*/ highest … | |
Re: you can't deactivate precision control. the precision is initially 6 and is always used for floating point (and never for any other type). so you could either make [ICODE]angle[/ICODE] an [ICODE]int[/ICODE] you can also write [code]cout << "sin(" << angle << ") = " << fixed << showpoint << setprecision(4) … | |
Re: well, if you just need to count the number of words that end in punctuation, you do not need to store it in an array or a vector first. assuming that words are whitespace separated, read word by word into a [B]std::string[/B] till there is nothing more. [code]std::string word ; … | |
Re: JIT Debugging is an abbreviation for just-in-time debugging. all that it does is allow a debugger to be attched to a process which was not running under a debugger so far. see [url]http://msdn2.microsoft.com/en-us/library/ms679295(VS.85).aspx[/url] > some system file on the computer will take care of this Exception. no one will take … | |
Re: this is not a valid c++ program (size of a c-style array must be a constant known at compile time). [code=c++]int main() { // ... int row_size = 1 ; // ... int new_row[row_size*4] ; // error: ISO C++ forbids // variable-size array 'new_row' // ... }[/code] this is (size … | |
Re: [code]void gettemps(int temp[], int numtemp) { int count = 0; while (count != numtemp) { cout << " Enter " << count << " :00 " << " temperature "<< endl; cin >> temp[COLOR="Red"][count][/COLOR] ; if (temp[COLOR="Red"][count][/COLOR] >= -51 && temp[COLOR="Red"][count][/COLOR] <= 131) { hourlytemps[count] = temp[COLOR="Red"][count][/COLOR]; count ++; } … | |
Re: > easy to learn, light -weight... TinyXml++ perhaps? [url]http://code.google.com/p/ticpp/[/url] | |
Re: [QUOTE=hammerhead;548015] [code=c++]int eig( int num ) { if( num<10 ) return num; else return eig( eig( num/10 ) + (num%10) ) ; }[/code][/QUOTE] cool! though you spoiled it by [ICODE]void main()[/ICODE] and [ICODE]#include<iostream.h>[/ICODE] | |
Re: the c++ standard only specifies the performance requirements: for insert, erase and find this is [B]O( log N )[/B]. to meet this requirement, any tree implementation must be (reasonably) balanced at all times. | |
Re: [code=c++]#include <iostream> #include <iomanip> int main() { // std::cout << std::setiosflags(std::ios::fixed) // << 22.6 << '\n' ; std::cout << std::fixed << 22.6 << '\n' ; // easier // std::cout << std::setiosflags(std::ios::hex) // << 34 << '\n' ; std::cout << std::hex << 34 << '\n' ; // easier }[/code] | |
Re: > So, if the user inputs 1234 for num, can I make a structure > where the name equals the value of num, i.e. Serialnumber 1234? 1234 is not a valid C++ identifier. but ven for a valid identifier ( like sn_1234 ), [B]the short answer to your question is … | |
Re: > What type of syntax do I need to set my class to be of type comparable? nothing at all; just declare and implement the comparison operator(s). in [code] template<class Comparable> class BinarySearchTree; [/code] Comparable is just a place holder; the above is equivalent to [code] template< class T > … | |
Re: it would be easier (and also more efficient) if you use a cliext::vector<> instead of a std::vector<> note: requires VC++ 9 (2008) [CODE]#include <cliext/vector> using namespace cliext ; // ... { // ... vector< String^ >^ vec = gcnew vector< String^ > ; vec->push_back( comboBox1->SelectedItem->ToString() ) ; String^ Box1 = … | |
Re: > The error message probably means that you should have a statement of some sort between each if and each else. >> No it doesn't. It is legal to have nothing in the TRUE position of an if yes, it does. having nothing is not legal C++. however, the statement … | |
Re: > What i expect after restore a network connection it should work proper the problem is that the FILE structure that is being passed to fprintf is no longer usable. it contains an operating system file descriptor (handle) which is not valid any more. you really do not need to … | |
Re: > How do I write three different calling statement to the MyFunction function > without declaring additional variable. what kind of question is this? the second parameter must be [B]I[/B]. there are an infinite number of ways in which the first parameter could be a bool. [CODE]MyFunction ( B, I … | |
Re: the file is not in a good state after your loop executes [ICODE]file.eof() == true[/ICODE] clear the eofbit first. [CODE] file.clear() ; file.seekg( 0, std::ios::beg ) ; [/CODE] the seekg you are doing is ok because you are seeking to the beginning of the file. for a seekg to some … | |
Re: > what should be coming next in this list > Hello world>>data types>>conditional execution>>C-style arrays>>C-style > strings>>loops>>functions>>structs>>classes templates, perhaps? > That book is very costly, and not available in any library near me Thinking in C++ 2nd Edition by Bruce Eckel is pretty ok and is available as a free … | |
Re: > How would I go about locating a file on the computer, and using it in my application? no really portable way of doing it (if you want a fast search). you could do this on unix [ICODE]system( "/usr/bin/locate 'file_name_glob' > files_found.txt" ) ;[/ICODE] and then pickup the complete path(s) … |
The End.