1,247 Posted Topics
Re: > I want to give the customer a 'CurrentAccount' which is inherited from Account; and then store it in an array of Accounts. > How do I go about doing this? You can't. `Account acnts[10];` holds objects of type Account ***by value***. When you try to store an object of … | |
Re: In general, 1. try to represent an elusive concept as a class - objects are more flexible than code. 2. C++ is one of the few programming languages with a strong sense of initialization and deinitialization - exploit it. For example: #include <iostream> int recurse( int n ) { static … | |
Re: Read 20 chars into a `std::strin`g: #include <fstream> #include <string> int main() { std::ifstream file( "file.txt" ) ; std::string str ; enum { NCHARS = 20 } ; // read upto NCHARS chars into str str.reserve(NCHARS) ; // not essential file >> std::noskipws ; // do not skip white space … | |
Re: > why the remove and rename function cannot perform? > everytime remove the original (hello.txt), it still remain. Probaly because you have not closed the files before attempting these operations. #include <cstdio> #include <fstream> #include <string> #include <iostream> std::string replace( std::string line, const std::string& substr, const std::string& replace_with ) { … | |
Re: You need to link both the DLL and the exe to the same instance of the run-time library. Ie. link both with the shared version of the standard library: either **/MDd** (Debug) or **/MD** (Release) | |
![]() | Re: > How to print a sentence (including white spaces) from a file ? > Or How to print entire text file which has a paragraph? Assunming that you have a compiler that is not antique: #include <iostream> #include <fstream> #include <string> int main() { std::string filename ; std::cout << "file … |
Re: A fast (very fast) implementation of the Sieve of Atkin: [url]http://cr.yp.to/primegen.html[/url] The less well known Sieve of Sundaram: [CODE=C++1x]#include <vector> #include <algorithm> #include <iostream> // sieve of sundaram (naive implementation) std::vector<int> generate_primes( int N ) { const int M = N / 2 ; std::vector<bool> sieve( M, true ) ; … | |
Re: Ideally use a `std::vector<>` to hold the names and numbers. If you **must** use arrays (you have a teacher ... ), you need to keep track of the size manually. Something like: struct PhoneBook { // ... void add( const Name& n, const PhoneNumber& p ) { if( current_size < … | |
Re: `std::array<>` is an **aggregate** which wraps a C-style array. It has no user-defined constructors. `{1,2,3,4,5}`is therefore not interpreted as a brace-enclosed `initializer_list<>` The situation is analogous to: struct X { int a[2] ; } ; X one = { 1, 2 } ; // ***error: missing braces around initializer for … | |
Re: //double getMean( double scores[], int numScores ) double getMean( const double scores[], int numScores ) // **** const { /* // You may want to ignore this bit for now. using limits = std::numeric_limits<double> ; if( numScores < 1 ) return limits::has_quiet_NaN ? limits::quiet_NaN() : limits::signaling_NaN() ; */ // int … | |
Re: Something like this: // C++11 std::string generate_file_name( const std::string& name, const std::string& college ) { static int slno = 0 ; return name + '_' + college + '_' + std::to_string( ++slno ) ; } // or C++98 std::string generate_file_name( const std::string& name, const std::string& college ) { static int … | |
Re: Write it in two stages, perhaps. You already have the code for converting a decimal number to some specified base. Write the code to convert in the reverse direction - from some specified base to decimal. Now conversion from base **A** to base **B** can be done in stages: * … | |
Re: > How is that the "My Name.." bit, which I believe to be a const char* or string literal > be somehow resolved to a String object for the purposes of the function. the String class has a (non-explicit) single argument constructor which takes a [ICODE]const char *[/ICODE]. this also … | |
Re: Split the line using a `std::istringstream` ? #include <vector> #include <string> #include <sstream> std::vector< std::string > split_into_ws_separated_columns( const std::string& line_from_file ) { std::vector< std::string > columns ; std::istringstream stm(line_from_file) ; std::string col ; while( stm >> col ) columns.push_back(col) ; return columns ; } | |
Re: See: http://www.daniweb.com/software-development/cpp/threads/438942/flter-a-log-file-using-c#post1886475 | |
Re: Something like this: #include <iostream> #include <vector> template < typename T > struct my_class : public std::vector<int> { typedef std::vector<T> base ; using base::iterator ; // etc explicit my_class( const char* n ) : name(n) {} // ... private: std::string name ; friend inline std::ostream& operator<< ( std::ostream& stm, const … | |
Re: > my task is to type 20 integer numbers from 0 to 99 as the input data. > But i have problem for my next task, which is to display prime numbers from the input data. 2, 3, 5 and 7 are prime numbers. If an integer between 8 and … | |
Re: > Now I just have to alter it so it doesn't read in repeat words in the book. The simplest way would be to use a `std::set<>` or `std::unordered_set<>` to filter out the non-uniqe words. #include <iostream> #include <fstream> #include <string> #include <set> #include <vector> #include <iterator> // http://en.cppreference.com/w/cpp/container/vector std::vector< … | |
Re: void encode() { char userInput[512], direction[32], keyword[32]; int transposition; cout << "Enter the text to be encoded: "; cin.get( userInput, sizeof(userInput) ) ; // unformatted input cout << "Enter direction (Forwards or Reverse): "; cin.get( direction, sizeof(direction) ) ; // unformatted input cout << "Enter transposition value (0...25): "; cin … | |
Re: Let us say we have a function: // return a string with all occurrences of substr in str replaced with replacement std::string replace_all( const std::string& str, const std::string& substr, const std::string& replacement ) ; This function can be called with many different input sets (each set being a tuple of … | |
Re: > The singleton design pattern is unique in that it's a strange combination: its description is simple, yet its implementation issues are complicated. - Andrei Alexandrescu in 'Modern C++ Design' If Animal is a singleton - that is a second object of type Animal cannot exist - its singletonness can … | |
Re: > I can't figure out how to use the cin.peek for the extracting numbers or charachters from the randomly typed input `std::istream::peek()` will retrieve, but will not extract the character from the input buffer (the charaxter remains in the input buffer). http://en.cppreference.com/w/cpp/io/basic_istream/peek Use `std::istream::get()` to read and extract the next … | |
Re: C++11: `std::to_string()` http://en.cppreference.com/w/cpp/string/basic_string/to_string | |
Re: Use `std::map<>` with `std::pair< e_ServerName1, e_ServerName2 >` as the key_type and `e_UserName` as the mapped_type. http://en.cppreference.com/w/cpp/container/map #include <map> enum e_UserName { one, two, three, four /* etc. */ } ; enum e_ServerName1 { Venus, Mars, Earth } ; enum e_ServerName2 { Jupiter, Pluto, Neptune, Saturn } ; int main() { … | |
Re: The Nuwen build of MinGW includes a number of pre-built libraries; **libjpeg-8d** is one of them. http://nuwen.net/mingw.html Nuwen MinGW is distributed as a single self-extracting archive and can be installed from the command line. | |
Re: To read a whole line (including whitespace) into a string, use `std::getline()` http://www.cplusplus.com/reference/string/getline/ To read the contents of a file, where each data set consists of the name of a state followed by three numbers, the code would be something like: std::ifstream file ("list.txt") ; std::string name_of_state ; int number … | |
Re: > I am facing problem using float > in loop its value stuck at 8388608.00 First read up a bit on the representation of floating point numbers and loss of precision. http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_representation.html Then, try out this program: #include <limits> #include <iostream> #include <iomanip> int main() { typedef std::numeric_limits<float> limits ; … | |
Re: +1 Gonbe and deceptikon. People do learn by going through, reasoning about, and then understanding a solution to a problem. Someone learning programming is not all that much different from a child learning to speak - the child learns by listening to people speak correctly. | |
Re: Something like this: enum { N = 1024*1024*1024 } ; // 1G times double value = 1.0 / 3.0 ; double plus_result = 0 ; for( int i=0 ; i < N ; ++i ) plus_result += value ; const double multiplies_result = value * N ; std::cout << std::fixed … | |
Re: > but now the problem I am having that it only deletes repeated pairs not all repeats. How can I make it so that it deletes all the repeats in the word? If you are allowed to have a different order of chars in the result, just sort the original … | |
Re: As of now, in terms of strict conformance, clang with its libc++ is ahead of the rest of the pack. http://clang.llvm.org/ It is also the best compiler for people starting out to learn C++; clang's clear and expressive diagnostic messages are much easier for beginners to understand. (GCC 4.8 has … | |
Re: > The last line is matching, but because it thinks the array size is zero it drops out and states that it doesn't match. Haven't looked at any of the logic except this: while(inFile) // the file stream is not yet at eof after the last line has been read … | |
Re: > i need to restrict the user from inputting an integer and string when inputting a char. Read the input into a string. Verify that the string consists of precisely one non-digit character. char get_single_nondigit_char() { std::string input ; while( std::cout << "enter a single non-digit, non-whitespace char: " && … | |
Re: C++11: http://en.cppreference.com/w/cpp/io/manip/put_money | |
Re: > As far as I can tell, the main reason to declare a data member as const is to remind the user of that class not to change it's value. A const member variable is an object that is initialized during construction of the object and is never changed after … | |
Re: Use a `std::istringstream` to extract the tokens from the string. http://www.artima.com/cppsource/streamstrings3.html #include <iostream> #include <string> #include <sstream> #include <cmath> inline void error() { std::cerr << "error in input\n" ; } int main() { std::string input ; while( std::cout << "? " && std::getline( std::cin, input ) ) { std::istringstream stm(input) … | |
Re: > How long has C++ supported this? C++ has never supported this. It wasn't supported in 1998 and it still isn't supported today. Variable length array is a C99 feature. In C++, use `std::vector<>` if similar functionality is required. http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm > I am using Dev-CPP 4.9.9.2 with GCC version 3.4.2-20040916-1. … | |
Re: > Also, I would get the input from the user, and than form a string composed by the name and pet strings. Yes. Then randomly shuffle the string and pick the first six chars. Likwwise, for the string containing digits (pick the first two after a shuffle). Randomly shuffle the … | |
Re: In general, prefer to declare a variable closest to its point of use. Local variables should have no more visibility than absolutely required; define them when you need them, and let them go out of scope when you're done with them. For instance, for( unsigned int i = 0 ; … | |
Re: #include <cmath> inline double log_to_base( double value, double base ) { return std::log(value) / std::log(base) ; } | |
Re: > a template that converts an array of type T to an array of type char, which can then be used as the operand to sizeof to get the number of elements: [code]template <typename T, size_t N> char (&array(T(&)[N]))[N];[/code] another way to use the same template idea (which i find … | |
Re: 1. the names **x**, **v**, **f** and **i** can be unambiguously resolved by name look-up; except that **f** may be the name of a function with two or more unambiguous overloads. 2. **v** is either a pointer (other than pointer to possibly cv-qualified void) or an object which has an … | |
Re: In ArrayList.cpp, do not redefine the type `ArrayList` That would be a violation of ODR. http://en.wikipedia.org/wiki/One_Definition_Rule Instead, `#include "ArrayList.h"` > I assume the problem is there is no operator != in the Country.h file. Yes. The problem is that there is no `operator !=` anywhere. > our instructor told us … | |
Re: Date.h requires an include guard. http://en.wikipedia.org/wiki/Include_guard Either: ////////// date.h //////////// class Date { // ... bool sameMonth( string aMonth, string aMonth2 ) const ; // *** const added }; //////// date.cpp /////////// #include "date.h" bool Date::sameMonth( string aMonth, string aMonth2 ) const // *** note the Date:: { return aMonth … | |
Re: > Dudes I am newbie to C++ and forums. I thought i was helping that dude. Fair enough. Perhaps you should have waited till marius2010 demostrated that a genuine attempt to solve the problem was made. Code snippets are a genuine aid in learning programming - one good snippet is … | |
Re: > im writing a program that creates a dynamic array then input numbers(not exceeding 100). Why do you need an array with dynamic storage duration? You know the size of the array at compile time - 100 - and that is not a huge number. constexpr std::size_t ARRAY_SIZE = 100 … | |
Re: //////////////////// a.h ////////////////////////// class C ; class A { public: void setInfo(int age, C *ptrToC); }; //////////////////// b.h ////////////////////////////// #include "a.h" class B { private: A exampleofA; }; //////////////////// c.h ///////////////////////////////// #include "b.h" class C { public: void randomFunction(); private: A secondexampleofA; B exampleofB; }; /////////////////////// a.cc ////////////////////////// #include "a.h" … | |
Re: In string::const_iterator url_beg(string::const_iterator b, string::const_iterator e) { static const string sep = "://"; // ... return e; } the value returned is always **e** (end). Instead, return an iterator to the begin of the url. Something like: string::const_iterator url_beg( std::string::const_iterator b, std::string::const_iterator e ) { static const string sep = … | |
Re: Some other options are, OpenCV: You could just use OpenCv's Mat class with an OpenCV primitive data type; and then just store 0 or 1 as the values. cv::Mat matrix4( 100, 60, CV_8U ) ; Standard C++ library: #include <vector> #include <bitset> // fixed width matrix of bits std::vector< std::bitset<100> … |
The End.