Another new person - incrementing

This is a discussion on Another new person - incrementing within the C++ Programming forums, part of the General Programming Boards category; I have two problems to work. This one uses n = 9 and the other one uses n = 10. ...

  1. #1
    MB1
    MB1 is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    Another new person - incrementing

    I have two problems to work. This one uses n = 9 and the other one uses n = 10. ++i means to increment i from 1 to 2 before determining if the integer is odd or even but when I run the program with both numbers, I get a blank answer.
    Suggestions please!


    Code:
    #include <iostream>
    using namespace std;
    #include <conio.h>
    
    int main()
    {
    	int n = 9;
    	int i = 1;
    	while (i < n) {
    		if ((i % 2) == 0)  {
    			++i;
    		}
    	}
    	cout << i << endl;
    getch();
    return 0;
    }

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Firstly, you are running into a loop issue. Your declaration states:

    While i is less than n
    > If i is odd
    >> Increment i

    What if i isn't odd? Your loop stalls and will not increment your variable.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Think about your logic here will this loop ever end?
    The answer is no because if n = 3 then 3 % 2 is one so i will not increment and your loop will run forever.
    Woop?

  4. #4
    MB1
    MB1 is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    I thought that it was saying if the integer was even (==0) increment i and that it would do so up to the point that i was not less than n

    So if n = 9, the number is odd and the loop stalls. Wouldn't my screen display '1' since that is the assigned value to begin with?

    If n = 10, then the loop would continue up to the point of (i = 10) in which case the loop would terminate and'10' would display on my screen! ???
    Last edited by MB1; 02-23-2005 at 04:27 PM.

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by MB1
    I thought that it was saying if the integer was even (==0) increment i and that it would do so up to the point that i was not less than n

    So if n = 9, the number is odd and the loop stalls. Wouldn't my screen display '1' since that is the assigned value to begin with?

    If n = 10, then the loop would continue up to the point of (i = 10) in which case the loop would terminate and'10' would display on my screen! ???
    Well, think about it...when n=3, 3%2 certainly isn't zero, so i doesn't even get incremented....ever. Therefore, your loop is stuck. i will never be > 10.
    Last edited by Krak; 02-24-2005 at 03:45 AM.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    If you haven't got the picture yet it's always good to insert cout in your code so you know what's happening. Compile the code below and you will see what you have done wrong.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int n = 9;
      int i = 1;
      int countLoops = 1;
    
      while (i < n) {
        cout << "i is: " << i << " while has looped: " << countLoops
        << " times" <<  endl;
        if ((i % 2) == 0)  {
          ++i;
        }     
        countLoops++;
      }
      cout << i << endl;
    
      return 0;
    }
    As you see 1%2 will never be 0, therefore the code never get's into the if statement and i will always be 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Having a problem!
    By Zildjian in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2004, 10:40 AM
  3. the us constitution
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 121
    Last Post: 05-28-2002, 05:22 AM
  4. 1st Person Shooter
    By Okiesmokie in forum Game Programming
    Replies: 3
    Last Post: 01-09-2002, 12:25 AM
  5. luckiest person in the world == me
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 01-06-2002, 08:00 PM

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