420 Posted Topics
Re: This thread is missing the most important step IMO. Software development always starts out with the statement of a problem; that is to say that [i]Programming [b]IS[/b] problem solving[/i]. With this in mind, you cannot expect to design a solution without first defining, in detail, what exactly you're trying to … | |
Re: A character surrounded by single quotes is a single character value; a pair of double quotes indicates that its enclosed data is a [i]string literal[/i], which is represented in memory as a contiguous sequence of characters (a.k.a an array of characters) Strings and single characters are like apples and oranges … | |
Re: I hate to be a bore, but [b]Narue[/b] has written an extremely concise, in-depth post which is pinned at the top of this forum called [i]How do I flush the input stream?[/i] - Please pay attention to sticky threads in future :-) [url]http://www.daniweb.com/forums/thread90228.html[/url] | |
Re: Excuse me if I may play devil's advocate a little.. I think the wiki is an excellent idea in principle, however my personal experience of wiki projects which go largely unmanaged is that they tend to fall apart and rapidly decay into a worthless disorganised mess if certain guidelines and … | |
Re: I believe you may be somewhat re-inventing the wheel here - you can achieve the same result using a std::stringstream, an ostream_iterator and the std::copy algorithm :-) [CODE=cpp]#include <string> #include <algorithm> #include <iterator> #include <sstream> template<typename FwdIt> std::string to_string(FwdIt begin, FwdIt end) { //retrieve the value type for any kind … | |
Re: Write her a poem using your favourite programming language! :D | |
Re: [QUOTE=griswolf;1251619]Rule one of optimizing: Find a better algorithm. [/QUOTE] I disagree there. Rule one of optimising: DON'T! at least, not until you have a working program which has been thoroughly tested and the result of that testing has shown that it actually [i]needs[/i] optimising; at which point, careful analysis needs … | |
Re: You can use [i]getline[/i] to read from a stringstream buffer upto a delimiter, then push each word into a vector (a C++ array) [CODE=c++]#include <string> #include <vector> #include <sstream> int main() { std::string line = "word1:word2:word3:word4"; std::istringstream buffer(line); std::vector<std::string> words; std::string word; while( std::getline(buffer, word, ':') ) words.push_back(word); } [/CODE] | |
Re: [b]Narue[/b] has a good linked list tutorial on her website here [url]http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_linklist.aspx[/url] If you're trying to wrap your head around the concept of linked lists, start out with something simple first - a singly-linked list which represents a stack is by far the easiest to write (because its simpler to … | |
Re: I don't really see how knowing "why" actually helps, although if you think about what inheritance represents, it should be obvious - a base class represents generalised components which are common to all objects in a heirarchy - essentially a base class is a 'sub set' of its derived classes … | |
Re: Well I don't usually listen to music while programming (if i'm at work, I'm usually keeping an ear out for things happening around me), but here's my take on it :D Visual Basic - Lady Ga-ga. Naff, Cheesy, Only appeals to teenagers. You probably wish you'd never heard of it … | |
Re: There are several beginner level books which often come highly recommended by the real experts/guru's in the C++ community; i.e. those people who understand the language, and also understand the correct way to teach it. - [b][i]"Accelerated C++"[/i][/b]: [url="http://accu.org/index.php?module=bookreviews&func=search&rid=1185"]http://accu.org/index.php?module=bookrevie...ch&rid=1185[/url] - [b][i]"You Can Do It!"[/i][/b]: [url="http://accu.org/index.php?module=bookreviews&func=search&rid=470"]http://accu.org/index.php?module=bookrevie...rch&rid=470[/url] - [b][i]"C++ Primer 4th Ed"[/b][/i]: … | |
Re: This seems somewhat unnecessary to me - the STL will already do this for any container which supports bidirectional iterators - all of those containers support begin/end and rbegin/rend, so you can use the std::equal algorithm [CODE=cpp]#include <functional> #include <algorithm> #include <string> #include <vector> #include <iostream> int main() { std::string … | |
Re: I agree with daviddoria. C++ has taken several evolutionary steps beyond C. The first (and probably most difficult) step for you to take, is to get out of the C mindset of doing everything the long way, using arrays and pointers for everything. Instead focus for energy on learning the … | |
Re: Andrei Alexandrescu came up with a way to check convertability between types at compile time as part of his Loki library [url]http://loki-lib.sourceforge.net/[/url] - you could use a similar trick to enforce the types which are allowed to use your container [CODE=cpp]class base {}; class derived : public base {}; template<typename … | |
Re: [QUOTE=joydsouza90;295556]hey thanx...but i want to know the exact syntax i need to write to make a rectangle move when a user presses the right or left key. I'm new to Turbo c++ and i would really need all the help i can get. Thanx a lot[/QUOTE] Start with something a … | |
Re: Both the OP and the 1st reply could benefit from this link [url]http://www.parashift.com/c++-faq-lite/friends.html[/url] | |
Re: if the base class were responsible for cleaning up memory within its destructor, then stopping that code being called would of course cause a memory leak. I can't see how that could benefit you, unless you have some reason why you'd intentionally not want that memory to be released - … | |
Re: This seems to be the age old 'artificial' problem that your classes are (at least in part) fundamentally unrelated to each another, but regardless of that they have an established relationship anyway; i.e, your classes do not share a common interface - the fact that they each have a function … | |
The C++ [i]string[/i] type can be converted to a [b]const[/b] null-terminated string using [icode].c_str()[/icode] for interoperability with legacy C code. However, this poses a potential problem when mixing [i]std::string[/i] with functions designed to mutate a string in-place. a const_cast to get around this issue results in undefined behaviour, since the … | |
Re: I believe private inheritance more accurately represents "implemented in terms of"; which is slightly different to 'has a'; the main difference being that a privately inherited implementation is guaranteed to be initialised before a class' own private members. Something which is associated with a few interesting initialisation tricks. | |
Re: Posting the errors you get is never pointless; People reading your post aren't psychic, so we have no idea what errors you're seeing; and they might try to compile it under a different compiler to you, which means they may get different errors (Especially if you use non-standard libaries like … | |
Re: [QUOTE=ayesha91;1091874][CODE] int length(string word) { int len=0; for(int i=0; word[i]!=NULL; i++) len++; return len; } [/CODE][/QUOTE] This function is completely unnecessary - it could also lead to undefined behaviour since the underlying representation of std::string is not necessarily "null terminated", which means that you could quite easily crash your program … | |
Re: Some would argue that [i]get[/i] and [i]set[/i] methods themselves are a symptom of bad design, but even in those situations, they are still superior than providing direct access to a private member variable because they at least do you the service of preventing someone from obtaining a pointer-to your class' … | |
Re: [QUOTE=kevintse;1088816]Here I know what exactly happened, and I want to show this message to the user [/QUOTE] This is your major problem - exceptions should not be used for handling user errors, and users shouldn't need to know about exceptions which have occurred in your program - That is to … | |
Re: p->prev looks to be a pointer: [icode]p->prev.next [/icode] did you intend to do this instead? [icode]p->prev->next [/icode] | |
![]() | Re: If you're doing this in C++, you might want to think about a vector of lists, e.g. [CODE=cpp]#include <vector> #include <list> int main() { std::vector< std::list<int> > lists; std::list<int> new_list; new_list.push_back(1); new_list.push_back(2); lists.push_back( new_list ); } [/CODE] As for a 'C' implementation: Do you know how to create an array … ![]() |
Re: [QUOTE=pspwxp fan;1090120][code=c] //main.cpp #include <iostream> #include "player.cpp" int main(){ players(); return 0; } [/code] Note that i've included player.cpp, not player.h (Which i accidentally named header.h, soz) [/QUOTE]This is a terrible way of doing things, and can easily end up in creating a mess - please don't encourage others to … | |
Re: Probably the easiest way is to use a map (An associative container), which lets you associate "keys" (identifiers) with values/objects.[CODE=cpp] hello world; world.name = "Earth"; world.size = 100; hello people; people.name = "Everyone"; people.size = 2; std::map<std::string, hello> table; table["world"] = world; table["people"] = people; [/CODE] STL maps come with … | |
Re: Errors like this are hard to see when your bracket alignment is all over the place. The closing bracket before [icode]return 0;[/icode] is the end of a "do {}" block, so its expecting a while statement. On a more general note, you don't need to create all those identical structs … | |
Re: Templates and operator overloading are just tools like everything else in C++ - Use them when they're appropriate. If you understand what they're capable of, you'll be able to recognise for yourself where and when they might be useful; but if you'd like some specific examples, then look no further … | |
Re: That would be a contradiction in terms; an array whose size is decided at runtime is a dynamic array. If you mean you'd like a dynamic array where you don't need to worry about explicit memory management, then the STL vector is C++'s "dynamic array". [i]edit: But having seen what … | |
Re: The problem is in your Add function - When you read anything using [icode]cin >> [/icode], there'll be (at least) a newline character left over from when the user hit 'enter'; using cin.ignore() will discard one character, but if the user typed in any other junk, you might sometimes need … | |
Re: Do you have an example of input/output? e.g. using 'substr' and 'erase' on a std::string [CODE=cpp] std::string str = "Hello, World"; std::cout << str.append(str.substr(0,5)).erase(0,5);[/CODE] "Hello, World!" cut at position 5 and appended to the end becomes ", WorldHello" It would help knowing what your code is [i]supposed[/i] to do before … | |
Re: This is unrelated to your compiler error, however, you've added [icode]#include <string>[/icode] at the top of your program, but you're not using it; I imagine if you replaced every reference to [b]char[][/b] and [b]char*[/b] with [i]std::string[/i] instead, your program will get a lot easier. | |
Re: Modern C++ beginner books start out by introducing vectors early on, sometimes without mentioning arrays at all except for the final chapters where they're discussed in among the 'C' subset of the language. They do this for a good reason; C++ has been deliberately designed with the aim that programmers … | |
Re: My first guess would be that seed could mean a number to pass to the [icode]srand[/icode] function, for random numbers. [url]http://www.cppreference.com/wiki/c/other/srand[/url] Otherwise, the best person to ask is whoever provided you with the details of the problem | |
Re: [QUOTE=bijayswain;1080916]error C2146: syntax error : missing ';' before identifier 'ip1' this is the error when i compile it.[/QUOTE] Included in learning how to write C++ code is learning how to troubleshoot extremely common and self-explanatory compiler errors. The compiler has told you exactly what's wrong and where, why not try … | |
Re: [QUOTE=BSKI01;1076794]I know there are many things like this on this site already but my teacher is smart and will search to find out if we just copied from an internet interveiw [/QUOTE]So you figured that a good way to avoid being caught cheating would be to beg on an IT … | |
Re: Have a look at these reviews. The books listed are generally considered 'best of breed' beginners books in the C++ community - [b][i]"Accelerated C++"[/i][/b]: [url=http://accu.org/index.php?module=bookreviews&func=search&rid=1185]http://accu.org/index.php?module=bookrevie...ch&rid=1185[/url] - [b][i]"You Can Do It!"[/i][/b]: [url=http://accu.org/index.php?module=bookreviews&func=search&rid=470]http://accu.org/index.php?module=bookrevie...rch&rid=470[/url] - [b][i]"C++ Primer 4th Ed"[/b][/i]: [url=http://accu.org/index.php?module=bookreviews&func=search&rid=778]http://accu.org/index.php?module=bookrevie...rch&rid=778[/url] - [b][i]"Programming: Principles and Practice using C++"[/i][/b]: [url=http://blog.cplusplus-soup.com/2009/02/programming-principles-and-practice.html]http://blog.cplusplus-soup.com/2009/02/pro...d-practice.html[/url] | |
Re: [QUOTE=firstPerson;1071385]Well there are also other ways. Here is a another way : <snip> [/QUOTE] Did you post this to the correct thread? I don't see how that relates to anything that the OP wanted to do; they wanted to change the behaviour of a template class depending on its type. … | |
Re: Sequences of characters (i.e. 'words' or 'plain text') are known as [i][b]string[/b][/i]s. In C++, strings are represented as a type of data called [i]string[/i] [CODE=cpp]#include <string> //allows you to use 'string' #include <iostream> int main() { using namespace std; string name; cout << "What is your name?" << endl; getline(cin, … | |
Re: Think about the relationship described by inheritance, and why what you're doing isn't possible; [CODE=cpp]class Vehicle {}; class Bike : public Vehicle {}; class Car : public Vehicle {}; class Truck : public Vehicle {}; [/CODE]The relationship can be described as "IS A"; in other words, [i]Car IS A Vehicle[/i] … | |
Re: [QUOTE=tkud;1049254]hey, everyone. I am still learning templtes and I have 2 questions: 1-Why is it that all the examples of templates I have seen are about arrays?Is that all templates are used for? [/QUOTE]I suppose because containers are the most prominent and easy-to-explain examples when it comes to introducing templates … | |
Re: I would consider the 'safer' way to be to ditch the array and go with an STL container such as a vector. In fact, a [inlinecode]std::vector[/inlinecode] is often nicknamed a "C++ Array". They're automatically initialised since they start out empty by default, and resize automatically every time you "push" some … | |
Re: [i]Edit - Never mind, I didn't read the OP correctly[/i] | |
Re: You can do exactly the same as you have described for your statically allocated array, then create a new smaller array and copy all of your remaining elements to it (This is an inefficient and perhaps slightly naive solution, but its a simple one) | |
Re: [QUOTE=sunnypalsingh]Then why don't you suggest another way. :D[/QUOTE] How about this? Although it uses equality, which is technically a comparison operator. [INLINECODE]"Is i1 greater than i2?"[/INLINECODE] [CODE]bool greaterThan(const int& i1, const int& i2) { return ( ((i2-i1)==abs(i2-i1)) ? false:true ); }[/CODE] | |
Re: For a standard, safe way of getting the program to pause, you could use a function like this[CODE=CPP]#include <iostream> #include <limits> void stop() { std::cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n'); std::cin.get(); } int main() { stop(); }[/CODE] | |
Re: How simple? If you look around, there's usually loads of people listing their homework assignments, a few that I can think of from the top of my head: Vending machine simulator Student grades database Calculator Noughts and crosses game (AKA "tic tac toe" ) Shopping basket/Restaurant menu/other similar "business order" … |
The End.