for loop problems

This is a discussion on for loop problems within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. Thanks for the help on the while loops. But now I'm confused on for loops. I'm writing the ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question 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;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > for (counter % 2 == 0; counter < secondNum; counter++)
    Should be something like
    for (counter = firstNum; counter < secondNum; counter++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    SYNTAX of for loops:

    Code:
    for (initialize; condition; statement)
       steatement;
    
    for (int a = 0; a < 3; a++)
       cout << 3;

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    My point exactly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    http://cboard.cprogramming.com/showt...890#post478890

    Did you happen to read my post???????????

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    So you put a condition in the initialization part of the for loop?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    huh?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why were you asking if I'd read your post? Or were you not asking me?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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