vendredi 30 juin 2017

Beginning C++: using if else, n--, number

I am struggling to get this program to work. Could someone please look it over to see what is wrong? (The compiler says that sum_positive is not defined in this way.) Also, I have searched this problem and found an answer on here, but I did not see the problem that way and want to avoid plagiarism when I turn in my homework. Thank you!

  1. Write a program that reads in ten whole numbers and that outputs 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.

Program:

#include <iostream>

using namespace std;

int main()
{

int number, n = 10, sum_positive = 0, sum_negative = 0, sum = 0;
    int count = 0, positive_count = 0, negative_count = 0;

    cout << "Please enter in 10 whole numbers, one at a time." << endl;

    do
    {
        cout << "Please enter another number." << endl;
        cin >> number;
        n--;
    }
    while (n > 0);

    if (number >= 0)
    {
        sum_positive += number;
        //positive_count++; count++;
    }
    else
    {
        sum_negative += number;
        //negative_count++; count++;

    }


    sum = sum_positive + sum_negative;

    cout << sum_postive << endl;
    cout << sum_negative << endl;
    cout << sum << endl;

return 0;
}

Aucun commentaire:

Enregistrer un commentaire