-
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;
}
-
When you initialize Number2 to 1535, being less than (20+1) will never occur. So, the second FOR loop will never run.
Todd
-
Because 1535 is never less than 21.
-
The comments in the code don't seem to match the post, but:
Quote:
Originally Posted by
Coding
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.
-
Yes ofcourse... :)... Thanks...
-
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?