2,712 Posted Topics
Re: Divide and conquer. In this case, if you break it up into function you will see that the logic is easy to see. [code] #include <iostream> using namespace std; void printN(const char c, int N){ while(N--) cout << c; } void printSeq(int start, int end){ while(start != end){ cout << … | |
Re: Realize that from your code, students and grades might not be the same size. You should instead do something like this: [code] for each students in the vector students:{ get student grades. } [/code] | |
![]() | Re: In your first post you can do this : [code] void print(const char*p){ cout << p << endl; } [/code] and call it like so : [code] int main(){ print("hello"); print("hello"); } [/code] Try that first |
Re: [B]psuedo-code:[/B] [code] create randomAges[N] = {0}: seedRng(); for (i = 0 : N){ randomAges[i] = rand(); } [/code] | |
Re: >>[B]I think vector::reserve() can cause a resize[/B] >>[B]The member functions vector::reserve()... both can cause re-allocation as well[/B] Just to let you know, your first statement is different from your second. reserve does not "resize" the vector as in vector::resize does, so just be weary of the lingo. And as for … | |
Re: [B]>>Well this forum likes assuming things doesn't it?[/B] Well you haven't mentioned it wasn't a h.w problem in your first post. And usually most threads here are h.w problem. So as you see, our default assumption is that this post is probably about some h.w problem. [B]>>It was automatically posted … | |
Re: >>std::auto_ptr Thats being deprecated. So I would avoid that. I would rather use boost smart pointers | |
Re: I think you are looking for reinterpret_cast<>? | |
Re: [B]1. Do you love what you do? why?[/B] Yes, otherwise I probably wouldn't do it often. [B] 2. Do you interact with other people or other engineers a lot?[/B] I guess, I am a college student in class with other engineering students. [B] 3. How dangerous is your job? Have … | |
Re: You can't really do that, but there might always be some hack. How about you create the most general lookup table[b]s[/b] and work with that? Maybe something like readMemoryFunctionTable, writeMemoryFunctionTable,... | |
Re: look into boost::any >>[B]I cannot predefine a structure because I dont know what data types I am gonna be given[/B] Why not? Well do you at least know if its a string or a number? ![]() | |
Re: What you showed above is almost like recursion : [code] void func(long i){ if(i > 0) func(i - 1); } [/code] Say "i = 5", then the functin func() will be called 6 times, because of the initial called. So your question is directly related to the question, "whats the … | |
Re: >>guessResponse( spec, tries) That should be : [i]guessResponse( spec, number)[/i] >>if (count = 10) That should be [i] if (count == 10)[/i] >>[B]void secretNum(int&);[/B] See the difference between that and this : [B]void secretNumber(int &num)[/B] | |
Re: I don't know if my link is appropriate, but what the hell. I visit my [URL="http://www.gamedev.net/community/forums/"]porn site[/URL] regularly. | |
Re: [B]Step 1: Declare variables[/B] [code] std::map<long,long> report; std::pair<long,long> info; std::ifstream file("data.txt"); [/code] [B]Step 2: Read in data [/B] [code] while( file >> info.first >> info.second ){ / * do stuff*/ } [/code] [B]Step 3: Apply algorithm [/B] [code] while( file >> info.first >> info.second ){ std::map<long,long>::iterator itr = report.find( info … | |
Re: Do you really need to have the data sorted every time when you add in data? | |
Re: Here is his code : [code] #include <stdio.h> #include <conio.h> int main() { char wew[7]; int smallest, largest, middle, intNo; int dividend=0; int modolus=0; int valid; do { printf("Enter a seven digit number:\n"); scanf("%s", wew); valid=0; // counter for no of digits for (int a=0;a< sizeof(wew);a++) { intNo=(int)wew[a]-48; // trap … | |
Re: And since it looks like you are already using classes, you can do something like this : [code] //creates a NxN maze template<typename T, int N> class Maze{ private: T _maze[N*N]; public: //some codes T& operator()(int row, int col){ //some validation assert(isPositive(row) && isPositive(col) ); assert(row*col < N*N); return _maze[ … | |
Re: >>[B] I want to Convert my input string to a const char[/B] The string class has a [I].c_str[/I]() member function, it returns a const char pointer. >>[B]Because I can not just convert a string directly to a integer[/B] Why can't you? What method are you using for the conversion? >>[B]Basically, … | |
Re: [code] const char *s = "1abcdef"; string str( (s+1) ); //copy from 'a' onwards. [/code] | |
Re: The data structure you need is hashmap or hashtable. Like always, boost got you covered. Look into their hashtable if you want. In a typical hashtable implementation, searching is O(1), that is, its constant. If you want to stay with stl, you can use std::map. Their search operation is usually … | |
Re: Are you serious? Narue's is gonna kill you lol. Your question is like comparing oranges to apples. Its really useless. | |
Re: And some food for thoughts,[URL="http://www.gotw.ca/gotw/031.htm"]here[/URL]. | |
Re: Template specialization like so : [code] //for general types template<typename T> T ask(const std::string& question){ cout << typeid(T).name() << endl; cout << question << endl; T answer; cin >> answer; return answer; } //specialize for string type template<> std::string ask(const std::string& question){ cout << "In string\n"; cout << question << … | |
Re: This is a good idea, me thinks. I think I could use more experience developing 'large' projects and working with other people. So if I can, I would like to join/add people to start a open source. What do you think Nic? As for ideas, I'm not sure yet but … | |
Re: Also [URL="http://www.cplusplus.com/reference/clibrary/cfloat/"]see here[/URL] for exact details. | |
Re: Your problem is that those function do not belong in you abstract class. It seems like you are not abstracting enough, if you want only some function of the base class to be implemented. Whats wrong with simply letting the child develop the necessary functions? You might not necessarily need … | |
Re: >>[B]Conversely, you can not call destructors explicitly, except through Dynamic Memory Management[/B] Actually you can : [code] #include <iostream> using namespace std; struct Foo{ Foo(){cout<<"Ctor\n";} ~Foo(){cout<<"Dtor\n";} }; int main(){ Foo f; f.~Foo(); return 0; } [/code] But you see the problem, the destructor is called [b]2[/b] times. | |
![]() | Re: @OP: outfile closes when its destructor is called, and main returns a int implicitly. So all that means is you don't need(according to your program) the [i]outfile.close()[/i] and [i]return(0)][/i] statement. Also as mentioned already, you need to seed the rand(), to see rand, just add [i] srand(time(0))[/i] before [i]ofstream outfile...[/i]. … ![]() |
Re: @OP: your compare arguments has to be of type myStruct. Here is an example : [code] #include <iostream> #include <list> #include <string> using namespace std; struct Foo{ string name; int age; Foo(){} Foo(const string& n, int a) : name(n), age(a){} void print()const{ cout << "("<<name << "," << age << … | |
![]() | Re: >>Is there any way to only allow insertion into string streams If you want to only insert into streams, then use [i]ostringstream[/i]. Similarly, if you want only extraction into the stream, then use [i]istringstream[/i]. If you want both extraction and insertion into the stream then use [i] stringstream[/i]. But if … ![]() |
Re: Yea, here is a mediocre way : [code] #include <ctime> #include <iostream> using namespace std; void runFunctionHere(){} int main(){ long start = clock(); runFunctionHere(); long end = clock(); cout << "Time took in milli seconds : " << end - start << endl; } [/code] | |
Re: First this code will show you what type null is : [code] cout << "NULL type is " << typeid(NULL).name() << endl; cout << "NULL value is " << NULL << endl; [/code] This is the output you will probably get : [code] NULL type is int NULL value is … | |
Re: And the reason why your original code didn't work is because your virtual add function hid the 1 parameter function. Thats why you had to use the scope resolution operator. | |
Re: You can use some math magic. Or you can get the input as a string, which is much easier. For example here is some not tested code. It gets the input as a string. Then sums the digits inside the string. And displays it. [code] string digits; cin >> digits; … | |
Re: @OP: whenever you use const_cast, you have a serious design and logic problem in your code. The cases are very very rare, where it would justify to use const_cast in a program, and I assure you that you case is not one of them. What does this function do, [i]int … | |
Re: This is what you want : [code] vector<BaseClass*> stuffvector; BaseClass* temp = new DerivedClass(par_a, par_b); stuffvector.push_back(temp); stuffVector.pop_back(); //added, remove all accounts of 'temp' delete temp; [/code] But I lied, this is what you really wanted : [code] std::vector< boost::shared_ptr<BaseClass> > baseVec; baseVec.push_back( new DerivedFromBase() ); //do stuff baseVec.pop_back(); [/code] | |
Re: [QUOTE=Narue;1314985]How do you get the hello world program wrong? Any decent C++ programmer should be able to type it out flawlessly while plastered from booze, spun around in a chair for ten minutes, and blindfolded. :D[/QUOTE] ROFLED! @OP : [code] //Try at your own risk. #include <iostream> using namspace std; … | |
![]() | Re: That's nothing a quick google can't help you with. But since you asked here it is : [code] #include <iostream> #include <ctime> using namespace std; int main(){ srand(time(0)); for(int i = 0; i != 10; ++i){ cout << rand() << endl; } } [/code] |
Just a beta of what I was working on lately. It evaluates mathematical expressions like [i]2*3[/i]. Anyways, its explained in the program. Report of any bugs would be great because I haven't tested it out that much. Without further ado, you can download the program [URL="http://uploading.com/files/ef3f69c5/ExpressionEvaluatorBETA.zip/"]here[/URL], [URL="http://www.easy-share.com/1912008094/ExpressionEvaluatorBETA.zip"]here[/URL] or [URL="http://www.mediafire.com/?69oo679h46sgtq2"]here[/URL]. Try … | |
Re: Does it have to be in C++? Can you use external libraries? | |
Re: What you are doing is hard coding each stream insertion operator. This is what you want : [code] struct Animal{ int weight, age; string name; Animal(string name = "", int weight = 0, int age = 0) : name(name), weight(weight), age(age){} ~Animal()=0{} }; struct Mammal : Animal{ }; struct Dog … | |
Re: You don't need to use the ignore function, you can just do something like this : [code] const int SPACE = ' '; //peek the next character to see if its a space, if so then discard it if(cin.peek() == SPACE) cin.get(); //opt #1 char ch = 0; cin.get(ch); //Opt … | |
Just post random stuff here, anything you want. Say whatever you like. For example, anyone here have a facebook? | |
Re: Do you really need to use the "==" operator. Can you settle for something like this : [code] typedef pair<int,int> IntPair; bool hasEqualSecond(const IntPair& lhs, const IntPair& rhs){ return lhs.second == rhs.second; } //.... if(hasEqualSecond(p1,p2)){ /*do something */ } [/code] | |
![]() | Re: Something like this should suffice, although you might want something better : [code] #include <iostream> #include <sstream> #include <string> using namespace std; struct SimpleTime{ int hours; int minutes; int seconds; SimpleTime(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {} }; //does no … ![]() |
Re: What you are looking for is polymorphism. This example should answer your question : [code] struct Polygon{ int width,height; virtual int area()const = 0; }; struct Rect : Polygon{ int area()const{ return width * height; } }; struct Triangle : public Polygon{ int area()const{ return width*height*0.5f; } } //notice input … | |
Re: >>It occurred to me that this only generates numbers uniformly over the range In fact, it dosen't. The only way rand() generate uniform distribution is by using the value it returns as the random number and not manipulate that random number. Thus when you do something like this :[i] int … | |
Re: In your last for loop, use an if statement to check if the element in the array is higher or lower than the current highest and the current lowest. Here is a psuedo-code: [code] set minPtr to temps; set maxPtr to temps; for i = 1 to MAXTEMPS{ if temps[i] … | |
Re: [B]>>how to do the break?[/B] Whats that? Some kind of new dance? |
The End.