# Simple program that needs tweaking

• January 14th, 2013, 05:01 PM
f0rumh4x
Simple program that needs tweaking
Below is the program I have written yet I can't seem to get it to work right. I have to believe that there is also I more efficient way to write this program. A little direction as to where I might have gone wrong would be great.

// Purpose: generate a class average for entered test scores
// Input: # of tests, test scores
// Output: total number of tests, average score of tests

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//declare variables
short numberOfTests = 0;
short testScores = 0;
short sum = 0;
short testNumber = 1;
double averageScore = 0.0;

do //begin loop
{
cout << "Enter the number of tests: ";
cin >> numberOfTests;

if (numberOfTests >= 1 && numberOfTests <= 100)
cout << "You entered " << numberOfTests << " as the number of tests. " << endl;
else
cout << "The number must be between 1 and 100 (inclusive). ";
//end if
} while (numberOfTests < 1 || numberOfTests > 100);

testScores = numberOfTests;

while (numberOfTests > 0)
{
cout << "Enter the score for test #" << testNumber << ": " << endl;

{
testNumber += 1;
numberOfTests -= 1;
}

cout << "You must enter a valid number between 0 and 100 (inclusive) as a score. ";
} //end while

averageScore = sum / testScores;
cout << "There were a total of " << testScores << " test scores. " << endl;
cout << "The average of all the test scores is: " << averageScore << endl;

cin.get();
return 0;
}
• January 14th, 2013, 05:58 PM
GCDEF
Re: Simple program that needs tweaking
First, you should use code tags when posting code.

Second, you should state what problem you're having.

All I see is this line "cout << "You must enter a valid number between 0 and 100 (inclusive) as a score. " should be inside the if statement that tests score validity, and if you want a floating point result from a mathematical operation, at least one of the operands needs to be floating point.
• January 14th, 2013, 06:02 PM
f0rumh4x
Re: Simple program that needs tweaking
I apologize for not being clearer with the issue. The program closes after it completes the first do while statement. There are no errors when it compiles though. I appreciate the input and this is my first attempt at solving a problem from scratch.
• January 14th, 2013, 06:12 PM
f0rumh4x
Re: Simple program that needs tweaking
Code:

```#include <iostream> #include <iomanip> using namespace std; int main() {         //declare variables         short numberOfTests = 0;         short testScores = 0;         short scoreGrade = 0;         short sum = 0;         short testNumber = 1;         double averageScore = 0.0;         do //begin loop         {                 cout << "Enter the number of tests: ";                 cin >> numberOfTests;                                 if (numberOfTests >= 1 && numberOfTests <= 100)                         cout << "You entered " << numberOfTests << " as the number of tests. " << endl;                 else                         cout << "The number must be between 1 and 100 (inclusive). ";                 //end if         } while (numberOfTests < 1 || numberOfTests > 100);         testScores = numberOfTests;                 while (numberOfTests > 0)         {                 cout << "Enter the score for test #" << testNumber << ": " << endl;                 cin >> scoreGrade;                 if (scoreGrade >= 0 && scoreGrade <= 100)                 {                         //update accumulator and counter                         sum += scoreGrade;                         testNumber += 1;                         numberOfTests -= 1;                 }                                 cout << "You must enter a valid number between 0 and 100 (inclusive) as a score. ";         } //end while         averageScore = sum / testScores;         cout << "There were a total of " << testScores << " test scores. " << endl;         cout << "The average of all the test scores is: " << averageScore << endl; cin.get(); return 0; } //end of main function```
• January 14th, 2013, 06:21 PM
GCDEF
Re: Simple program that needs tweaking
I ran it in the debugger. All the code executed. cin.get() isn't doing what you think it is. Try getch if you want to stop the execution and wait for input.
• January 14th, 2013, 06:32 PM
f0rumh4x
Re: Simple program that needs tweaking
Thank you for the quick replies. Leaving campus now but will be back on the forums later and let you know how it turned out.
• January 14th, 2013, 08:59 PM
f0rumh4x
Re: Simple program that needs tweaking
Here is the finished product that I have been working on. And it runs without any problems now. Thanks :)
Code:

```#include <iostream> #include <iomanip> using namespace std; int main() {         //declare variables         short numberOfTests = 0;         short testScores = 0;         short scoreGrade = 0;         short scoreSum = 0;         short testNumber = 1;         double averageScore = 0.0;         do //begin loop         {                 cout << "Enter the number of tests: ";                 cin >> numberOfTests;                 if (numberOfTests >= 1 && numberOfTests <= 100)                         cout << "You entered " << numberOfTests << " as the number of tests. " << endl;                 else                         cout << "The number must be between 1 and 100 (inclusive). ";                 //end if         } while (numberOfTests < 1 || numberOfTests > 100);         testScores = numberOfTests;         while (numberOfTests > 0)         {                 cout << "Enter the score for test #" << testNumber << ": " << endl;                 cin >> scoreGrade;                 if (scoreGrade >= 0 && scoreGrade <= 100)                 {                         //update accumulator and counter                         scoreSum += scoreGrade;                         testNumber += 1;                         numberOfTests -= 1;                 }                 else                         cout << "You must enter a valid number between 0 and 100 (inclusive) as a score. ";         } //end while         averageScore = static_cast<double>(scoreSum) / static_cast<double>(testScores);         cout << "There were a total of " << testScores << " test scores. " << endl;         cout << fixed << setprecision(2);         cout << "The average of all the test scores is: " << averageScore << endl; cin.get(); return 0; }```