in a reply to eibro, this following codeline will compile in vc++
(not that my variable name is diff because its from another program)Code:std::transform(userInput.begin(), userInput.end(), userInput.begin(), tolower);
in a reply to eibro, this following codeline will compile in vc++
(not that my variable name is diff because its from another program)Code:std::transform(userInput.begin(), userInput.end(), userInput.begin(), tolower);
Thanks again everyone. I'll put these up soon.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Some more for the todo list, possibly. :D
Code:/* A simple class derived from the time.h 'struct tm'. I used a similar class to write a time-clock program. A class like this is very useful for compactly storing/retrieving time info from files, BTW. For instance, 1000 dates can be stored in a binary file of only 4K (assuming 4-byte time_t storage). TODO: Only localtime() support was added. Expand as necessary. */ class Time : tm { public: Time(struct tm& time); Time(time_t time); Time(); void second(int set); void minute(int set); void military_hour(int set); // 0-23 void hour(int set, bool pm = false); void day(int set); void yearday(int set); // 0-364 void weekday(int set); // 0-6 void month(int set); void year(int set); int second() const; int minute() const; int military_hour() const; int hour() const; int day() const; int yearday() const; int weekday() const; int month() const; int year() const; time_t time(); operator time_t () // implicit conversion, very useful. { return time(); } time_t now(); // set to the current time. time_t set(time_t time); time_t set(struct tm& time); double difference(time_t other); time_t operator = (struct tm& time); time_t operator = (time_t time); bool operator == (time_t other); bool operator > (time_t other); bool operator < (time_t other); const char * printable(); // beware: returns a static buffer! }; /* The Implementation: */ Time::Time(struct tm& time) { set(time); } Time::Time(time_t time) { set(time); } Time::Time() { now(); } void Time::second(int set) { tm_sec = set; } int Time::second() const { return tm_sec; } void Time::minute(int set) { tm_min = set; } int Time::minute() const { return tm_min; } void Time::military_hour(int set) { tm_hour = set; } int Time::military_hour() const { return tm_hour; } void Time::hour(int set, bool pm = false) { if(set < 12 && pm == true) { set += 12; } else if((set == 24) || (set == 12 && pm == false)) { set = 0; } military_hour(set); } int Time::hour() const { int hr = military_hour(); if(hr == 0) hr = 12; else if(hr > 12) hr -= 12; return hr; } void Time::day(int set) { tm_mday = set; } int Time::day() const { return tm_mday; } void Time::yearday(int set) { tm_yday = set; } int Time::yearday() const { return tm_yday; } void Time::weekday(int set) { tm_wday = set; } int Time::weekday() const { return tm_wday; } void Time::month(int set) { tm_mon = set-1; } int Time::month() const { return tm_mon+1; } void Time::year(int set) { tm_year = set-1900; } int Time::year() const { return tm_year+1900; } time_t Time::time() { return std::mktime(this); } time_t Time::now() { return set(std::time(NULL)); } time_t Time::set(time_t time) { struct tm * record = std::localtime(&time); if(record) *this = *record; else return 0; return this->time(); } time_t Time::set(struct tm& time) { memcpy(this, &time, sizeof(*this)); return this->time(); } double Time::difference(time_t other) { return std::difftime(*this, other); } time_t Time::operator = (struct tm& time) { return set(time); } time_t Time::operator = (time_t time) { return set(time); } bool Time::operator == (time_t other) { return (difference(other)==0); } bool Time::operator > (time_t other) { return (difference(other) > 0); } bool Time::operator < (time_t other) { return (difference(other) < 0); } const char * Time::printable() { return std::asctime(this); } /* An example using the Time class. */ int main() { const char day[7][15] = { {"Sunday"}, {"Monday"}, {"Tuesday"}, {"Wednesday"}, {"Thursday"}, {"Friday"}, {"Saturday"}, }; Time now; cout << day[now.weekday()] << ", " << now.month() << "/" << now.day() << "/" << now.year() << endl; cout << now.hour() << ":" << now.minute() << ":" << now.second() << endl; cin.get(); return 0; } /* An example of timing a program using the Time class. */ int main() { int i = 0; Time start; while(i++ <= 1000000000) continue; Time stop; Time performance = stop-start; say("One billion loops took" " %i minutes and %i seconds.", performance.minute(), performance.second()); return 0; }
Last edited by Sebastiani; 02-23-2003 at 09:44 PM.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Seb, you care to put any comments around that code or at least a brief description as to what it does and how?
Also, I tried compiling your directory walker, but couldn't. Now, this is obviously highly compiler specific code, so I have 2 questions for you: What compiler does the code compile with, and is that code directly from your editor (as I can't compile it, I can't verify the code myself).
Thanks, btw
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
This will compile on VC++, Bordland, and Dev-C++:
Code:#include <dirent.h> #include <iostream> static char CURRENT_DIRECTORY[1024]; void Search(const char * folder, const char * substring) { DIR * directory = opendir(folder); if(directory == NULL) return; chdir(folder); _finddata_t * file = &directory->dd_dta; long index = _findfirst("*", file); do { if(strstr(file->name, substring)) { if(file->attrib & _A_SUBDIR) cout << " Folder:" << endl; else cout << " File:" << endl; if(file->attrib & _A_ARCH) cout << "(Archive)" << endl; if(file->attrib & _A_SYSTEM) cout << "(System)" << endl; if(file->attrib & _A_HIDDEN) cout << "(Hidden)" << endl; if(file->attrib & _A_RDONLY) cout << "(Read Only)" << endl; getcwd(CURRENT_DIRECTORY, 1024); cout << CURRENT_DIRECTORY << "\\" << file->name << endl; } if(file->attrib & _A_SUBDIR && strcmp(file->name, ".") != 0 && strcmp(file->name, "..") != 0) { Search(file->name, substring); chdir(".."); } } while(_findnext(index, file) == 0); _findclose(index); closedir(directory); return; }
I will add comments to the Time class later tonight.
- cheers.
Last edited by Sebastiani; 02-24-2003 at 08:18 AM.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Yep. Most of my code knowledge is pretty simple, so examples with as little extraneous stuff as possible tend to help. Thanks Hammer for the help last time I asked about this stuff, even though my compiler didn't quite like it.@deathstryke: Are you meaning this entry? I'll write something easier to understand if so.
One death is a tragedy, one million... a statistic.
-Josef Stalin
In case I forget, I use Bloodshed Dev C++ v.4
An Intro to Pointers
Pointers are a great struggling point among peopple trying to the C/C++. There is an aura surrounding them implying that they are only something that true Gods can understand (which is obviously not true). When one uses an int or char or whatever, the value stored at the address of it is the actual value of the variable. However, pointers are different. Instead of holding a value, pointers hold a memory address. This means that if you do not dereference it, you will get the address that it points to. However, using the dereference operator (the little star thing that you always see… it looks like this: *), one can access the value that is in the address that is being pointed to by the pointer (try saying that 3 times fast). This means that the type of the pointer must be the same as the type of data you plan to access (unless you are using void pointers, where you can cast (Don’t worry about this now)). To get the actual address of the pointer, one must use the & (address of) operator. Here is some sample code to demonstrate this: (note, this code uses C++ output mechanisms and can be easily converted to C)
Code:#include <iostream> using namespace std; int main(void) { int *foo; //declares a pointer to an int foo=new int; //means that foo points to an int on the heap or free store (same thing, 2 different names) *foo=4; //the address that foo is pointing too will now hold a value of 4 // Now here is where it all comes together cout<<”The address of the pointer is “<<&foo<<”.”<<endl <<”The address the pointer is pointing to is “<<foo<<”.”<<endl <<”The value that is held in the address that the pointer is pointing to is “<<*foo<<”.”<<endl; cin.get();//pause the program delete foo; return 0; }
Another big feature of pointers is that you can make them point to other variables. In doing this, you can indirectly access a different variable. This allows you to manipulate the variable through the pointer. Take this example:
Code:#include <iostream> using namespace std; int main(void) { int* foo;//again, we have a pointer to an int int number;//just a regular int foo= &number;//foo now points to number's address //now, any change to foo will also change number number=5; cout<<"The address that number is at is "<<&number<<endl; cout<<"This address being pointed to by foo "<< foo<<endl; cout<<"number= "<<number<<endl; cout<<"(accessed through foo) number= "<< *foo<<endl<<endl; number=2; cout<<"number= "<<number<<endl; cout<<"*foo= "<<*foo<<endl<<endl; *foo=10; cout<<"Number= "<<number<<endl; cout<<"*foo= "<<*foo<<endl; cin.get();//pause the program return 0; }
Last edited by golfinguy4; 02-27-2003 at 08:15 PM.
Hammer, just to give you a heads up...
I was checking out the FAQ and realized with the new site design, some of it has become "clipped." It happens on multiple pages.
Same with the tutorials, you have to load the printable version to be able to read them.Originally posted by golfinguy4
Hammer, just to give you a heads up...
I was checking out the FAQ and realized with the new site design, some of it has become "clipped." It happens on multiple pages.
thanks for the info.
can you give me a link to a broken page, I checked a few and they looked OK to me.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Broken here: http://faq.cprogramming.com/cgi-bin/...&id=1031248513
Broken: http://faq.cprogramming.com/cgi-bin/...&id=1031248513
I didn't go through all of them. But those are a couple of examples. Also, a bunch of others don't look to great as the text goes (and almost touches the border). This constrasts the nice margin on the other side.
I checked the first 20 FAQ entries (WinXP/Mozilla) and they looked fine.
The tutorials also look fine, except that the yellow code boxes are getting clipped.
I could read all the content of both correctly.
Away.
Yeah, I see now, they don't look too snappy in IE, but are OK in Mozilla.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I've fixed the FAQ pages as far as I can tell. Let me know if there's any more problems with it.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]