![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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;
} 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]; |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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
![]() |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Could I suggest using recursion?
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
char text = abc[b] && abc[c] && abc[d] && abc[e] && abc[f] && abc[g] && abc[h] && abc[i] && abc[j]; 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. |
|
|
|
|
|
#5 | ||
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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:
Quote:
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. |
||
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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! ![]() |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
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]; |
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
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;
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|