GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2008, 12:14
mxb1145 mxb1145 is offline
New Member
 
Join Date: Oct 2008
Posts: 3
mxb1145 is on a distinguished road

Writing an Output file


I'm having problems writing an output file from a permutation function. The file takes in 6 letters and then makes all possible permutations, but I need those permutations to be outputted to a file because I'm going to need to compare them to another file. My current code only outputs what I believe is the last permutation charater to the file. If anyone can help me figure out where the error lies it would be really appreciated. Here is the code:

I
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>

void perm(char v[], int, int);
void swap(char v[], int, int);
void print(char);

using namespace std;

int main ( )
{
	char next;
	int count = 0;
	int i = 0;

	char letters[7];
	
	cout << "Gabriela Zambrano" << endl;
	cout << "Project 1: Permuations" << endl;
	cout << "CS2300 001" << endl;
	cout << "--------------------------------------" << endl;
	cout << "\n" << endl;
	cout << "Please enter up to 6 characters for the program to begin:" << endl;
	cin >> next;
	while(count < 7 && next != '\n')
	{
		cout << next;
		if(count < 6)
		{
			letters[i] = next;
			i++;
		}
		count++;
		cin >> next;
	}
	perm(letters,count,0);
	
	
	system("PAUSE");
	return 0;
}

void perm(char v[], int size, int begin)
{
	int j;
	char n = v[size];
	char ii = v[begin];
	if(n == ii)
	{
		for(j=0; j< size; j++)
		{
			ofstream temp("printout.txt");
			temp << v[j];
			temp.close();
		}
		cout << endl;
	}
	else
	{
		for(j = begin; j < size; j++)
		{
			swap(v,begin,j);
			perm(v,size,begin+1);
			swap(v,size,j);
		}
	}
}

void swap(char v[], int x, int y)
{
	char t;
	t = v[x];
	v[x] = v[y];
	v[y]= t;
}
  #2  
Old 09-Dec-2008, 14:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Writing an Output file


Quote:
Originally Posted by mxb1145
I'm having problems...

First of all, by default, the "cin>>next" operation skips whitespace, so it can never return a '\n' character. If you want to test with fewer than six characters, you could use cin.get(). Many C++ programmers would use getline(), but it's OK with get(). Anyhow, I think debugging will go a lot faster if you try it with two or three characters to begin, and then work your way up to six.

CPP / C++ / C Code:
    cin.get(next);
    while (count < 7 && next != '\n') {
        cout << next;
        if (count < 6) {
            letters[i] = next;
            i++;
        }
        count++;
        cin.get(next);
    }

Secondly, I would either initialize the array to all zeros, or, more likely, I would explicitly put the terminating '\0' byte in the array so that I could print out the contents for debugging purposes:

CPP / C++ / C Code:
    cin.get(next);
    while (count < 7 && next != '\n') {
        cout << next;
        if (count < 6) {
            letters[i] = next;
            i++;
        }
        count++;
        cin.get(next);
    }
    letters[count] = '\0';
    // Uncomment the following four lines for debugging
    cout << endl << "count = " << count << endl;
    cout << "Calling perm(" << letters << "," << count << ")" << endl;
    cout << "Press 'Enter' to continue: ";
    cin.get(); // Throw the newline away
    perm(letters, count, 0);

The extra statements that I put for debugging just might be helpful.

Thirdly, when you are putting stuff out to the file, you reopen it as a brand-new file each time.

I would probably try debugging by putting stuff to cout to make sure the permutation was working, then re-introduce the file operations after I was happy with the results:
CPP / C++ / C Code:
void perm(char v[], int size, int begin)
{
    int j;
    char n = v[size];
    char ii = v[begin];
    if (n == ii) {
        for (j = 0; j < size; j++) {
            cout << v[j];
        }
        cout << endl;
    }
    else {
.
.
.
}

Then when you put the file stuff back in, note that if you want to add something to a file rather than opening it up anew (and therefore overwriting its previous contents), you use the ios::app mode:
CPP / C++ / C Code:
        ofstream temp("printout.txt", ios::app);
If you do it this way, you have to make sure that you delete the old version of printout.txt before running the program again. There may be other ways to write to a file (by opening the file in main() and passing a reference to the file as an argument to the perm function, for example) so that you don't have to be opening and closing so many times.

And, by the way, did you wonder why your screen blanked out when you ran the program? (I did.) Look carefully at the output stuff in your original perm. (Why is there "cout << " there?)

Regards,

Dave


[c]
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 04:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.