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

Pages: 1-4041-

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:24

1) Paste unmodified code.

2) You don't want to; use an array.

Name: Anonymous 2006-04-11 18:28

>>1
I hope you're joking or your compiler is seriously fucked up. And use: string file = "lol.txt";

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. 

Name: Anonymous 2006-04-11 18:41

>>3
NVM, I just realized a better way using arrays.  Don't answer point one of >>4.

Name: Anonymous 2006-04-11 19:38 (sage)

>>4
That should work, otherwise refer to >>3.

Name: Anonymous 2006-04-11 20:43

>>6
I'm using dev C++...

Name: Anonymous 2006-04-12 5:58

>>1
That can't happen, check your code and delete the files it supposedly created before running the program again.

Name: Anonymous 2006-04-12 13:11

>>8
I'll try the compilers at school...

Name: Anonymous 2006-04-12 16:55

God fucking damn...  Won't fucking compile in microsoft visual studio.  9 fucking errors.  How the fuck can this thing go this wrong?

Are the names of the includes different for the MS compiler?

Name: Anonymous 2006-04-12 18:42

>>10
what errors?

Name: Anonymous 2006-04-12 19:13

>>1
Maybe im nitpicking, but i noticed you wrote "lol.txt" in your example, and "lo.txt" in your actual code. Is this a typo or is it supposed to be like this?

Name: Anonymous 2006-04-12 19:30

>>4
should not
    communism.open (file_opened);

Name: Anonymous 2006-04-12 20:20

>>1
You forgot to include <string> and <fstream>

Name: Anonymous 2006-04-14 1:08

>>14
LOL U FINK?

>>13
Thank you... that worked.

Just wondering, what the hell does that c_str() mean?  Does it tell the program to expect a string or what?  Why didn't file_opened work?

Name: Anonymous 2006-04-14 1:24

>>15
.c_str() just returns the C-style null-terminated string associated with the std::string object.  useful for any functions or methods that except a C-style string and don't know how to deal with a C++ std::string.

Name: Anonymous 2006-04-14 2:32

>>16
did you mean to write "Accept" where you wrote except?  Or do you mean "except" as in exception?

Name: Anonymous 2006-04-14 9:34

>>17
I guess he meant "accept" as arguments to these functions or methods.

Name: Anonymous 2006-04-14 12:04 (sage)

s/except/expect

Name: Anonymous 2006-05-07 3:08

>>16
Sorry to bump this, but is that .c_str thing an example of a method?  What the fuck is a method?

Name: Anonymous 2006-05-07 3:25

a method is a fancy way of saying a function or procedure that's in an object.  and yes, someString.c_str() is an example of a method (c_str) in an object (someString).

Name: Anonymous 2006-06-03 3:09

>>13
Something I've allways wondered...
Why doesn't the string class define a conversion class to char *?
string {
  operator char* () {
    return c_str();
  }
}

I mean... it'd sure be useful.

Name: Anonymous 2006-06-03 10:28

>>23 I guess so but then there is the problem of implicit conversions everywhere. Using a method to convert to char * might make more sense because it makes the conversion obvious.

I've always been fucked up about conversions though:
string meme = "NO U";
char *str = meme.c_str();
This looks wrong ya? But it's not! We're accessing the meme through the pointer.

Name: Anonymous 2006-06-05 3:54

Duck typing motherfucker, do you know it?

Name: Anonymous 2006-06-05 4:25

>>25
The word "dynamic" has neither a 'u' nor a 'k', you fail at spelling.

Name: Anonymous 2006-06-05 5:36

>>25
Is this duck?

Name: Anonymous 2006-06-05 8:10

>>27
it is if it walks like one my good friend!

Name: Anonymous 2006-06-05 8:53

>>28
It must also quack like one.

Name: Anonymous 2006-06-15 20:35 (sage)

sage for great spam justice

Name: DoubleAW 2006-06-22 19:04 (sage)

>>22
>>30
>>32
>>33

What the hell?

Name: Anonymous 2006-06-22 20:27

Fuck this spam shit, I'm getting tired of it

Name: Anonymous 2006-06-23 23:29

>>21
And in that case, would string be the class of that object?

And what are std:: strings?  I heard they were a structure of arrays or something, so that they could support unicode, but I'm not sure.

Name: Anonymous 2006-06-24 3:26

>>36
And in that case, would string be the class of that object?
yes.

And what are std:: strings?
in the standard C++ library, the string class is located in the std namespace.  std::string just means 'string that is in the std namespace'.

Name: Anonymous 2006-06-24 8:02

>>36
Nope. The C++ standard string class is basically a checked array of ASCII characters. If you want a Unicode string, use something like Qt's QString or glibmm's ustring (glibmm is the collection of C++ bindings for GNU glib).

>>37
To be really pedantic, std::string is actually just a typedef for std::basic_string<char>.

Name: Anonymous 2006-06-25 2:57

>>3
string file = "lol.txt";
This creates a temporary string then assigns it to file.

string file("lol.txt");
This initializes file directly with the char*.

Both probably optimize to the same assembly, but I would consider using the constructor more correct than using the assignment.

Name: Anonymous 2006-06-25 14:35

>>24

I left my Stroustrup book at work, but I'll see how well I remember from memory.

The method .c_str() returns a C-style string based on the contents of the string object that calls .c_str().  A C-style string is a null-terminated character array (example: "Hello" --> 'H' 'e' 'l' 'l' 'o' '\0'.).  In C (and in C++), a pointer can be used to point to an array.  For example:


//C-style code to output all chars.  Not the way I'd code myself
void outputAllChars(char *outputStr)
{
   while(outputStr)  //Loop until current Char == '/0' or NULL
          //Is it %c for a char?  We could always just use cout anyways
      printf("%c\n", outputStr++);  //Output AND go to next char
}


void outputStr(char outputStr[])
{
   int i = 0;
   while(outputStr[i] != '/0')
   {
      cout << outputStr[i] << endl;
      i++;
   }
}

Anyways, the point is, a pointer to an array or an array (i.e. []) are the same thing.  This is less important to worry about in C++ than in C.

Name: Anonymous 2010-09-21 17:11

Guys, please use code tags. Shit.

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