Okay, I made a new program for my next assignment, and I found some new information. First the program:
//setup
#include<cstdio>
#include<cmath>
int main(void)
{
//declare variables
double DataIn, Average, Sum;
int NGrades, Value;
//print title
printf ("Program \x22");
printf ("Average\x22 \n\n");
printf ("This program calculates the average of an arbitrary number\n");
printf ("of grades.\n\n");
printf ("After all the grades have been entered, any negative number\n");
printf ("is entered, and the averaging is performed.\n\n");
//get input
NGrades = 0;
Sum = 0.0;
Value = 0;
while (Value == 0)
{
scanf ("%lf", &DataIn);
if (DataIn > 0)
{
Value = 0;
Sum = DataIn + Sum;
++NGrades;
}
else (++Value);
}
//calculate
Average = Sum / NGrades;
//output data
switch (NGrades)
{
case 0:
printf ("\nNo valid grades have been entered.\n\n");
printf ("An average cannot be computed.");
break;
case 1:
printf ("\nThere is only one grade whose value is %4.2lf.", Average);
break;
default:
printf ("\nThere are %d grades, with an average of %4.2lf.", NGrades, Average);
}
printf ("\n\n");
return 0;
}
This one is much shorter, but it took me forever to figure out how to write the while loop correctly. I also learned more about Visual C++ 8, aka VC++ 2005. The only real difference between the free version and the $300 version, so far as I can tell, is that the free version doesn't have the ANSI-C/C++ libraries. That's okay, because the CLI libraries are for the most part identical, you just have to remember to use iostream and not iostream.h and cmath instead of math.h, etc. Also, for iostream you have to add the line using namespace std;
otherwise you cin's and cout's won't work. I also figured out why the consoles were always closing after executing instead of having that "Press any key to continue" statement at the end. That was because I was pressing F5 which runs the "real program" instead of CTRL-F5 which runs the "troubleshooting program" which does have that statement at the end. The same thing happens in my studen VC++ 6.0, so everything's cool now.