Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 10th, 2005, 6:27 PM   #1
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Post For loops, a lot, outputting weird characters

Ok... So I'm learning a few new things, and think I know the problem with my code now, but don't know how to fix it First, I'll start off by showing the code:
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream ostream;
    ostream.open("WL.txt", fstream::in);
    ofstream wstream ("WL.txt");
    char abc[70] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    if (wstream.is_open())
    {
             for (int a = 1; a < strlen(abc); a++)
             {
                 for (int b = 1; b < strlen(abc); b++)
                 {
                     for (int c = 1; c < strlen(abc); c++)
                     {
                         for (int d = 1; d < strlen(abc); d++)
                         {
                             for (int e = 1; e < strlen(abc); e++)
                             {
                                 for (int f = 1; f < strlen(abc); f++)
                                 {
                                     for (int g = 1; g < strlen(abc); g++)
                                     {
                                         for (int h = 1; h < strlen(abc); h++) 
                                         {
                                             for (int i = 1; i < strlen(abc); i++)
                                             {
                                                 for (int j = 1; j < strlen(abc); j++)
                                                 {
                                                     char text = abc[b] && abc[c] && abc[d] && abc[e] && abc[f] && abc[g] && abc[h] && abc[i] && abc[j];
                                                     wstream << text << endl;
                                                     }
                                                 }
                                             }
                                         }
                                     }                
                                 }
                             }
                         }
                     }
                 }
             }
    wstream.close();
    ostream.close();
    return 0;
}
Ok, so this should make a permutation of every letter and character possible, correct? Now I know that running this, will soon blow up my computer because the text file will probably be bigger then my computers memory... But, it's for my math teacher to show him that C++ could calculate every single possible permutation of every number and letter possible But when I compile this, it compiles fine. But when I run it, for like one second, I get a 1,000 Kb size file with "" in it... But why? I think it's because of this part right here:
char text = abc[b] && abc[c] && abc[d] && abc[e] && abc[f] && abc[g] && abc[h] && abc[i] && abc[j];
Well, could anyone tell me if I am either way off, or I am on track, but a logic error needs fixing? Thanks... :eek:
layer is offline   Reply With Quote
Old May 10th, 2005, 6:49 PM   #2
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Oh yea, one more thing, when writing a console application, any way to get the X and Y coordinates of the cursor. I know how to set it, but how to you retrieve it? Thanks
layer is offline   Reply With Quote
Old May 11th, 2005, 6:40 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Could I suggest using recursion?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 11th, 2005, 7:50 AM   #4
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
char text = abc[b] && abc[c] && abc[d] && abc[e] && abc[f] && abc[g] && abc[h] && abc[i] && abc[j];
You're right this line is your problem, or at least part of it (I didn't notice anyting else wrong, but didn't look).

I guess you're trying to concatenate a seris of single characters using &&'s and assign them to a string?

well you can't use && to do that, and you can't assign a string to a single char variable.

what you are doing is assigning the result of an expression to the variable text, which will always equal 1, and then printing it as a character, so check the ascii value corresponding to 1 to see why it's printing garbage.

off the top of my head I can't think of an elegant way to concatenate the characters. you could do something like
char text[10];
text[0] = abc[a];
text[1] = abc[b];
...
text[9] = '\0';

or you can just directly print the characters without the temp variable at all
wstream << abc[b] << abc[c] << abc[d] << abc[e] << abc[f] << abc[g] << abc[h] << abc[i] << abc[j] << endl;


I would try running for a smaller subset of the alphabet when testing. As you've already admitted yourself you are going to run into, at the least, memory issues. You're already at what ?... 62^9 iterations with the code you have there.

Last edited by spydoor; May 11th, 2005 at 7:59 AM.
spydoor is offline   Reply With Quote
Old May 11th, 2005, 2:13 PM   #5
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Talking

