Thread: for loop only running once

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    for loop only running once

    this is my code, i added the for loop with p as the iterator to try to loop the program 9 times but it only runs once in the console window;

    Code:
    using five;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ai5
    {
        class Program
        {
            static void Main(string[] args)
            {
                for(int p = 0; p < 9; ++p)
                {
                    part_one read_list = new part_one();
                    part_two input_from_while = new part_two();
                    part_fourteen forward = new part_fourteen();
                    part_fifteen happy_1 = new part_fifteen();
                    part_seventeen looping_and_record = new part_seventeen();
                    part_nineteen two_lines = new part_nineteen();
                    string book_choice_sentence = ""; // the user input sentence
                    int use_record = 0; // part_9, step_fourty_five, user inputs 1 then use_record = 1, user inputs 2 or 3 then use_record = 0
                    int no_test = 0; // gets a special message in looping_and_record.step_fourty_two
                    int is_input_good = 0; // part_4, step_9, used to break the while loop meaning the book_choice_sentence holds a valid user input
                    int no_follow_counter = 0; // used for the array holding the lines in disagree.txt
                    string temp = ""; // the last line in disagree.txt, and is the last line in solution.txt at the time the user chose no.
                    int i_copy = 0; // for the end()
                    string entry_1 = ""; // part_9, step_fourty_five, holds 1 for use input_record.txt, 2 for use user input, or 3 for no input.
                    int check = 0; // if in looping_and_record.step_fourty_two the user enters 3, then the line in disagree.txt is erased and put into change_topic.txt
                    int ignore_counter = 0; // counts the lines in change_topic.txt
                    int found = 0; // for when the user input "book_choice_sentence" matches a line in change_topic.txt
                    string original_input = ""; // for use in the end when i make happy_two.txt
                    int test_2 = 0; // for removing too much automatic response, erases the automatic file.
                                    //read_list.no_follow_step_1(ref no_follow_counter);
                                    //string[] no_follow_array = new string[no_follow_counter];
                                    //read_list.no_follow_step_2(no_follow_array, no_follow_counter); // the lines in disagree.txt
                    int test = 0; // for when i use the automatic input, 1 for yes
    
                    read_list.the_beginning();
    
                    int previously_counter = 0;
                    happy_1.previously_step_1(ref previously_counter);
                    string[] previously_array = new string[previously_counter];
                    happy_1.previously_step_2(previously_array, previously_counter); // the lines in happy_one.txt
    
                    int article_counter = 0;
                    input_from_while.article_step_1(ref article_counter);
                    string[] article_array = new string[article_counter];
                    input_from_while.article_step_2(article_array, article_counter); // the lines in article.txt
                    int article_array_counter = 0;
    
                    if (article_counter > 0)
                    {
                        for (int i = 0; i < 4; ++i)
                        {
                            i_copy = i;
    
                            found--;
    
                            Console.WriteLine("/////////////////////\r\n");
    
                            Console.WriteLine("Building " + 4 + " sentences. this is sentence # " + (i + 1) + "\r\n");
    
                            if (i > 0)
                            {
                                // step 0
    
                                // part_9
                                looping_and_record.step_fourty_two(ref book_choice_sentence,
                                                                ref use_record,
                                                                ref no_test,
                                                                temp,
                                                                ref entry_1,
                                                                check,
                                                                no_follow_counter);
                                book_choice_sentence = book_choice_sentence.Trim();
    
                                if (test_2 == 1)
                                {
                                    File.WriteAllText("automatic_limit.txt", String.Empty);
                                    test_2 = 0;
                                }
                            }
    
                            // method 2 of getting input in book_choice_sentence
                            // looping_and_record.step_fourty_two user chooses 2 = (use record = 0)
                            // (use record = 0) means the user writes a new sentence to be used for input
    
                            int second_while_loop_run = 0;
                            input_from_while.the_while_loop(article_array,
                                                    use_record,
                                                    ref book_choice_sentence,
                                                    ref is_input_good, i,
                                                    ref test_2,
                                                    second_while_loop_run,
                                                    ref article_array_counter,
                                                    article_counter);
    
                            if (i == 0)
                            {
                                original_input = book_choice_sentence;
    
                                for (int j = 0; j < previously_counter; ++j)
                                {
                                    if (previously_array[j] == "") continue;
                                    if (previously_array[j] == null) continue;
                                    if (previously_array[j] == book_choice_sentence)
                                    {
                                        forward.the_beginning(i, book_choice_sentence, ref test);
                                        break;
                                    }
                                }
    
                                if (test == 0)
                                {
                                    happy_1.ignore_step_1(ref ignore_counter);
                                    string[] ignore_array = new string[ignore_counter];
                                    happy_1.ignore_step_2(ignore_array, ignore_counter); // the lines in change_topic.txt
                                                                                         // ignore_counter is counting the lines in change_topic.txt
    
                                    happy_1.setting_happy_one(article_array,
                                        ignore_array,
                                        ignore_counter,
                                        ref found,
                                        i,
                                        ref book_choice_sentence,
                                        ref article_array_counter,
                                        article_counter);
                                }
                                else if (test == 1)
                                {
                                    break;
                                }
                            }
    
                            // if(ignore_counter > 0), the lines in change_topic.txt were compared to the current input "book_choice_sentence"
                            // if i find a match, the following condition is false for 2 rounds then found = 0
                            if ((i > 0) && (found <= 0))
                            {
                                // step 3
    
                                // the previous line in solution.txt is compared to the current input "book_choice_sentence"
                                // to see if the flow logically together or not
    
                                two_lines.step_fifty(book_choice_sentence,
                                                    ref no_test,
                                                    ref temp,
                                                    use_record);
    
                                entry_1 = "";
                            }
                            else
                            {
                                StreamWriter solution_2 = new StreamWriter("solution.txt", true);
                                solution_2.WriteLine(book_choice_sentence);
                                solution_2.Close();
                            }
    
                            // for symmetry.txt
                            if (no_test != 1)
                            {
                                StreamWriter temp_symmetry = new StreamWriter("temp_symmetry.txt", true);
    
                                if (book_choice_sentence != "")
                                {
                                    temp_symmetry.WriteLine(book_choice_sentence);
                                }
    
                                temp_symmetry.Close();
                            }
                        }
    
    
                        // for symmetry.txt
                        StreamWriter temp_symmetry_dot = new StreamWriter("temp_symmetry.txt", true);
    
                        temp_symmetry_dot.WriteLine(".");
    
                        temp_symmetry_dot.Close();
    
                        if (test == 0)
                        {
                            // the the newest temp_symmetry.txt to the top of symmetry.txt
                            part_twenty symmetry = new part_twenty();
                            symmetry.step_C();
                        }
    
                        part_twenty_one wrap_it_up = new part_twenty_one();
                        wrap_it_up.the_end(article_array,
                                        i_copy,
                                        original_input,
                                        ref article_array_counter,
                                        article_counter);
    
                        part_three write = new part_three();
                        write.step_O();
    
                        StreamWriter solution_3 = new StreamWriter("solution.txt", true);
                        solution_3.WriteLine(".");
                        solution_3.Close();
    
                        File.WriteAllText("compare_article.txt", String.Empty);
    
                        string record_sentence = "";
    
                        StreamReader article = new StreamReader("article.txt");
                        StreamWriter compare_article = new StreamWriter("compare_article.txt", true);
    
                        while ((record_sentence = article.ReadLine()) != null)
                        {
                            if (record_sentence == "") break;
    
                            compare_article.WriteLine(record_sentence);
                        }
                        article.Close();
                        compare_article.Close();
                    }
                    else
                    {
                        Console.WriteLine("article.txt is empty. fill it with about 10 lines of text.");
                    }
                }
            }
        }
    }
    i looked online for an answer and found out this in a javascript forum with the same looping problem;

    Code:
    The problem is in your iterators (i):
    
    for (i = 0; i <= 5; i++)
    i is global, and both your for loops test against it, making them only run once, and aborting when i == 5.
    
    So, what happens is this:
    
    When you call foo(), foo tells the js interpreter to create a variable in the global scope called i, and set it to 0. Then foo calls f1.
    There, the for loop sets i, which already exists, to 0, and runs it's loop like it should, incrementing i up to 5.
    Then, it's time for the second iteration of the loop in foo, so it checks if i < 5, but it's not (i==6 (5 from f1, +1 from foo)), so it will not call f1 again.
    
    To fix this, either declare them in the function's local scope using var:
    
    function f1() {
        for (var i = 0; i <= 5; i++)
            console.log(i);
    }
    function foo() {
        for (var i = 0; i < 5; i++)
            f1();
    }
    foo();
    Or, use different variables:
    
    function f1() {
        for (i = 0; i <= 5; i++)
            console.log(i);
    }
    function foo() {
        for (j = 0; j < 5; j++)
            f1();
    }
    foo();
    However, this second option is a bad idea, since it will both place i and j in the global scope, which is asking for conflicts. I'd suggest using the var option.
    which i dont understand. but its something maybe youll understand.
    Last edited by jeremy duncan; 06-15-2017 at 06:44 PM. Reason: pasted wrong code to show my problem

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    i was giving the wrong input. it was returning what it was supposed to for the input i was giving it. i made a mistake. when i put the code without the p for loop in a class then called the function in main in a for loop then the program returned what it was supposed too, just a public void function in the class called in main. so no problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop is running twice
    By Nikitaatwork in forum C Programming
    Replies: 4
    Last Post: 11-08-2011, 06:42 PM
  2. Can't get loop to keep running
    By mytrademark in forum C Programming
    Replies: 2
    Last Post: 11-01-2011, 06:08 PM
  3. running through a loop using pointers
    By boy in forum C Programming
    Replies: 43
    Last Post: 08-19-2011, 04:43 PM
  4. Running a program in a loop
    By Robert_Sitter in forum C# Programming
    Replies: 7
    Last Post: 01-27-2006, 10:09 AM
  5. Running triangles through loop
    By shanedudddy2 in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 12:59 AM

Tags for this Thread