Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++. Write a program that reads in ten whole numbers?

Write a program that reads in ten whole numbers and that output the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.

Assume the user will type integer numbers.

this is what i got but it wont run saying there is an error

#include<iostream>;

using namespace std;

int main()

{

int count=0;

int num;

int positive=0;

int negative=0;

do

{

cout<<"Enter 10 whole numbers"<<endl;

cout<<num;

if (num<=0)

{

negative= negative+num;

}

else

{

positive= positive+num;

}

count++;

}

while (count<10);

cout<<" Sum of postive number= "<< positive<<endl;

cout<<" Sum of negative number= "<<negative<<endl;

cout<<" Sum of all numbers= " <<positive+ negative<<endl;

}

Update:

well when i run my program it repeats the question"Enter 10 number" 10 times..

4 Answers

Relevance
  • cja
    Lv 7
    7 years ago
    Favorite Answer

    The problem is that you're not getting any input from the user. You should try something like this:

    #include <iostream>

    #include <string>

    #include <sstream>

    using namespace std;

    const size_t N = 10;

    int getInt(void);

    int main(int argc, char *argv[]) {

        int n, sumNeg = 0, sumPos = 0;

        cout << "Enter " << N << " whole numbers:" << endl;

        for (size_t i = 0; i < N; i++) {

            if ((n = getInt()) > 0) {

                sumPos += n;

            } else {

                sumNeg += n;

            }

        }

        cout << " Sum of postive number = " << sumPos << endl;

        cout << " Sum of negative number = " << sumNeg << endl;

        cout << " Sum of all numbers = " << sumPos + sumNeg << endl;

        return 0;

    }

    int getInt() {

        string line;

        stringstream ss;

        bool inputOk = false;

        int n;

        cout << "> ";

        do {

            getline(cin,line);

            ss.clear(); ss.str(line);

            if ((!(ss >> n)) || (ss.good())) {

                cout << "invalid input, try again" << endl << "> ";

            } else {

                inputOk = true;

            }

        } while (inputOk == false);

        return n;

    }

    #if 0

    Sample run:

    Enter 10 whole numbers:

    > 1

    > 2

    > -9

    > 0

    > 5

    > -2

    > -5

    > 3

    > 0

    > 10

      Sum of postive number = 21

      Sum of negative number = -16

      Sum of all numbers = 5

    #endif

  • Anonymous
    7 years ago

    Just move the

    cout<<"Enter 10 whole numbers"<<endl;

    outside the loop

    #include<iostream>

    using namespace std;

    int main(){

      int count=0;

      int num;

      int positive=0;

      int negative=0;

      cout<<"Enter 10 whole numbers"<<endl; //this

      do{

        cout<<num;

        if (num<=0)

          negative= negative+num;

        else

          positive= positive+num;

        count++;

      }

      while (count<10);

      cout<<" Sum of postive number= "<< positive<<endl;

      cout<<" Sum of negative number= "<<negative<<endl;

      cout<<" Sum of all numbers= " <<positive+ negative<<endl;

    }

  • ?
    Lv 7
    7 years ago

    Remove semicoln

    #include<iostream> //<---

    Also

    cout<<" Sum of all numbers= " <<(positive+ negative)<<endl;

  • 5 years ago

    Thanks everyone for the answers.

Still have questions? Get your answers by asking now.