-
The new FAQ
I've been working on a new format FAQ, which you can view here.
I've used most of the content from Lightatdawn's FAQ, taken some stuff from the FAQ board, and written some of my own. I'll add more in due course. If there's anything specific you'd like to see in there, let me know. If you want to write anything yourself, feel free to do so, ask me for details on how to submit it.
All comments/suggestions/corrections are welcome, either reply to this thread or PM me.
Enjoy. :)
-
Nice job, hopefully it sees more use than the old one. I don't like the logo though :P
edit: i think the vc++ series should be in the compiler/ide section, i was suprised that it is not.
-
geez Hammer... you're all involved in the community and making it better now... what gives? ;)
seriously... looks good. Keep up the superb work, my friend.
-
This got bumped down b4 it got seen, maybe sticky it? well....bumpety...
-
>>hopefully it sees more use than the old one
I can but hope that newbies read it :D
>>I don't like the logo though
Yah well... it ain't that bad. I couldn't do too much in a 100x100 image. Still, this is about the FAQ itself, not the logo :)
>>involved in the community, what gives
Well, yonks ago I said I'd do this, so I thought I'd better keep my promise. :cool:
[edit]Stickied for a day or two
-
Looks good...For beginners that is, I now have to click 2 links to
get to my answer!
Yeah, I know, I'm a lazy bump:p
-
Woohoo, my submission is up! Looks like we'll have to stop our late-night submission sodomizations, l@d :eek:
-
Do you want me to do a C++ Style converting int to string?
BTW, it looks nice.
Code:
#include <sstream> //this header automatically includes iostream and is needed for ostringstream
#include <string>
using namespace std;
string IntToString(int num); //this will be our converter function
int main(void)
{ int number=5;
string result=IntToString(number);
cout<<result<<endl;
cin.get();
return 0;
}
string IntToString(int num)
{ ostringstream myStream; //creates an ostringstream object
myStream<<num<<flush;
/*outputs the number into the string stream and then flushes
the buffer (makes sure the output is put into the stream)*/
return myStream.str(); //returns the string form of the stringstream object
}
-
>>Do you want me to do a C++ Style converting int to string?
Sure :) Help is welcome.
If anyone wants to submit anything for this, here's a few guidelines:
The code must be:
- valid, ie it compiles and does what its supposed to
- standard. Don't use things like getch() when there is no need
- not over complicated for target audience.
- documented. Try to put some decription text with your code. This is an FAQ afterall, not a snippets library.
As for the formatting, just submit it to me in text, I'll sort out the HTML to go round it.
[edit]
Thanks golfinguy4, I see you've posted some code. I'll sort that out later. I'm going to be off in a minute :o
-
Use this if you'd like...
If you want, i'll write some more tommorow.
Why does my program enter an infinite loop if the user inputs invalid data?
Suppose you prompt the user to enter an integer value, and they enter something like "foo" or 'q'. Your istream-derived object (usually cin) attempts to extract an integral value from the input buffer, fails, and all subsequent input attempts fail.
To remedy this, you must check your object for a fail state after extraction, flush the input buffer, and attempt another extraction.
Code:
using std::cout;
using std::cin;
// ...
int input = 0;
while (cout << "Enter a number: " && !(cin >> input))
{
cout << "\nInvalid input\n";
cin.clear();
cin.ignore(std::numeric_limits<int>::max());
}
Output
Enter a number: Hello
Invalid input
Enter a number: d
Invalid input
Enter a number: 2
Note: If you're unfamiliar with the funky std::numeric_limits<int>::max() statement, it's equivalent to the INT_MAX macro in <climits> or <limits.h>. Basically, it descripts the largest value an integer can hold. Include <limits> to use the std::numeric_limits template.
std::endl vs. '\n'
It's simple, std::endl sends a '\n' and flushes the output buffer. '\n' does not flush the output buffer.
How can I get input without having the user hit <enter>?
Using standard C/C++, you cannot. However, if you're lucky your compiler may have the non-standard conio.h header (which includes getch()), if you're on *nix (UNIX, Linux, etc.) you can try the ncurses library, else try the different API input routines (such as those included in the Win32 API)
-
Thanks Eibro. I'll have a proper look at this tonight and put it up.
-
I'm having trouble figuring out the section on unbuffered input. May be I just need to learn a little more before I try, but it seems so much more useful than cin.
It might prove more effective to simplify the code (maybe even as far as something like saying to press enter and then spitting out whether they did or not, in this case). Complexity leads to confusion.
-
@deathstryke: Are you meaning this entry? I'll write something easier to understand if so. :)
-
I'm going to write some more once I get some time.
A fix to "Why does my program enter an infinite loop..."; below the code where I mention the std::numeric_limits<int> template, '<int>' is being treated as an HTML tag and it's not showing up.
Edit:
How can I convert a char/string to upper or lower case?
For single characters converting to upper/lower case is simple, use the functions toupper() and tolower() included in ctype.h or cctype.
For example:
Code:
#include <cctype>
#include <iostream>
int main(int argc, char** argv)
{
using std::cout;
using std::toupper;
using std::tolower;
char letter = 'a';
cout << "Before toupper: " << letter << endl;
letter = toupper(letter);
cout << "After toupper: " << letter << endl;
letter = tolower(letter);
cout << "After tolower: " << letter << endl;
return 0;
}
/*
Output:
Before toupper: a
After toupper: A
After tolower: a
*/
For strings, it's a little more compilcated.
First, it depends on what type of 'string' you're using. If it's traditional c-style strings (character arrays) then you'll need to iterate through the array and call tolower or toupper for each element.
Code:
#include <cctype>
#include <iostream>
int main(int argc, char** argv)
{
using std::cout;
using std::endl;
char hello[] = "Hello World";
cout << "Before conversion: " << hello << endl;
for (char* iter = hello; *iter != '\0'; ++iter)
{
*iter = std::tolower(*iter);
++iter;
}
cout << "After conversion: " << hello << endl;
return 0;
}
/*
Output:
Before conversion: Hello World
After conversion: hello world
*/
For C++-style strings of the std::string type, things are essentially the same. You need to iterate through each character and call tolower or toupper. Luckily, the STL includes an algorithm which does most of the work for us.
Code:
#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
int main(int argc, char** argv)
{
using std::string;
using std::cout;
using std::endl;
string hello = "Hello World";
cout << "Before conversion: " << hello << endl;
std::transform(hello.begin(), hello.end(), hello.begin(), std::tolower);
return 0;
}
/*
Output:
Same as above example
*/
Edit again: I'm unsure whether tolower and toupper are in namespace std. VC6 doesn't seem to think so, but that compiler is far from standards compliant.
-
For the todo list: :)
A sample directory walker:
Code:
/*
Note: your compiler may require preceding underscores for some of the functions/constants/data structures used here.
ie: _findfirst or _A_SUBDIR, etc...
*/
#include <dirent.h>
#include <string.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;
}
int main()
{
Search("c:\\", "config.sys");
cout << endl << "Finished Searching.";
cin.get();
}
A simple busy wait function:
Code:
#include <time.h>
void wait(unsigned milliseconds)
{
unsigned timeout = clock() + milliseconds;
while( clock() < timeout ) continue;
}
Two handy functions for newbies:
Code:
void say(const char * str, ...)
{
char quote[strlen(str)+1024];
va_list v;
va_start(v, str);
vsprintf(quote, str, v);
int length = strlen(quote);
if(length && length < 80)
{
if(length%2)
++length;
for(int i = 0; i < 40-length/2; ++i)
cout << " ";
cout << quote << endl;
}
cin.get();
}
void affirm(bool condition)
{
if(condition)
say("Yes.");
else
say("No.");
}
int main()
{
int x = 4096;
affirm(x > 4095);
}