-
for loop problems
Hey everyone. Thanks for the help on the while loops. But now I'm confused on for loops. I'm writing the same program, just trying to replace the while loops with for loops. Here's what I got.... but when I compile, the same three errors keep coming up with "!=" twice and with "==". Can I not put operations like this in the for loop stuff?
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int firstNum;
int secondNum;
int sum;
int squareSum;
int counter;
int i;
int square;
char c;
cout << "Enter two numbers." << endl;
cout << "First number must be less than or equal "
<< "to the second number you enter" << endl;
cout << "Enter numbers: ";
cin >> firstNum >> secondNum;
cout << endl;
counter = firstNum + 1;
if (firstNum <= secondNum)
{
cout << "Odd integers between " << firstNum << " and "
<< secondNum << " are: \n";
for (counter % 2 != 0; counter < secondNum; counter++)
{
cout << counter << " " << endl;
}
}
else if (firstNum > secondNum)
cout << "There are no odd integers between " << firstNum << " and "
<< secondNum <<endl;
sum = 0;
counter = firstNum + 1;
if (firstNum <= secondNum)
{
for (counter % 2 == 0; counter < secondNum; counter++)
{
sum += counter;
}
cout << "\nSum of even integers between " << firstNum << " and "
<< secondNum << " = " << sum << endl;
}
else if (firstNum > secondNum)
cout << "\nThere are no even integers between " << firstNum << " and "
<< secondNum << endl;
cout << left << setw(10) << "Number"
<< setfill(' ') << right << "Square of Number" << endl;
i = 1;
for (square = i * i; i <= 10; i++)
{
cout << right << setw(4) << i << setfill(' ') << right << setw(18) << square << endl;
}
cout << endl;
counter = firstNum + 1;
squareSum = 0;
if (firstNum <= secondNum)
{
for (counter % 2 != 0; counter < secondNum; counter++)
{
squareSum = squareSum + (counter * counter);
}
cout << "Sum of the squares of odd integers between "
<< firstNum << " and " << secondNum << " = ";
cout << squareSum << endl;
}
else if (firstNum > secondNum)
cout << "There are no odd integers between " << firstNum << " and "
<< secondNum <<endl;
cout << "Upper case letters are: ";
for (c = 'A'; c <= 'Z'; c++)
{
cout << c << " ";
}
cout << endl;
return 0;
}
-
for loops work like this:
for(initialization; condition; increment);
Initialization: The first time the loop runs, this code is executed.
Condition: While this is true, the loop continues (like the condition in a while).
Increment: This happens with each iteration of the loop.
ie,
Code:
for(int x = 0; x < 10; x ++) {
cout << x << endl;
}
prints
0
1
2
3
4
5
6
7
8
9
-
> for (counter % 2 == 0; counter < secondNum; counter++)
Should be something like
for (counter = firstNum; counter < secondNum; counter++)
-
SYNTAX of for loops:
Code:
for (initialize; condition; statement)
steatement;
for (int a = 0; a < 3; a++)
cout << 3;
-
-
-
So you put a condition in the initialization part of the for loop?
-
-
Why were you asking if I'd read your post? Or were you not asking me?
-
No, he was asking me. Yeah, sorry... I remember that you had posted that, but totally spaced it when I went to changing everything around. Thanks. It works now!!!