CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Help!

  1. #1
    Join Date
    Sep 2008
    Posts
    5

    Help!

    I am trying to write a code that asks for 10 whole numbers to be entered. The program will then compute the sum of the numbers greater than zero, the sum of the numbers less than zero, and then the total sum. I can not get the program to do this for me no matter what I do. PLEASE HELP!!! Also I am using "Pico"

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help!

    Perhaps if you posted your efforts so far.

  3. #3
    Join Date
    Sep 2008
    Posts
    5

    Re: Help!

    include <iostream>
    using namespace std;
    int main (void)
    {
    int count=0;
    int number;
    int sum = 0;
    int sum_greater, sum_less, total_sum;

    while (count < 10)
    {
    cout << "Enter a whole number, then press enter:\n";
    cin >> number;
    count++;

    if (number > 0)
    {
    cout << "The sum greater than zero is:\n";
    sum_greater = sum + number;
    cout << sum_greater;
    }
    }
    return 0;
    }

    If you enter this code into pico or whatever you use you will see the problem I am having.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help!

    1) You never modify sum, so your sum_greater = sum + number statement is equivalent to just plain sum_greater = 0 + number;
    2) You never initialize sum_greater. If it's starting value is 54243423, which it may well be, your final output won't be useful.

  5. #5
    Join Date
    Sep 2008
    Posts
    5

    Re: Help!

    Even so, when I run this program it doesnt take ten numbers and then compute. It does it for each number entered until there is 10 entries. I know the if else statement needs to be in the loop. I will do the things you said, but how do I get the program to take ten numbers and then add them?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help!

    You'd need to input the 10 numbers into separate variables (typically an array of size 10), and then have two loops: One to read the values, and the second to operate on them.

    However, there's an easier way to adjust the code to acheive the same result: Do the calculations as-you-go (no need for an array or a second loop), but just move the cout statement of the result outside the loop.

  7. #7
    Join Date
    Dec 2007
    Posts
    13

    Re: Help!

    you can also input the number into a int array. such as int num[10].
    Then the program will like this.

    for(int i=0;i<10;i++)
    {
    if(num[i]>0)
    sum=sum+num[i];
    }







    ----------------------------------------------------
    codeuu,source code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured