Thread: Write a loop that reads positive integers from standard input.....

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    Write a loop that reads positive integers from standard input.....

    Hey guys, very new to programming and need some help with a homework problem.

    Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by at least one space. Declare any variables that are needed.
    Note a few things:
    - This does not need to be a complete program, just what is asked above.
    - This need to be a do-while loop.
    - The spaces between the numbers is important, but I don't know how to get spaces. How do I set up the output to be spaced numbers like 1 2 3 4 instead of 1234?

    Here is what I have so far:
    Code:
    int num, sum=0;
    int sumeven=0;
    int numeven=0;
    int totalnum=0;
    do 
    {
    cin >> num;
    if (num % 2 == 0 && num >= 0)
    {
    sumeven = sumeven + num;
    numeven++;
    }
    sum = sum + num;
    totalnum++;
    }
    while (num>0);
    cout<< sum, sumeven, numeven, totalnum;
    Right now, the problem is the program is simply adding up ALL the numbers, not the odd, evens, etc. Also, I know I need to add numodd and sumodd still, but I am still just lost.

    Again, I am very new to this so go easy on me .
    Thanks in advance!

    PS - Accidentally posted this in the C forum so I am reposting it here.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Respond here:
    http://cboard.cprogramming.com/c-pro...ard-input.html

    Maybe a mod can close this one and move the existing thread here.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-01-2012, 08:00 AM
  2. Replies: 4
    Last Post: 10-29-2011, 07:02 PM
  3. find the totals of positive and negative integers
    By noaksey in forum C Programming
    Replies: 5
    Last Post: 05-11-2004, 01:59 PM
  4. Replies: 6
    Last Post: 11-04-2002, 05:11 PM
  5. Positive integers
    By cxs00u in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2002, 06:11 PM