Thread: Looping Variables and outputting to .txt

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    Looping Variables and outputting to .txt

    Hey, I'm rather new at C++ (just started today, actually.) And as practice I'm trying to write a program to create a stat block for DnD and output it to notepad.

    This is my problem:

    I'm trying to create multiple variables using a loop, for example, prof1, prof2 and so on. This is what I have, and it doesn't work:
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    Code:
    int numclass;
    int numloop = 0;
    Code:
    
    cout<<"Number of classes: ";
    cin>> numclass;
    
    while ( numclass < x ) {
    
          char prof>>x[50];  This is what I tried... didn't work.
    
          cout<<"Class Name: ";
          cin.getline ( prof>>x, 50, '\n' );
    
          cout<<"Level: ";
          cin>> level;
          x++;
          }
    I also want to be able to reference to it again using a loop, how would I do that?

    Next problem: I want to be able to output the file as a .txt file, so I use this:

    Code:
    ofstream out (name".txt");
    Again, that didn't work for me. I want to be able to name it as whatever name the person gave it and then .txt.

    Any help would be greatly appreciated. Thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well....

    Code:
    char prof>>x[50];
    Making up random code isn't going to get you anywhere. Go back to the tutorials and figure out how to do what you're trying to do. I could guess as to your intentions, but that would just waste both of our time.

    Code:
    cin.getline ( prof>>x, 50, '\n' );
    Leave the >> operator alone for things like this. It works for cin, cout, and a number of other places, but not here (unless you're trying something very, very weird).

    As for getting a filename..... First just accept the full line with an extension. Make the user enter "bleh.txt". Then after the program works, then go back and change it to automatically add the .txt extension.

    Lastly, welcome to cboard.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Problem is, I couldn't find any tutorials on either << or >>. So I just guessed on what they did. I'll look over the tutorials one more time though.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    for saving to a file, i use this:
    Code:
           char filen[32];
           ofstream savef;
    	cout << "Please enter the filename: (no .txt): ";
    	cin >> filen;
    	strcat(filen, ".txt");
    	cout << endl << filen << " will be saved in the same directory as the program." << endl << endl;
    	savef.open(filen);
           if (savef.is_open()) {
                    // if it opened the file successfully, then put the write commands in here, ie.
                   savef << variablename; //note the << operator here, not the >>
                   //etc.
            }
            else {
                   cout << "Error." << endl; //if the file could not be opened
           }
    a brief explanation:
    char filen[32] initializes the variable, in this case a char array of 32. normally i would use a string (string varname) but ofstream only takes char arrays. then cin is used to get input from the user, by typing, cin >> [variablename]. strcat() is a function that appends something to something else. in this case we want it to append ".txt" to the filen variable (where the filename inputted is stored). then i just tell the user where the file will be saved, and try to open the file.
    Last edited by Salem; 07-22-2007 at 11:18 PM. Reason: Added code tags - learn to use them yourself

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code tags........ Code tags.......

    BTW, you run the risk of overrunning your buffer with that method. You should be using C++ strings, not C strings, but if you do use C strings, you need to be vigilant with regard to overrunning buffers.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Thanks for the help, and especially the explanation. Still can't find anything on including variables in other variable's names.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and why do you want to modify the variable names? use vector for storing multiple instanses of the same data...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Whoops, hadn't thought of that. Thanks.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Blurr View Post
    Thanks for the help, and especially the explanation. Still can't find anything on including variables in other variable's names.
    Not that this is useful, but for the record it is possible if the variables are #defined constants:

    Code:
    #define var 3 
    #define constantNum(a) constNum##a
    #include <ostream>
    int main()
    {
        int constantNum(1), constantNum(2), constantNum(3);
        cout << constantNum(var);
    }
    Most of the time an array, vector, or map would be more appropriate though.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed