Fix the attached program (CountNegativeNumbers.cpp) so that it will display how
ID: 666402 • Letter: F
Question
Fix the attached program (CountNegativeNumbers.cpp) so that it will display how many negative numbers are entered from the user. The attached image (countNegNum.gif) shows a sample output.
Only two lines of code in the program need to be modified or added.
#include <iostream>
using namespace std;
//**************** main function ****************************
void main()
{
cout << "This program count how many negative numbers you enter. There will be three sections. Enter 0 to end a section." <<endl<<endl;
for (int i=0; i<3; i++)
{
cout << " ************ SECTION " << i+1 << "**************"<<endl;
cout << " You have entered " << getInput() << " negative numbers so far."<<endl;
cout << "**************************"<<endl<<endl;
}
return;
}
//***************** count function *********************
int getInput()
{
double number; //hold number entered by the user
count=0;
//get input
do
{
cout << "Enter a number: ";
cin >> number;
//count positive number
if (number < 0)
count ++;
} while (number !=0);
//return counting result
return count;
}
Explanation / Answer
Here is the your code. I have fixed all errors:
#include <iostream>
using namespace std;
//***************** count function *********************
int getInput()
{
double number; //hold number entered by the user
int count=0;
//get input
do
{
cout << "Enter a number: ";
cin >> number;
//count positive number
if (number < 0)
count ++;
} while (number !=0);
//return counting result
return count;
}
//**************** main function ****************************
int main()
{
cout << "This program count how many negative numbers you enter. There will be three sections. Enter 0 to end a section." <<endl<<endl;
for (int i=0; i<3; i++)
{
cout << " ************ SECTION " << i+1 << "**************"<<endl;
int j=getInput();
cout << " You have entered " << j<< " negative numbers so far."<<endl;
cout << "**************************"<<endl<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.