Help with making a string in C++

December 7, 2010 at 20:00:28
Specs: Windows 7, intel Quad core@2GHz/6GB
ok so i'm writing a program and i'm having trouble figuring out how to add an int that already exists into a string...
const int MAX=50;
char string[MAX]= "The number "number " is present in the array";
ofstream outfile("c:/File1.txt", ios::out);//opens File1.txt and gets it ready to save things

if (!outfile)//incase File1.txt wont open
	cerr<<"File Could not be opened\n";
					outfile<<string;//saves string into File1.txt

the problem is right at the begining, number is an int and i've already declared it earlier on in the string, but i need it to be part of the string

i'm using microsoft visual studio 2008 espress

See More: Help with making a string in C++

Report •

December 7, 2010 at 21:16:37
"string" is a part of the string class, so it's probably a good idea to rename this variable to something else.

#include <cstdio>

char s[40];
int number = 5;
sprintf(s, "the number %d is present in the array", number);

Report •

December 9, 2010 at 12:45:27
The C++ way would be to use a stringstream.

How To Ask Questions The Smart Way

Report •
Related Solutions

Ask Question