Thread: Variable In While Loop

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22

    Variable In While Loop

    I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At the beginning of my program, I initialize a variable "sum" to 0, and a variable "temp" to 1. Afterwards I enter a loop where I determine if "temp" is even or not, and if so add it to sum. However, at the end of my program, when I print "sum", I get a result of 0. Below is my code.

    Code:
    #include <stdio.h> 
    
    
    int main(void)
    {
       int sum = 0, temp = 1; 
       
       while(temp <= 100)
       {
                  if(temp % 2 == 0)
                          sum += temp; 
                  temp = temp + 1; 
       }
       
       printf("Sum is %i", sum); 
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I compiled and ran your program to get the output:
    Code:
    Sum is 2550
    It might help to write a blank line at the end, e.g.,
    Code:
    printf("Sum is %i\n", sum);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22
    Nevermind, it works. I just ran the program and got the right output.

  4. #4
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22
    How can I get rid of the extra blank lines when I post my code within tags?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You sure you're actually compiling that code? It should give a value of 2550. It does so on my system.

    Try deleting the executable, object files, and rebuild from scratch. In an IDE, do a "Build Clean", then "Build All" (noting those processes may have slightly different names, depending on IDE).

    Note that some IDE's will output an additional line reporting that the program exits with return value zero. With those IDE's you need to scroll up one line. It would help if the printf() statement appended a '\n' at the end (i.e. printf("Sum is %i\n", sum)) to avoid output being overwritten.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner, help with loop and variable
    By keeganstarr in forum C Programming
    Replies: 16
    Last Post: 05-28-2012, 07:31 PM
  2. looping a variable within a loop.
    By vespine in forum C Programming
    Replies: 3
    Last Post: 06-27-2010, 10:30 PM
  3. While loop with variable problem
    By Todd88 in forum C++ Programming
    Replies: 33
    Last Post: 04-06-2008, 07:36 PM
  4. Variable declaration with `for' loop
    By slippy in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2007, 03:14 PM
  5. Loop variable
    By anirban in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 10:36 PM