[RESOLVED] Novice nested loop issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] Novice nested loop issue

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    [RESOLVED] Novice nested loop issue

    I'm writing a series of basic decipher programs and I have run into an issue where I get the correct answer when I start the loops at the iteration that contains the correct answer.

    Obviously I'm new here so if there are any issues with the way im posting this problem feel free to point them out.

    Code:
     // generate key "words" with length of 3
        for (int x = 0; x < 26; x++){
            for (int y = 0; y < 26; y++){
                for (int z = 0; z < 26; z++){
                    
                    for (int i = 0; i < arraySize; i++){
                        if (text[i][0] >= 97 && text[i][0] <= 122){
                            
                            if ( keyPos == 0 ){
                                text[i][1] = (text[i][0] - x);
                                if ( text[i][1] < 97 ){
                                    text[i][1] = (text[i][1] + 26);
                                }
                                keyPos++;
                            }
                            else if (keyPos == 1){
                                text[i][1] = (text[i][0] - y);
                                if ( text[i][1] < 97 ){
                                    text[i][1] = (text[i][1] + 26);
                                }
                                keyPos++;
                            }
                            else if (keyPos == 2){
                                text[i][1] = (text[i][0] - z);
                                if ( text[i][1] < 97 ){
                                    text[i][1] = (text[i][1] + 26);
                                }
                                keyPos = 0;
                            }
                        }
                        else {
                            text[i][1] = text[i][0];
                        }
                    }
                    
                    if (x == 17 && y == 7 && z == 12){
                        for (int i = 0; i < arraySize; i++){
                            cout << text[i][1] << int(text[i][1]) << " ";
                        }
                        cout << "\n\n";
                    }
                }
            }
        }
    This is the essence of the loop, I've attached the program in its entirety if necessary. Basically what happens is if I start the loops at x = 17, y = 7, z = 12, then I get the correct decipher shifts but if I start at 0,0,0 whenever it gets to that iteration (12,000 ish) the shifts are off by 2 or 3. "koq" should translate to "the" but im getting "dcz". Is this a simple bug in the or is something moving to fast for something else to keep up?

    Thanks for the input!!

    l3_ws.txt
    main.cpp

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,844

    Re: Novice nested loop issue

    Have you tried to debug your program using the debugger to understand why it's not working as expected? You mention nothing about this. If the program does not do what you expect, you start by debugging your code. After you have done this and you still have a query about the c++ language and its use, then come back to us.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center