[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Loop troubles

In the following code, for some reason, i have to loop it 52 times (26 x 2) inorder to have it go through 26 iliterations. Anyone know why? Here is the code:

namespace CompilerTester
{
    class Tester
    {
        static void Main(string[] args)
        {
            CodeStack Code = new CodeStack();
            Code.EnqueueCommand(CellCommands.AddToCell, 65);

            for (int i = 0; i < 52; ++i)
            {
                Code.EnqueueCommand(CellCommands.OutputCommand, 0);
                Code.EnqueueCommand(CellCommands.AddToCell, 1);
            }

            Console.WriteLine(Code.RunCode());
            Console.ReadLine();
        }
    }
}

This is from an side project to test a interpreter for a set of simple commands. This should add 65 to the cell, and then output the character for the value of the cell, increase it by one, and do output it again, 26 times. Changing the for loop from "for (int i = 0; i < 52; ++i)" to "for (int i = 0; i < 26; ++i)" ends up with 13 letters or 1/2 of the alphebet with the 52 outputs the whole thing. Any reason for this seemgly glitch (I am, btw, using a Microsoft Visual C# 2005 Express Edition Beta)
0
cimnik029
Asked:
cimnik029
1 Solution
 
AaronReamsCommented:
uh, my guess would be 26 lower case letters + 26 upper case letters = 52 total iterations.  am i missing something?
0
 
cimnik029Author Commented:
no, its only to get the capital letters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ <- thats the exact output.

Plus thats not possible as there are a handful of characters that rest between the caital Z and the lowercase a
0
 
AaronReamsCommented:
hmmm, good point... ascii 91-96 aren't letters.  well i just took a wild guess.  i'll have to think about it some more.  good luck.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sendmeblahCommented:
What happens when you try a while loop?

int i=0
while (i < 26)
{
   Code.EnqueueCommand(CellCommands.OutputCommand, 0);
   Code.EnqueueCommand(CellCommands.AddToCell, 1);
   i++
}

Do you have a hyperthreaded machine?  I have seen weird things happen in if statements concerning the ++ operator in VS 2003.
0
 
cimnik029Author Commented:
same problem persists. and no i dont have a hyperthreaded machine.
0
 
TheAvengerCommented:
I didn't find the class CodeStack in the VS 2005 Beta 1 documentation so I suppose it's some code you wrote, or? What does it do? I suppose the problem is inside the method EnqueueCommand
0
 
cimnik029Author Commented:
i wonder. should have posted code. here is the summerized version

CommandType in an enum, Command is a struct to hold a command (command function and paramater)

class CodeStack
    {
        public Queue Code;

        public void EnqueueCommand(CellCommands CommandType, int Value)
        {
            Command NewCommand = new Command();
            NewCommand.CommandType = CommandType;
            switch ((int)CommandType)
            {
                case 1: // NextCellCommand;
                    NewCommand.ACommand = new NextCellCommand();
                    break;
                case 2: // PreviousCellCommand
                    NewCommand.ACommand = new PreviousCellCommand();
                    break;
                case 3: // AddToCellCommand
                    NewCommand.ACommand = new AddToCellCommand();
                    break;
                case 4: // SubtractFromCellCommand
                    NewCommand.ACommand = new SubtractFromCellCommand();
                    break;
                case 5: // JumpCommand
                    NewCommand.ACommand = new JumpCommand();
                    break;
                case 6: // OutputCellCommand
                    NewCommand.ACommand = new OutputCommand();
                    break;
            }
            NewCommand.ACommand.Table = MemoryTable;
            NewCommand.CommandValue = Value;
            Code.Enqueue(NewCommand);
        }
0
 
cimnik029Author Commented:
i figured it out! what a dumn question!

i had a loop statment in my code look like this

for (int i = 0; i < Code.Count; ++i)

Code is a queue, and in every iliteration, as i went up, code.count went down. Therefore, when dealing with 8 elemenst, it would stop half way as i and code.count would end up both equaling 4 half way through and quiting, creating the illusion of a loop at the beginning that needed to be run 2 times as much. I figured out the error when steping through the code showed that all the commands got stored, but only half, with the inter loop counter stoping half way through. I changed the code to this and it works.

int LoopCount = Code.Count;
for (int i = 0; i < LoopCount; ++i)
0
 
SRigneyCommented:
It's not a dumb question, things like that happen.  I hope that this experience shows you that you should post all of the code in question.  The code you posted does not contain the loop wth Code.Count in it, so the experts here didn't have the opportunity to find it and help you.

Congratulations on getting the problem solved.
0
 
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now