Like Tree1Likes
  • 1 Post By qny

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

This is a discussion on Write a loop that reads positive integers from standard input..... within the C++ Programming forums, part of the General Programming Boards category; Hey guys, very new to programming and need some help with a homework problem. Write a loop that reads positive ...

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

    Talking 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.

    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.

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

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Wrong board? I think you want the C++ Programming board.
    Click_here likes this.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Quote Originally Posted by qny View Post
    Wrong board? I think you want the C++ Programming board.
    No, this is a C programming class.

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by Gundown64 View Post
    No, this is a C programming class.
    Well, then.
    In C there is no "cout" or "cin"; the "<<" token has a very different meaning than in the usage in your code.

    Anyway, your program appears to be almost done.
    You need to introduce the variables sumodd and numodd and an else part for the if.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I see two problems. In the loop that does the counting and adding you have to use an if-else because if you don't you might add a number to both totals. A number can either be even or odd, so you want to do one or the other thing.

    The other problem is that the cout statement does not do what you think it does. To use cout properly always insert with operator <<.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Quote Originally Posted by qny View Post
    Well, then.
    In C there is no "cout" or "cin"; the "<<" token has a very different meaning than in the usage in your code.

    Anyway, your program appears to be almost done.
    You need to introduce the variables sumodd and numodd and an else part for the if.
    Oh, woops, my bad. Yes, I am in C++, don't know why I thought C, but thanks for the tips.

    Also, if anyone can move this thread, that would be great.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Ok guys, I started working on this again. Cleaned a few things up and got my output to add spaces between the numbers. It's still not quite right, but it is getting closer. Here is my code:

    Code:
    int num=0;
    int sum1=0;
    int sum2=0;
    int sumeven=0;
    int sumodd=0;
    int evencount=0;
    int oddcount=0;
    
    
    do
    {
    cin >> num;
    if (num % 2 == 0 && num > 0)
    {
    evencount++;
    sum1=num++;
    sumeven = sum1 + sumeven;
    } 
    else if (num > 0)
    {
    oddcount++;
    sum2++;
    sumodd = sum2 + sumodd;
    }
    }
    while (num > 0);
    cout<<sumeven;
    cout<<" "<<sumodd;
    cout<<" "<<evencount;
    cout<<" "<<oddcount;
    The program does a test input and output and here are the results.
    Input: 2 4 6 8 0
    My codes output: 15 0 5 0
    Correct output: 20 0 4 0

    So as you can see, I am close, at least, for those numbers. I need help figuring out how to get it correct. Thanks in advance!

    EDIT - Ok, I realize that I need another integer here. What I have for sumeven and sumodd isn't going to work because this is not adding the correct things. I need some variable, I'll call sum, to equal the sum of the numbers entered. How do I get the integer sum to do that; to be the sum of every number that is entered?

    EDIT 2 - Ok, I am getting the first part of the output to work - "prints out the sum of all the even integers read" - Still need to get the other three parts working correctly.
    Last edited by Gundown64; 09-30-2012 at 08:33 PM. Reason: Added updated code.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,390
    You should post your problem in the C++ forum

    Code:
    if (num % 2 == 0 && num > 0)
    {
        evencount++;
        sum1=num++;
        sumeven = sum1 + sumeven;
    }
    That should be num % 1 for even/odd
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by Click_here View Post
    That should be num % 1 for even/odd
    Rubbish. The remainder on dividing any integral value by 1 is zero.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Quote Originally Posted by Click_here View Post
    You should post your problem in the C++ forum

    Code:
    if (num % 2 == 0 && num > 0)
    {
        evencount++;
        sum1=num++;
        sumeven = sum1 + sumeven;
    }
    That should be num % 1 for even/odd
    Whaaaat? Whats the use of having num%1 ??

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    if (num % 2 == 0 && num > 0)
    {
         evencount++;
         sum1=num++;
         sumeven = sum1 + sumeven;
    }
    Incrementing "num" after assigning it to "sum1" is unnecessary. Thus "sum1" is unnecessary too because it's the same as "num". You can reduce the body of your if-statement to
    Code:
    evencount++;
    sumeven += num;
    Code:
    else if (num > 0)
    {
         oddcount++;
         sum2++;
         sumodd = sum2 + sumodd;
    }
    Why do you increment "sum2" and add it to "sumodd"? You should add "num" to "sumodd" as you do for even numbers.

    Bye, Andreas

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,390
    Quote Originally Posted by poornaMoksha View Post
    Whaaaat? Whats the use of having num%1 ??
    I'm so sorry - I don't know what I was thinking typing that. I think that I was thinking of num &1.

    You are right. I've been misreading things all day. I'm sorry again!!
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Quote Originally Posted by AndiPersti View Post
    Code:
    if (num % 2 == 0 && num > 0)
    {
         evencount++;
         sum1=num++;
         sumeven = sum1 + sumeven;
    }
    Incrementing "num" after assigning it to "sum1" is unnecessary. Thus "sum1" is unnecessary too because it's the same as "num". You can reduce the body of your if-statement to
    Code:
    evencount++;
    sumeven += num;
    Code:
    else if (num > 0)
    {
         oddcount++;
         sum2++;
         sumodd = sum2 + sumodd;
    }
    Why do you increment "sum2" and add it to "sumodd"? You should add "num" to "sumodd" as you do for even numbers.

    Bye, Andreas
    Thank you very much! That was the solution! The sum1 and sum2 were just something I was testing, but I knew they were wrong. Thank you! Here is the finished code:

    Code:
    int num=0;
    int sumeven=0;
    int sumodd=0;
    int evencount=0;
    int oddcount=0;
    
    
    do
    {
    cin >> num;
    if (num % 2 == 0 && num > 0)
    {
    evencount++;
    sumeven += num;
    } 
    else if (num > 0)
    {
    oddcount++;
    sumodd += num;
    }
    }
    while (num > 0);
    cout<<sumeven;
    cout<<" "<<sumodd;
    cout<<" "<<evencount;
    cout<<" "<<oddcount;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to check whether input is positive...
    By Giri in forum C Programming
    Replies: 10
    Last Post: 09-04-2012, 12:37 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, 04:11 PM
  5. Positive integers
    By cxs00u in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2002, 05:11 PM

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