2 For-Loops

This is a discussion on 2 For-Loops within the C++ Programming forums, part of the General Programming Boards category; I use this code to do a count for the variable "counting" ! As seen in the code the First ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    2 For-Loops

    I use this code to do a count for the variable "counting" !

    As seen in the code the First for-loop do this counting and put this to the "Mainfile" as it is written but without the // ofcourse before.


    Below I have a Second For loop. As seen I am counting from:
    1535 until (20 + 1).

    If I put back the // before the First for loop before MainFile and try to use the Second ouput for MainFile nothing is written to the file.

    So the problem is why does the First MainFile write to the file but not the other one ?
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    
    using namespace std;
    int main () 
    { 
    	int Combinations = 0;
    	int counting = 0;
    	
    	ofstream MainFile;
    	MainFile.open ("Main.txt");
    	ifstream StockFile ("CCE.txt");
    
    	for (int Number = 1535; Number < 1555; Number++)			
    	{
    	        if ((Number >= 1535) && (Number <= 1555))
    		
    		{
    		counting = (counting + 1);						
    		//MainFile << counting << "\n";
    		}
    	}
    	
    
    		for (int Number2 = 1535; Number2 < (20 + 1); Number2++)	
    		{
    			if ((Number2 >= 1535) && (Number2 <= 1555))
    			{
    			MainFile << Number2 << "\n";
    			}
    		}
    
    	return 0;
    }
    Last edited by Coding; 01-11-2008 at 09:26 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When you initialize Number2 to 1535, being less than (20+1) will never occur. So, the second FOR loop will never run.

    Todd

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Because 1535 is never less than 21.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The comments in the code don't seem to match the post, but:
    Quote Originally Posted by Coding View Post
    Code:
    		for (int Number2 = 1535; Number2 < (20 + 1); Number2++)	
    		{
    			if ((Number2 >= 1535) && (Number2 <= 1555))
    			{
    			MainFile << Number2 << "\n";
    			}
    		}
    
    	return 0;
    }
    This for-loop will execute for all numbers greater than 1535 and less than 21 (==20+1). I am not aware of any numbers that are greater than 1535 and less than 21.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes ofcourse... ... Thanks...

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    for (int Number = 1535; Number < 1555; Number++)			
    	{
    	        if ((Number >= 1535) && (Number <= 1555))
    		
    		{
    And here, since your loop runs from 1535 to 1554 the if condition will always be true, so you might just remove the check.

    But what is it that you want to do? Count from any number upwards 20 times?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21