# Thread: Need Help

1. ## Need Help

I have been given an assignment to write a C++ program that inputs integer numbers bewtween 0 and 100 until -1 is entered and then outputs the average of those numbers. The average must be shown with two decimal places, even if they are both 0. Your program must use at least one function. Please remember our class is still in the basic functions of programming. I also had to input those two lines with cout.setf, this was require by the teacher. Thanks for any help, our teacher is one of the worst ones I have had in my entire time at college.

[CODE]

#include<iostream.h>
using namespace std;

void prompt();

main()
{
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);

int score=0;
int total=0;
int count=0;
int sentinel=-1;
while (score!=sentinel)
{
prompt();
cin >> score;
if (score!=sentinel)
{
total=total+score;
count=count+1;
}
if (count > 0)
cout << "The average is: "
<< total/count << endl;
else
cout << "No genuine scores were entered"<< endl;
return 0;
}
void prompt ()
{
cout << "Enter a number: ";
}

2. Code:
```#include<iostream>
#include<iomanip>
using namespace std;

void prompt();

main()
{
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(2);

int score=0;
int total=0;
int count=0;
int sentinel=-1;
while (score!=sentinel)
{
prompt();
cin >> score;
if (score!=sentinel)
{
total=total+score;
count=count+1;
}
}
if (count > 0)
cout << "The average is: "
<< (double)total/(double)count << endl;
else
cout << "No genuine scores were entered"<< endl;
return 0;
}
void prompt ()
{
cout << "Enter a number: ";
}```

3. total=total+score;

has capacity to only have a total of 2^32.. 4 billion. If you enter 2 numbres that amount to more than that the result will be quite unexpected.

I would declare:

double total = 0.0;

and at least avoid the obvious overflow...

4. achacha, he said that the inputted numbers would be less than or equal to 100. You'd be there for quite some time entering 100 to get to the max integer. I don't think declaraing total a double is necessary. I do think, however, that it could be made an unsigned long.

5. ## Thanks for the help guys.

I appreciate your input, I got stuck with this computer programming class. I did not realize this computer class was for computer programming majors. I am a finance major. As long as the program works and the teacher gives me a B or C grade I am happy. It does not help that this is one of the worst teachers have had. I'll be posting more questions in a couple of weeks.
Thanks.

Shawn

Also, what is parse error.

6. A parse error is an error that occurs during the parsing phase of the compile process, it is also called the syntactic analysis.

Popular pages Recent additions