printing a string to a file

// these are the errors that I'm getting from an online c++ compiler
// main.cpp:4:10: error: #include expects "FILENAME" or
// main.cpp: In function 'void permute(char*, int)':
// main.cpp:17:9: error: 'f' was not declared in this scope
// I don't understand how to print full_string to a file!
// otherwise, I know that it gives the correct output -- 90 strings.
// thanks for any/all help in advance. Bill

#include <string>
#include <iostream>
#include <fstream>
#include >ios> // line 4 error

using namespace std;

char full_string[] = "112233";
int counter = 0;
void iter_swap(char *ptr1, char *ptr2);
char *min_element(char *start, char *end);

void permute(char *str, int length)
{
if(length == 0)
{
f << full_string << std::endl; // line 17 error
++counter;
return;
}
else
{

// find the smallest char in the string set it to be the first character. solve the subproblem for

the smaller string.

char *smallest = min_element(str, str + length);
iter_swap(str, smallest);
permute(str+1, length-1);

// look for the smallest element strictly greater than the first element of the current string

char *smallest_greater = str + length;
for(char *i = str+1; i != str+length; ++i)
{
if(*i > *str && (smallest_greater == str + length || *i < *smallest_greater)) smallest_greater

= i;
}

while(smallest_greater != str+length)
{

// if such an element is found, swap it into the first slot and recurse

iter_swap(str, smallest_greater);
permute(str+1, length-1);

// repeat the loop if possible for the next greater character

smallest_greater = str + length;
for(char *i = str+1; i != str+length; ++i)
{
if(*i > *str && (smallest_greater == str + length || *i < *smallest_greater))

smallest_greater = i;
}
}
}
}

//iter_swap – it just swaps the elements pointed to by the respective pointers without changing the

pointers themselves. so, it's basically equivalent to the function:

void iter_swap(char *ptr1, char *ptr2)
{
char tmp = *ptr1; *ptr1 = *ptr2;
*ptr2 = tmp;
}

// min_element – finds the location of the minimum element that exists in the given range. in this case,

it is the char* pointer pointing to the location of that element. it can be implemented like:

char *min_element(char *start, char *end)
{
// end is 1 beyond the last valid element

if(start == end) return end; // empty range
char *min_pos = start;
for(char *iter = start+1; iter != end; ++iter)
{
if(*iter < *min_pos) min_pos = iter;
}
return min_pos;
}

int main()
{
std::ofstream f;
f.open("test.txt",std::ios::out);
permute(full_string, 6);
f.close();
return 0;
}
1. Code tags?
2. Do you spot the error on line 4?
3.
What is "f" right there? I don't know! Maybe if it was one of the parameters of the function...
I don't know, let's ask the user!

^ Compiler's thoughts on the line 17 error.
Last edited on
"f" is defined in "main". I thought that it needed to be declared with the "open" and "close" statements to help direct the output??
Exactly: It's defined in main.
But your function "permute" knows nothing about it.
You should pass it as a std::ofstream& parameter for "permute".
Remember to keep your "&". It means a reference, the parameter won't be copied and will be kept as in int main().
so, in "main", the statement... permute(full_string, 6); should read... permute(full_string, 6, std::ofstream& f); and the "permute" function should read... void permute(char *str, int length, std::ofstream& f), and then the function will recognize "f" ??? thanks, mille grazie! also, do I really have to have the "#include >ios>" ???
The error in line 4 is:
#include >ios>
should be
#include <ios>

About the other problem:
void permute(char* str, int length, std::ofstream& f)
and
permute(full_string,6,f);
Mi avete aiutato tanto!!!!!!!!!!
Topic archived. No new replies allowed.