-4

I was trying to figure out this task, but so far have been unsuccessful. I think I understand the logic behind it, I just don’t know how to nest loops so it works (if that makes sense). I would very much appreciate your help!

Task: "Create an application in which a user enters full numbers until they enter number 0 (zero). The application should print out how many even numbers have been entered, how many odd numbers, sum of even numbers and sum of odd numbers, and total sum of numbers."

my code so far:

#include <iostream>
using namespace std;

void main() {

do {

int input1;
cout << "Type in a number";
cin >> input1;

} while (input1 != 0);

cout << "Type in a number";
cin >> input1;

if (input1 % 2 == 0)
{
 int even = 0;
 while (input1 % 2 == 0 )
  cout << even;
 even++;
}
else
{
 int odd = 0;
 while (odd != 0)
 {

  cout << odd;
  odd++;
 }
}

}

system("pause");
}

Note: I did not try to do the third part of the task, since the second one won't work :/ I figured out first part, and I did it with do while loop. Thanks again.

  • 1
    This algorithm is quite a way off from the intended goal - the best advice would be to read a [good book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also note that `void main` isn't valid C++. – hnefatl Nov 04 '17 at 18:09
  • Just as I thought...this doesn't even compile! – Aditi Rawat Nov 04 '17 at 18:13
  • *I figured out first part, and I did it with do while loop.* That loop in itself is faulty since it basically does nothing. It keeps on inputting the data but does nothing with it and also `input1` is local to that block! – Aditi Rawat Nov 04 '17 at 18:15
  • 1
    I think you went astray when you assumed that there would be any nesting of loops – there should only be one. All the processing and bookkeeping goes inside the reading loop. All the output goes after it. – molbdnilo Nov 04 '17 at 18:41

2 Answers2

1

Try this:

int oddCount = 0;
int evenCount = 0;
int oddSum = 0;
int evenSum = 0;

int in;
do
{
    std::cout << "Type in a number:";
    std::cin >> in;

    if (0 == in)
        break;

    if ( in % 2 == 0 )
    {
        evenCount++;
        evenSum += in;
    }
    else
    {
        oddCount++;
        oddSum += in;
    }
} while ( true );

std::cout << "Odd count: " << oddCount << std::endl;
std::cout << "Even count: " << evenCount << std::endl;
std::cout << "Odd sum: " << oddSum << std::endl;
std::cout << "Even sum: " << evenSum << std::endl;
std::cout << "Total sum: " << oddSum + evenSum << std::endl;
svm
  • 392
  • 3
  • 9
-1

Take a look at this piece of code:

#include <iostream>

using namespace std;

int main() {
    int input;
    int total_sum = 0, odd_sum = 0, even_sum = 0;
    int odd_count = 0, even_count = 0;

    do {
        cout << "Type in a number: ";
        cin >> input;

        total_sum += input;

        if (input % 2 == 0)
        {
            even_count++;
            even_sum += input;
        }
        else
        {
            odd_count++;
            odd_sum += input;
        }

    } while (input != 0);

    cout << "Total Sum: " << total_sum << endl;
    cout << "Even sum: " << even_sum << endl;
    cout << "Odd sum: " << odd_sum << endl;
    cout << "Even Count: " << even_count << endl;
    cout << "Odd Count: " << odd_count << endl;

    return 0;
}

See how input is declared outside of the loop. if it was inside it, then you essentially create it each time you enter the loop. that would be fine if you did not want to use its values outside of the loop (like in the loops condition).

Also, notice that the values you need to calculate can be updated within that same loop.

Yuval Ben-Arie
  • 1,280
  • 9
  • 14