Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

newb question....

Name: Anonymous 2006-04-11 18:08

Check this c++ code:

    string file_opened ("lol.txt");
    ofstream communism;
    communism.open (file_opened);

The thing keeps writing to a file called file_opened rather than lol.txt.  How do I get the thing to treat the variable as the string that makes it up?  Tutorials say nothing about this...  They always just show you how to write strings directly to output.

Also, while I'm at it, (I suspect this is related) if I wanted to generate the names of variables using code, how would I do it?  Say I wanted to initialize ten variables, name1 - name10, but I didn't feel like typing out every one.  Here's what I would write intuitively: 

while (i < 9)  {
    i+=1;
    int namei;
};

But I know this isn't right.

Name: Anonymous 2006-04-11 18:40

>>3
That was just a hypothetical for what I really want to do.  If I did want to construct the names of variables, how would I do that?

>>2

// basic file operations
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int rebar (int a) {
    if (a == 11 or a == 12 or a == 13) {return 4;} else {
          int m;
          if (a > 10 && a < 20) {
                m = 10;
          } else if (a > 20 && a < 30) {
                 m = 20;
          } else if (a > 30 && a < 40) {
                 m = 30;
          } else if (a > 40 && a < 50) {
                 m = 40;
          } else {
                 m = 0;
          };
          return (a - m);
    }
}
   
int main () {
    int i (0);
    string file_opened = "lo.txt";
    ofstream communism;
    communism.open (file_opened);
    if (communism.is_open()) {
       while (i < 100) {
       string ordin ("st");
       switch (rebar(i+1)) {
              case 1:
                    ordin = "st";
              break;                    
              case 2:
                    ordin = "nd";
              break;                    
              case 3:
                    ordin = "rd";
              break;
              default:
                    ordin = "th";
              break;
       }
      
       communism << "Im'a write this ten times.  this is the " << i+1 << ordin << " time.\n";
       cout << "I done a good thing daddy.  I done wrote up in this here file " << i+1 << file_opened << " times\n";
       i += 1 ;
       };
    };
    communism.close();   
    return 0;
}


I told you I'm a newb.  I'm not trying to make mission critical applications in which everything has to be done perfectly and look beautiful. I'm just trying to learn how to do this shit. 

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List