Thread: c++ string creation and file manipulation

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    c++ string creation and file manipulation

    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?

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    thanks for the fast reply, but im sort of a noob to cpp, could you explain that a little

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    The file stream version

    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;
    
    }
    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.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    when i went to compile it using ms vc++ 6, it gave me 9 errors and 1 warning...

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    --------------------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)

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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

  12. #12
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Looking at the errors, are you not missing "using namespace std"?

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    ok, this is what i have so far:


    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;
    
    }
    and it gives me these error messages:
    --------------------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)

  14. #14
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Do I have to say it again. Add this line to get rid of all those errors:
    using namespace std;

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  3. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  4. File Creation with fixed PREFIX & SUFFIX !!!
    By mehuldoshi in forum C Programming
    Replies: 2
    Last Post: 07-29-2002, 06:02 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM