Hello all...
I have been working on this for about a week now, which is probably pretty pathetic to admit. This assignment is for a beginners C Ansi programming class. I have created this using the "for", "do" and "while" loops, and am missing the same thing every time.
Here is my problem:
When the user enters a negative number, an error message is initiated. The program is then supposed to ignore that and prompt the user for the number again. For example, if it were positive value # 3, and a negative number was entered, then the negative number would be ignored, and the program would prompt for #3 again.
I'm sure this is something stupid I keep doing over again, since I keep coming up with the same anwer, but I would truly appreciate any insight you could give me. Even if you were to simply ask me a question to put me in the right direction. I seem to have entered my own loop, and can't get out.
Thanks in advance!!!!!![]()
Very humbly .....
Kathy
here is the code:
#include <stdio.h>
void main (void)
{
/*------------Declare Variables--------------------------*/
int value,x;
int tot_val = 0;
int pos_int = 0;
float average;
/*------------Describe what the program does-------------*/
printf("\nThis program calculates the average of as many numbers as you wish to enter.");
/*------------Give instructions to the user--------------*/
printf("\n\nFirst, enter the number of values to process:");
scanf("%i", &value);
printf("\nNow enter the %i numbers to be averaged.\n\n",value);
/*------------Enter the loop information----------------*/
for (x=1; x <= value; x++)
{
printf("Enter the positive integer #%i: ", x);
scanf ("%i", &pos_int);
tot_val=tot_val + pos_int;
{
if(pos_int < 0 )
printf("\n***Invalid entry. Value must be positive.\n");
continue;
}
pos_int++;
}
/*-------------Figure the average--------------------- */
average = (float)tot_val / value;
printf("\n\nThe average of the %i numbers entered is %.2f\n.",value,average);
}//end main



LinkBack URL
About LinkBacks