Ok... I tried using the second method by just writing them directly, spydoor... And I get the right output, except in the output, before the actual numbersd and letters I want, I get this:
Quote:
0x22fdd0bbbbbbbb
Here's a small portion of the file (theres like tons more times a million that gets outputted in a second...)
Quote:
0x22fdd0bbbbbbbbbb
0x22fdd0bbbbbbbbbc
0x22fdd0bbbbbbbbbd
0x22fdd0bbbbbbbbbe
0x22fdd0bbbbbbbbbf
0x22fdd0bbbbbbbbbg
0x22fdd0bbbbbbbbbh
0x22fdd0bbbbbbbbbi
0x22fdd0bbbbbbbbbj
0x22fdd0bbbbbbbbbk
0x22fdd0bbbbbbbbbl
0x22fdd0bbbbbbbbbm
0x22fdd0bbbbbbbbbn
0x22fdd0bbbbbbbbbo
0x22fdd0bbbbbbbbbp
0x22fdd0bbbbbbbbbq
0x22fdd0bbbbbbbbbr
0x22fdd0bbbbbbbbbs
0x22fdd0bbbbbbbbbt
0x22fdd0bbbbbbbbbu
0x22fdd0bbbbbbbbbv
0x22fdd0bbbbbbbbbw
0x22fdd0bbbbbbbbbx
But I dont want the "0x22fdd0bbbbbbbb"... I just want the "bbv". Anyway to do this? Or do you know why I get the 0x22fdd0bbbbbbb?

Again, thanks

EDIT: Also, Ooble, I just skimmed over a small Recursion tutorial, because I haven't touched up on that yet, I mean, I know what it is and all, just gotta find the right way to use it Thanks for the suggestion though!

Last edited by layer; May 11th, 2005 at 2:17 PM.
layer is offline   Reply With Quote
Old May 11th, 2005, 3:11 PM   #6
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
No, I don't know a specfic reason why, but it is odd that you opened two streams to the same file, and one of them presumable for reading in input (fstream::in), which you never use.

This may be the cause of your problem, but I tried your code that way and it actually did work for me.

Another problem may be where you're actually printing the charcters (would have to see your code);

Here's an example of code that should work using only one fstream.

    fstream ostream;
    ostream.open("WL.txt" , fstream::out);

    char abc[70] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    if (ostream.is_open())
    {
		//all the for loops...
				ostream << abc[b] << abc[c] << abc[d] << abc[e] << abc[f] << abc[g] << abc[h] << abc[i] << abc[j] << endl;												
		//close loops...
    }

    ostream.close();
    return 0;

Last edited by spydoor; May 11th, 2005 at 3:17 PM.
spydoor is offline   Reply With Quote
Old May 11th, 2005, 3:30 PM   #7
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Hmm... The new code you supplied took away the 0x22 but still leaves an extra 5 or 6 b's... Maybe I'll just trim some characters out of the file when outputting them, cause I gotta go now

Thanks for your help though spydoor!
layer is offline   Reply With Quote
Old May 12th, 2005, 8:26 AM   #8
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
you're printing too many b's because your algorithm for finding all the permutations is wrong. A quick search turned up a couple of solutions to the problem, iteratively and recursively. Your solution looks like it would end up printing N^N lines while it should be N!

http://www.google.com/search?q=strin...ions+iterative
spydoor is offline   Reply With Quote
Old May 12th, 2005, 3:48 PM   #9
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
you can also do this using bytes, iterating the bytes, and outputing them by casting them as chars. something like this

byte someThing=0;
for(int x=0; x<256; x++)
{
cout <<char(byte);
byte++;
}

of course, you'd need to test the byte for value within a range.

you could likewise use an array of bytes
byte something[6];
brkstf is offline   Reply With Quote
Old May 12th, 2005, 4:57 PM   #10
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
how about making:

byte someThing=0;
for(int x=0; x<256; x++)
{
cout <<char(byte);
byte++;
}

A lot simpler...

for(int i=0;i<256;i++)
    cout << (char)i;
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC