-
Programming
I have a C++ function that assigns a character an ASCII value. It will create a wordlist of aa, ab, ac, etc. I have similar functions for numbers and upper case letters. I'm having trouble making one that would combine lowercase and upper case letters. Can anyone help me out please?
Code:
void lowercase()
{
ofstream a_file("lower.txt");
for (int counter = 97; counter < 123; counter ++)
{
for (int count =97;count < 123; count ++)
{
num[0] = counter;
num[1] = count;
a_file<< num << endl;
}
}
a_file.close();
return;
}
-
This really isn't the site to be asking those type of programming questions on. Try this. This has helped me a ton. CppHome.com Good luck.
Regards,
Xierox
-
The main problem is here: a_file<< num << endl;
when you're saying num you're actually saying it's adress, something like 0x09...
as I figured, num is an array... so...: a<<num[0]<<num[1]<<endl;
Furthermore, you were saying something about letters, so you should call a function to convers those ASCII into characters: a<<char(num[0])<<char(num[1])<<endl;
That's about it... oh... and this is not the place to ask such questions... :D
-
Now off the top here, what do you mean you have a problem? Simply extend the boundaries of your for loops to include the int values for the uppercase letters. Since there are some chars you would not want in there [92 to 96] simply make an if clause before assigning to num
Something on the lines of
Code:
for (int counter = 65; counter < 123; counter ++)
{
for (int count =65;count < 123; count ++)
if(count!=92 && count!=93... && counter!=92...) //you get the working here
{
num[0] = counter;
num[1] = count;
a_file<< num << endl;
}
}
That is not the prettiest code I have seen, certainly. You could use a constant declaration for all the values you would use.
Code:
int values[52]={65,66,67,68...};
Then you could simply loop through this array and assign to count/counter the int value of each element
Code:
for (int counter = 0; counter < 52; counter ++)
{
for (int count =0; count < 52; count ++)
{
num[0] = values[counter];
num[1] = values[count];
a_file<< num << endl;
}
}
As said, this is only off the top here :)
[edit]
reply was written during previous post. I'm not sure about the a_file << num but it does look a little odd. also, depending on your compiler the type cast could or could not be done inherently. it is recommended that you use the method outlined in the post above.
as a note, it is helpful if you actually say what problem you are having. compiler output error, if it's a bug etc. it really speeds up the process.
[/edit]
-
One possible solution to loop thru 65 to 122 while skipping 91 to 96. (You won't find many code with the ? : operator in for's... it just came across my mind :) )
Code:
char num[3];
num[2] = 0;
for (int counter = 65; counter < 123; counter == 90 ? counter += 7 : counter ++)
{
for (int count = 65; count < 123; count == 90 ? count += 7 : count ++)
{
num[0] = counter;
num[1] = count;
// printf("%d %d %s\n", counter, count, num);
a_file << num << endl;
}
}
Anyway, making this forum more active won't do any harm... :D In fact it's fun!
Peace always,
<jdenny>
-
Hmm jdenny nice code. Yeah, not a lot of ?: used nowadays but I don't think we should forget that some things are easier done in a simpler fashion.