1,247 Posted Topics
Re: use an array. eg. [code=cplusplus] enum { MAX_STUDENTS = 16 }; Student students[MAX_STUDENTS] ; for( int i=0 ; i<MAX_STUDENTS ; ++i ) { // ... cin >> students[i].first_name ; // etc. } [/code] you should also modify function print_student to take const string& args (not void*). passing by reference is … | |
Re: [code=cplusplus] ps->first_name == (*ps).firstname ; // ok MyClass& ref = *ps ; ref.first_name ; // ok *(ps).first_name ; // error, equivalent to *(ps.first_name) ; // error, ps is a pointer // reason: operator precedence [/code] | |
Re: the stanard libary <stack> and <queue> are just container adapters, eg. [code=cplusplus] template< typename T, typename CNTR = std::deque<T> > struct queue ; [/code] they just adapt an implementation of a sequence container to provide the interface required for a stack/queue. [quote=Narue;372810]...You can also use a vector or deque as … | |
Re: [code] // ... base( int t ) : i(t) {} // initialize member i with t // ... [/code] | |
Re: [code=cplusplus] int howManyCatsFunction() { cout << "there " << (Cat::HowManyCats == 1 ? "is " : "are " ) << Cat::HowManyCats << " cats alive!" << endl; } [/code] you also need to write a copy constructor which increments Cat::HowManyCats to get the right number always. | |
Re: MIT's Technology Review magazine published an interview with Stroustrup in Nov, 2006: [B]'The Problem With Programming: Bjarne Stroustrup, the inventor of the C++ programming language, defends his legacy and examines what's wrong with most software code.' [/B] link: [URL]http://www.technologyreview.com/Infotech/17831/[/URL] this generated such heated debate for an example, see: [URL="http://it.slashdot.org/article.pl?sid=06/12/05/0045234%29"]http://it.slashdot.org/article.pl?sid=06/12/05/0045234 [/URL] … | |
Re: [quote=laconstantine;371241]...c++ takes function name as an address of it...[/quote] c++ takes the name of the function (as an expression) to be either a reference to the function or a pointer to the function. eg. [code=cplusplus] #include <iostream> void foo( void(*pfn)() ) { std::cout << "pointer\n" ; } void foo( void(&rfn)() … | |
Re: you are probably trying to enter a name that has embedded (white) spaces. to read an entire line (may contain white spaces) use getline. getline( cin, employee ) ; | |
Re: you are using a 16 bit compiler. which one? | |
Re: you need to use a CPaintDC (not a CClientDC) in a handler for OnPaint (WM_PAINT requires calls to BeginPaint/EndPaint). it would be much better (and easier) if you override the virtual function CView::OnDraw( CDC* ) and write your code for painting the view there (and not implement OnPaint yourself). let … | |
Re: if you are using the current version of platform sdk: header is [B]winsock2.h[/B] import library (link to) [B]ws2_32.lib[/B] shared library (dll) [B]ws2_32.dll[/B] | |
Re: [code=cplusplus] private: System::Void button1_Click( System::Object^ sender, System::EventArgs^ e) { textBox1->Text = __box( randNumGen->Next(0,41) )->ToString(); } [/code] | |
Re: this would probably be more readable. and as efficient with a decent compiler. [code=cplusplus] struct client { explicit inline client( int a, int b, const std::string& n ) : aa(a), bb(b), name(n) {} int aa; int bb; std::string name ; }; extern std::vector<client> vec ; extern std::istream file ; int … | |
Re: [quote=afflictedd2;369935]The reason I have to give this a fixed size, is becuase I have to map this in shared memory...[/quote] just placing the map in shared memory is insufficient; you would need to a. write a custom allocator (compatible with std::allocator) that can (sub)allocate from the shared memory region for … | |
Re: at the place where the definition of the array is visible, sizeof will give the size of the entire array. it is when you use the name of the array in an expression that it is treated as a pointer to the first element. [code=cplusplus] #include <iostream> int main() { … | |
Re: convert a string to int this way. [code=cplusplus] string test = "1234" ; istringstream stm(test); int num = 0; stm >> num ; // if using boost (boost lexical cast), it is easier num = boost::lexical_cast<int>(test) ; [/code] | |
Re: [quote=Narue;364327]>This will work in C++ wht abt c? No, it won't work in either because you can't call a function at global scope....[/quote] calling a function at global scope is certainly possible in C++. dynamic initialization of statics at global scope is supported; here caling printf to initialize a global … | |
Re: [code=cplusplus] #include <iostream> #include <string> #include <cassert> int main() { std::string city = "Memphis" ; std::string state = "Tennessee" ; assert( city.size() < 16U ) ; std::string result = city ; result.insert( result.end(), 16U-result.size(), ' ' ) ; result += state ; std::cout << result << '\n' ; // or … | |
Re: [quote=Zay;359173][COLOR=red][COLOR=dimgray]for checking if word is Palindrome-[COLOR=teal]madam[/COLOR]-. or not by push half of the word in a stack and then compare it with the other half of the word)[/COLOR][/COLOR][/quote] why do you need a stack at all? [code=cplusplus] template< typename CHAR_TYPE> inline bool is_palindrome( const CHAR_TYPE* begin ) { const CHAR_TYPE* … | |
Re: as a matter of principle, do not write [code] char *ptr = "String Literal"; [/code] even if your compiler does not complain about it. instead always write [code]const char* ptr = "String Literal"; const char* const ptr2 = "another literal" ; // more usual [/code] programming is much simpler if … | |
Re: get to the command line terminal, cd to the directory and type [B]copy /A *.txt all.txt [/B]you will get one big text file. i'm not sure about microsoft (or any other) word, but it should be possible to convert a text file to a word processing document. | |
Re: looks like a buffer overflow in the line [code] fscanf(fp, "%s %s %s %s",part1,part2,part3,part4); [/code] the contents of the file is something that is not under programmatic control; a secure alternative should be used. | |
Re: processes do not have names; images (executable files) usually have names. | |
Re: [code=cplusplus] struct eq_course_number { explicit eq_course_number( int n ) : course_num(n) {} bool operator() ( const course& c ) const { return c.get_number() == course_num ; } int course_num ; }; // cin >> target; if( remove_if ( cache.begin(), cache.end(), eq_course_number(target) ) != cache.end() ) cache.pop_back() ; [/code] an alternative … | |
![]() | Re: [quote=iamthwee;358319]An array of objects should be handled in a similar fashion to an array of ints or strings for that matter.[/quote] this is possible only if there is an accessible default constructor. |
Re: [quote=Rashakil Fol;356500]Maybe [code]int i = p * q << 12; int j = (i >> (sizeof(int)-1) * CHAR_BIT) & (~0xF) i = (i & j) | (~ j);[/code][/quote] this would be about twice as slow as the original code. and the code size would also be close to double the … | |
Re: unless the size is known at compile time, or there is an externally imposed constraint, it would be easier and less error prone to use a std::vector rather than a c-style array. [code=cplusplus] #include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> #include <cassert> #include <algorithm> #include <iterator> using … | |
Re: it does print CleanUP; try it from the command line in a terminal. instead of (double) clicking the executable. | |
![]() | Re: [code=cplusplus] #include <iostream> #include <string> using namespace std ; struct account { explicit account( const string& name, double amount = 0.0 ) : _name(name), _amount(amount) {} // ... private: const string _name ; double _amount ; }; int main() { string name ; double amt ; cout << "enter name … |
Re: [quote=jenymaru08;356472] can i ask what is the purpose or use of iomanip in C++ ?[/quote] to produce formatted output, or to perform operations like flushing the stream, the stream classes privide member functions. eg. [code=cplusplus] std::cout.width(10) ; std::cout.fill('0') ; std::cout.setf( ios_base::fixed, ios_base::floatfield ) ; std::cout.precision(2) ; std::cout << 100.2 << … | |
Re: a. [URL]http://www.boost.org/libs/serialization/doc/index.html[/URL] b. [URL="http://www.gnu.org/software/commoncpp/"][U][COLOR=#0000ff]http://www.gnu.org/software/commoncpp/[/COLOR][/U][/URL] c. [URL="http://www.sourceforge.net/projects/eternity-it/"][U][COLOR=#0000ff]www.sourceforge.net/projects/eternity-it/[/COLOR][/U][/URL] d. [URL="http://www.s11n.net"][U][COLOR=#0000ff]www.s11n.net[/COLOR][/U][/URL] e. [URL]http://msdn2.microsoft.com/en-us/library/caz3zy5s(VS.80).aspx[/URL] | |
variations of this are being posted repeatedly here. hope this will clarify things once and for all. [code=c] // to choose a random element from a sequence when // a. you do not know how many elements are there before hand // b. you want to make one single pass … | |
Re: if you are comfortable with c++ (ie. you can do more than write c code using c++ syntax), you would find the spirit recursive descent parser framework (part of the boost libraries) easy to use. internally, it uses template meta-programming techniques; but to use it productively, you just need to … | |
Re: [quote=Thinka;354794]Actually, my idea is to run rand (not sure how many times I'll need to), and to then say for each time it returns a number, the program should check that current number with the previous one; if the current number is larger than the previous one, it gets printed … | |
Re: you are getting the error here. [code]my_reverse_iterator p(it);[/code] this is because my_reverse_iterator is a template class; you are not using it as such. change that to [code] my_reverse_iterator<vector<int>::iterator> p(it); [/code] and you can get started on the rest of the class. i presume you are not allowed to use the … | |
Re: the easiest way that i know is to use the boost tokenizer library. [code=cplusplus] #include<iostream> #include<boost/tokenizer.hpp> #include<string> #include <fstream> int main() { using namespace std; using namespace boost; ifstream file( __FILE__ ) ; string str ; while( getline( file, str ) ) { tokenizer<> toker( str ) ; for( tokenizer<>::iterator … ![]() | |
Re: the function(s) [B]next_permutation[/B] in <algorithm> would generate the next permutation. you could call that repeatedly. | |
Re: the ordering of data declared as bit fields is implementation dependant. you have to test and figure out what it is for your compiler. [code=cplusplus] #include <iostream> int main() { // assumes that the ordering of data declared as bit fields is from // low to high bit. this is … | |
Re: [quote=joeprogrammer;353091]in fact the performance loss is so minimal, it's hardly worth taking that into account when trying to decide when and where to use exception handling.[/quote] not true. exceptions are designed with the idea that a. you do not have to pay a high performance penalty in the event of … | |
Re: construct a bitset using the string and then convert it to a ulong. [code=cplusplus] #include <iostream> #include <string> #include <bitset> #include <limits> using namespace std ; int main() { string str = "00000100" ; cout << bitset<numeric_limits<unsigned long>::digits>(str).to_ulong() << '\n' ; } [/code] | |
Re: [code] enum { N=20, M=50 }; int(*a)[M] = malloc( sizeof( int[M] ) * N ) ; // C int(*b)[M] = new int[N][M] ; // C++ [/code] | |
Re: this kind of functionality is required only when the generic or metaprogramming paradigms are used. otherwise, when you write a function in a statically typed language, you already know the type of every expression. for a tutorial on discovering and using information about types, see this: [url]http://www.boost.org/doc/html/boost_typetraits/background.html[/url] these books would … | |
Re: [quote=jrivera;350578] what is the definition of the + operator for STL vectors? template<typename T, typename Allocator> vector<T, Allocator> &operator+ (const vector<T, Allocator> &a, const vector<T, Allocator> &b) and do I just code this outside of main?[/quote] yes, you define it as a global. however, there is a logical error in … | |
Re: [code=cplusplus] #include <iostream> #include <vector> #include <map> #include <string> #include <algorithm> using namespace std; typedef map<string,int> simap ; typedef simap::iterator miterator ; typedef vector<miterator> ivector ; ivector find_values( simap& m, int val ) ; int main() { simap map ; map["one"] = 1; map["two"] = 2; map["three"] = 3; map["four"] … | |
Re: what you need is integer division. in your program, temp is a float. float(123)/100 will give you 1.23, not 1. and a standards-compliant compiler would give you a compile-time error if you try float(123)%100 | |
| |
Re: [quote=Mac.Z;348625]let's say the user enters a value in this format xx:xx:xx, where xx is an integer, how can I assign each xx to integer vars? [/quote] this is one way. [code] #include <string> #include <iostream> #include <algorithm> #include <sstream> using namespace std ; int main() { string str ; getline( … | |
Re: [quote=tech291083;350286]Thanks, Fully agree with your reply, but I just want to see if it is possible to tell the compiler to display the number/output in a manner (with all the zeros/digits in it) that one wants to. Cheers...[/quote] [code] stm << fixed << setprecision(200) << number << '\n' ; [/code] … | |
Re: [quote=jerryseinfeld;348623]last question :( I heard some bubblesort thing.. what is it.. is it suitable for me???[/quote] yes jerry, it would be very suitable for you. you would enter the struct (with name, year, doctor, illness as member variables) into an array and use a bubble sort (not the best sort … | |
Re: [quote=addicted;349316]If yes, Help with the declaration[/quote] [code] template< typename T > inline void swap( T& a, T& b ) ; // declaration (function) template< typename T, size_t N > struct array ; // declaration (class) // definition of function template< typename T > void swap( T& a, T& b ) … |
The End.