Thank you very much for that insight.
I read the Bridge Out Ahead in your signature and laughed... lol
I have taken both of your suggestions in to account and added more on to the program.
Thank you for the System("PAUSE") functionality. I should have recognized that because I do write Batch. I didn't make the connection however.
Laserlight, thank you for letting me know about the array initialization. I have fixed that.
I have added a few more lines of code and commented everything. Thanks for all of your assistance.
In linux, I have gedit set to treat all warnings as errors. In Dev-C++ I don't know how to set that up yet. I have used Visual Studio before but it has A LOT more than what I need at this point and I don't know everything that it does. I will probably switch over to that when I have a better understanding of errors and can understand debugging a little better.
Here is the newest copy of the code. It compiles and works perfectly
Code:
/*********************************************/
/* This program will ask how many students */
/* are in the class. Based upon your answer */
/* it will then ask what the ages are for */
/* each student. From the answers provided */
/* it will round to the nearest whole number */
/* the average age for the class. */
/*********************************************/
#include <stdio.h> // Allows printf() function
#include <math.h> // Allows the roundf function to be used.
int main(int argc, char *argv[])
{
// introduce and initialize the variables to be used within the program.
char numOfStudents=0,a=0,b=0;
float sum=0,average=0;
// Since we need to repeat the question if a wrong answer is provided we use a for loop.
// The if statements determine whether or not a correct answer was provided.
for(a=2;a>0;a--) {
printf("How many students do you have (1-10)? ");
if (scanf("%d*c",&numOfStudents)) {
if(numOfStudents<1) {
printf("Come on, how can you have a class with %d students?\n",numOfStudents);
a=2;
}
else if(numOfStudents>10) {
printf("Your class cannot hold more than 10 students.\n");
a=2;
}
else
a=0;
}
else {
printf("A valid number was not entered. This program will now exit.\n");
numOfStudents=0;
a=0;
}
}
// Now that we know how many Students we have in the class, we need to initialize the array.
char studentsAge[numOfStudents];
// First, test to see if we made it out of the for loop above with a correct answer.
// Then ask a proper question with the switch statement based upon the number of students
if(numOfStudents>0) {
switch(numOfStudents) {
case 1: printf("What is your students age? (5-65)\n");
break;
default: printf("What are your students ages? (5-65)\n");
}
}
// Great, we have made it asking about the age of the students. Here we are going to use a
// for loop to determine what the students ages are just like we did above.
// We exit the program if a valid number is not entered.
for(b=0;b<numOfStudents;b++) {
printf("Student %d: ",b+1);
if (scanf("%d*c",&studentsAge[b])) {
if(studentsAge[b]<5) {
printf("The youngest student allowed in this class is 5.\n",numOfStudents);
b=b-1;
}
else if(studentsAge[b]>65) {
printf("The oldest person allowed in this class is 65\n");
b=b-1;
}
else {
sum=sum+studentsAge[b]; // Used to add up all of the students ages
}
}
else {
printf("A valid number was not entered. This program will now exit.\n");
sum=0;
numOfStudents=0;
b=0;
}
}
// Check to see if sum is greater than 0. If it is then we must have a correct answer for both numOfStudents and sum.
// We display the combined ages for everyone in the class. We now figure out the average age for the class and display it.
if(sum>0) {
average=sum/(float)numOfStudents;
printf("The total added age of every student is: %.00f\n",sum);
printf("The average age of the students is %.0f.\n",roundf(average));
}
system("PAUSE"); // Proprietary to Windows. Keeps the command line window open when it reaches the end of the program.
return 0;
}