Jump to content

Trying to print a variable in quatation marks...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Hey, so im trying to make this so it display the variable but i dont know how i would do that because of the quatation marks? dont know if that made any sense but for example:



#include <iostream>

#include <string.h>

#include <stdio.h>

#include <windows.h>

#include <fstream>


using namespace std;


int main(int argc,char *argv) 

 {

      ofstream myfile;       

      std::string name_file = "cooldude";

      int name_file_num = 1;

      std::string name_file_ext = ".txt";   

         for (int i = 0;i < 10;i++)

          {

             myfile.open(name_file + name_file_num + name_file_ext);       

          }

      myfile << "lol, it would be cool";            

      myfile.close();

      system("pause");                           

 }


Now you see here is that the code i showed wont compile for the following reason:

myfile.open(name_file + name_file_num + name_file_ext); 


But this will give you a idea of what i want it to do, so basicly it would be: cooldude1.txt
Posted Image

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  for (int i = 0;i < 10;i++)
  {
    std::cout<<(name_file + char(name_file_num+'0') + name_file_ext)<<"\n";       
  }

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Um... im not trying to cout this :S
Posted Image

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I realize, but the logic is the same.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Yeah, i know what you mean, but i got an error when i tried that :(
Posted Image

#6
FrozenSnake

FrozenSnake

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>
#include <fstream>

using namespace std;

int main(int argc,char *argv)
{
	ofstream myfile;
	std::string name_file = "cooldude";
	std::string name_file_ext = ".txt";
	for (int i = 1;i <= 10; ++i)
	{
		std::string tmp = std::string(name_file + char(i) + name_file_ext);
		myfile.open(tmp.c_str());
	}
	myfile << "lol, it would be cool";
	myfile.close();
	std::cin.get();
}

Class std:: ofstream does not take an std::string as argument, it takes a const char*
But char makes it ascii so It will only create cooldude1.txt I am to tired to think of a fix.
But hope it helps!

I did this but as I said I am to tierd (been up for 32h) so this doesn't work as intended but it MIGHT help you in the right direction

#include <iostream>
#include <string>
#include <sstream> // for stringstream (variable 'ss')
#include <stdio.h>
#include <windows.h>
#include <fstream>

using namespace std;

int main(int argc,char *argv)
{
	ofstream myfile;
	std::string name_file = "cooldude";
	std::string name_file_ext = ".txt";
	std::stringstream ss;
	for (int i = 1;i <= 10; ++i)
	{
		ss << name_file << i << name_file_ext;
		std::string tmp(ss.str());
		std::cout << "---------------------------\n" << "tmp = " << tmp << "\n---------------------------\n" << "\n";
		myfile.open(tmp.c_str());
	}
	myfile << "lol, it would be cool";
	myfile.close();
	std::cin.get();
}

Hope I did what you needed help with ;P even if it need a bit of poking before it works as intended!

Edited by FrozenSnake, 11 April 2009 - 10:15 PM.


#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Perhaps because you were creating the same file 10 times?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
FrozenSnake

FrozenSnake

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts

WingedPanther said:

Perhaps because you were creating the same file 10 times?
:P Like I said I was very tired when I wrote the code, but I think he can figure it out when he takes a look at the code :)