Results 1 to 6 of 6

Thread: Programming

  1. #1
    Senior Member
    Join Date
    May 2004
    Posts
    206

    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;
    }
    It is better to die on your feet than to live on your knees.

  2. #2
    Hoopy Frood
    Join Date
    Jun 2004
    Posts
    662
    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
    "Personality is only ripe when a man has made the truth his own."

    -- Søren Kierkegaard

  3. #3
    Junior Member
    Join Date
    Jul 2004
    Posts
    5
    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...
    Never interrupt your enemy when he is making a mistake.

  4. #4
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    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]
    /\\

  5. #5
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    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 &lt; 123; counter == 90 ? counter += 7 : counter ++)
    {
      for (int count = 65; count &lt; 123; count == 90 ? count += 7 : count ++)
      {
        num[0] = counter;
        num[1] = count;
        // printf("%d %d %s\n", counter, count, num);
        a_file &lt;&lt; num &lt;&lt; endl;
      }
    }
    Anyway, making this forum more active won't do any harm... In fact it's fun!

    Peace always,
    &lt;jdenny&gt;
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  6. #6
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    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.
    /\\

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •