how would i go about creating a c++ program that generates strings that range from 1 character to 8 characters (all asci) in such a way that all possible combinations are recorded in a simple txt file?
how would i go about creating a c++ program that generates strings that range from 1 character to 8 characters (all asci) in such a way that all possible combinations are recorded in a simple txt file?
Hold up, I'm programming something that might answer your question.
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict
Function pointers are so beautiful. This is the console version of what you're talking about.
Code:void PrintRandomWord( char(*Print)(int), int, const int ); char PrintLetter( int ); void PrintRandomWord( char(*Print)(int rand_max), int max_random, const int MAX ) { srand( (int)time ); for(int i=0; i<=MAX; i++) { cout << Print( max_random ); } } char PrintLetter( int rand_max ) { int random= rand() % rand_max; switch( random ) { case 1: return 'A'; break; case 2: return 'B'; break; case 3: return 'C'; break; case 4: return 'D'; break; case 5: return 'E'; break; case 6: return 'F'; break; default: return 'S'; break; } } int main( int argc, char *argv[] ) { PrintRandomWord( PrintLetter, 6, 5 ); cin.get(); return 0; }
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict
thanks for the fast reply, but im sort of a noob to cpp, could you explain that a little
The file stream version
Is this what you're talking about? I just now coded those two in a desperate attempt to try and help someone for once in my life.Code:#include <fstream> // important #include <ctime> // important void PrintRandomWord( char(*Print)(int), int, const int); char PrintLetter( int ); void PrintRandomWord( char(*Print)(int rand_max), int max_random, const int MAX ) { srand( (int)time ); ofstream fout; fout.open("RandomLetters.txt", ios::app); for(int i=0; i<=MAX; i++) { fout << Print( max_random ); } fout.close(); } char PrintLetter( int rand_max ) { int random= rand() % rand_max; switch( random ) { case 1: return 'A'; break; case 2: return 'B'; break; case 3: return 'C'; break; case 4: return 'D'; break; case 5: return 'E'; break; case 6: return 'F'; break; default: return 'S'; break; } } int main( int argc, char *argv[] ) { PrintRandomWord( PrintLetter, 6, 5 ); cin.get(); return 0; }
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict
A newbie to C++?
If you're a REAL newbie(like just started a few days ago) then I strongly suggest that you don't feel/worry about file streaming for now. But if you're somewhat intermediate and know some stuff then go ahead and ask me a question about my source code.
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict
Oh yeah, and try to compile that(I already tested it but it would be better if you compiled it to see what it does).
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict
when i went to compile it using ms vc++ 6, it gave me 9 errors and 1 warning...
--------------------Configuration: generator - Win32 Debug--------------------
Compiling...
generator.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(13) : error C2065: 'ofstream' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(13) : error C2146: syntax error : missing ';' before identifier 'fout'
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(13) : error C2065: 'fout' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(15) : error C2228: left of '.open' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(15) : error C2653: 'ios' : is not a class or namespace name
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(15) : error C2065: 'app' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(20) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(23) : error C2228: left of '.close' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(76) : error C2065: 'cin' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(76) : error C2228: left of '.get' must have class/struct/union type
Error executing cl.exe.
generator.exe - 9 error(s), 1 warning(s)
Be sure to include iostream, fstream, ctime, cstdlib
like so:
Code:#include <iostream> // or #include <iostream.h> #include <fstream> // or #include <fstream.h> #include <cstdlib> #include <ctime>
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict
fstream.h has all of the file streaming functions and stuff so you need to include it if you're going to open, write to, or read from a .txt file
Including ctime is fine for my compiler. Your compiler is different so you might have to use time.h instead
C:\Program Files\Microsoft Visual Studio\MyProjects\Generator\generator.cpp(13) : error C2065: 'ofstream' : undeclared identifier
That error isn't my fault. ofstream is un-identified because you didn't include fstream. "ofstream" is a output file stream class in fstream.h
Without including that file, the compiler won't know what that is.
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict
Looking at the errors, are you not missing "using namespace std"?
ok, this is what i have so far:
and it gives me these error messages:Code:#include <iostream> // or #include <iostream.h> #include <fstream> // or #include <fstream.h> #include <cstdlib> #include <time.h> void PrintRandomWord( char(*Print)(int), int, const int); char PrintLetter( int ); void PrintRandomWord( char(*Print)(int rand_max), int max_random, const int MAX ) { srand( (int)time ); ofstream fout; fout.open("RandomLetters.txt", ios::app); for(int i=0; i<=MAX; i++) { fout << Print( max_random ); } fout.close(); } char PrintLetter( int rand_max ) { int random= rand() % rand_max; switch( random ) { case 1: return 'A'; break; case 2: return 'B'; break; case 3: return 'C'; break; case 4: return 'D'; break; case 5: return 'E'; break; case 6: return 'F'; break; default: return 'S'; break; } } int main( int argc, char *argv[] ) { PrintRandomWord( PrintLetter, 6, 5 ); cin.get(); return 0; }
--------------------Configuration: generator - Win32 Debug--------------------
Compiling...
generator.cpp
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(14) : error C2065: 'ofstream' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(14) : error C2146: syntax error : missing ';' before identifier 'fout'
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(14) : error C2065: 'fout' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(16) : error C2228: left of '.open' must have class/struct/union type
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(16) : error C2653: 'ios' : is not a class or namespace name
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(16) : error C2065: 'app' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(21) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(24) : error C2228: left of '.close' must have class/struct/union type
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(77) : error C2065: 'cin' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\generator\generator.cpp(77) : error C2228: left of '.get' must have class/struct/union type
Error executing cl.exe.
generator.exe - 9 error(s), 1 warning(s)
Do I have to say it again. Add this line to get rid of all those errors:
using namespace std;
Add "using namespace std; " after all of your includes. I don't think it will get rid of the 'ofstream' error, but it might get rid of some/most/all of your errors, maybe. Try it.
Name: Eric Lesnar
Learning: C/C++, SDL, WinAPI, OpenGL, and Python
Compiler: Dev-C++ 4.9.0
Current Game Project: Acoznict