Use a fixed-count for loop to have five numbers entered. - C++ Statement

C++ examples for Statement:for

Description

Use a fixed-count for loop to have five numbers entered.

Demo Code

#include <iostream>
using namespace std;
int main()//  ww w  .  ja v a  2s .co  m
{
   const int MAXNUMS = 5;
   int i;
   double usenum, positiveSum, negativeSum;
   positiveSum = 0; // this initialization can be done in the declaration
   negativeSum = 0; // this initialization can be done in the declaration
   for (i = 1; i <= MAXNUMS; i++)
   {
      cout << "Enter a number (positive or negative) : ";
      cin  >> usenum;
      if (usenum > 0)
         positiveSum = positiveSum + usenum;
      else
         negativeSum = negativeSum + usenum;
   }
   cout << "The positive total is " << positiveSum << endl;
   cout << "The negative total is " << negativeSum << endl;
   return 0;
}

Result


Related Tutorials