All I'm trying to do is write a very simple loop program in C to enter the ages of four sprogs between 5 and 7. To display their total and average age using do, while and if statements. I also need to display an error message if an invalid number is entered then when there are no more ages to enter to type in -99 to stop the loop and display the results.
Simple... you'd think! :rolleyes:

I appreciate this is really basic stuff but for some unknown reason my compiler dislikes my 'if' statement no matter where I put it in the program and also insists on throwing a hissy fit regarding the last '}'.

Any suggestions would be greatly appreciated.

{ 
int age1, age2, age3, age4;
float number = -99;
float total;
float average;
 
do
{ 
printf ( "Please enter the first child's age\n" );
scanf ( "%f", &age1 );
fflush(0);
 
printf ( "Please enter the second child's age\n" );
scanf ( "%d", &age2 );
fflush(0);
 
printf ( "Please enter the third child's age\n" );
scanf ( "%d", &age3 );
fflush(0);
 
printf ( "Please enter the fourth child's age and press -99 when done\n" );
scanf ( "%d", &age4 );
fflush(0);
 
total = age1 + age2 + age3 + age4 + number;
average = total / 4;
}
 
if (( age < 5 ) || ( age > 7 ))
{ 
printf ( "Error - Age is out of Range\n" );
}
 
while ( number != -99 ); 
{ 
printf ( "The total is %1.2f\n", total );
( "The average is %1.2f\n", average );
}
}

Thanks for looking :cheesy:

Recommended Answers

All 12 Replies

Did you copile this? Use [ CODE] [ /CODE] tags.

Actually, you can now use [code=c] :) Tags added for him.

In my limited experience I've not encountered

or

just opening and closing delimiters '{' and ' }' in C

Um you need a main func. Where did you declared the age variable?

do
{
   /* stuff */
} while(/*your code going here */);

This is a do while loop, not

do
{
  /* stuff */
}
if (/* something*/)
{
}
/* and then */
while();

Actually, you can now use [code=c] :) Tags added for him.

Ah nice didn't know that it exist.

I haven't included the main func on this forum as I find compilers are finicky creatures - some prefer main(0) or void main() whilst others prefer void(0) - the variations are seemingly endless!

I've declared the ages as integers as they are whole numbers.

I haven't included the main func on this forum as I find compilers are finicky creatures - some prefer main(0) or void main() whilst others prefer void(0) - the variations are seemingly endless!

I've declared the ages as integers as they are whole numbers.

WHAT!?!? Okay use this

int main()
{
   /* your code going here */
   return 0;
}

Ah nice didn't know that it exist

It was just added yesterday, to do [code=language] where language is anything within our code snippet library.

It was just added yesterday, to do where language is anything within our code snippet library.[/QUOTE] Ok while U're here just to report that the posts order are different for internet explorer and for firefox. EDIT: is that a bug?[code=language] where language is anything within our code snippet library.

Ok while U're here just to report that the posts order are different for internet explorer and for firefox.
EDIT: is that a bug?

Hmm yeah this thing has even happened to me quite a number of times..

Which browser you use Mr. Andor, i use Firefox ?

Hmm yeah this thing has even happened to me quite a number of times..

Which browser you use Mr. Andor, i use Firefox ?

Sometimes this can happen after using the Quick Reply box, since it uses AJAX to add your post to the bottom of the thread list without refreshing the entire page. A page refresh should normally fix this.

the age restriction applies to each sprog, so the check needs to be placed in appropriate relation to the age variable.

do
  printf  enter an age between 5 and 7
  scanf age
while age < 5 or age > 7
age1 = age

repeat for age2, age3 and age4 or, if you know about arrays, use an array of 4 ints to represent the 4 sprogs instead of 4 separate ints and then use a loop to collect all 4 or enter -99 to stop anytime before the 4th sprog. Using the array version you can determine how many sprog values are actually entered so you can calculate an average over any number of sprogs up to a total of 4.

//declare variables
int age
int sprogs[4]
int index = 0
int flag = 0

//don't add more than 4 values to sprogs
while index less than  4 and flag not equal to -99

  printf enter -99 to stop loop and any other number to continue
  scanf flag

  if flag not equal to -99
    //get a sprog age
   do
     printf  enter an age between 5 and 7
     scanf age
   while age < 5 or age > 7

   assign age to appropriate index of sprogs
 
calculate average
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.