hello every body

i have a problem with a c program .
the question that ( write a c program that reads a real numbers untill error then calculate the sum and the average of all numbers )

i have solve it with a while looping . and i need it with adiffrent looping .

#include<stdio.h>
 
void main()
{
 float total,average;
 int s;
 float a;
 
 while((scanf("%f",&a)>0))
 {
  total=total+a;
  s=s+1;
 }
 average =total/s;
 printf("The sum of numbers are %f and the average is %f",total,average);
}

I don't get it, what's your problem?

Also, don't use void main, replace it with int main.

the problem that i need to use this program in another way
for example : for looping , do-while looping

the problem that i need to use this program in another way
for example : for looping , do-while looping

So what's the problem? Doesn't your book describe a for loop and a do-while loop?

You need to try it, we aren't supposed to do it for you.

thaaaaaanx WaltP ;

i am still trying

#include<stdio.h>
#include<conio.h>
 
int main()
{
 float total,average;
 int s;
 float a;
 clrscr();
do
{
total=total+a;
  s=s+1;
}
 while((scanf("%f",&a)>0));
 average =total/s;
 printf("The sum of numbers are %f and the average is %f",total,average);
}

Don't use conio ( #include<conio.h> ), it's unportable!
To the OP: Are you encountering any problems with your current code? (as you only posted your new code)

i need another solution for for looping

i need another solution for for looping

Changing it to a for loop is not that difficult, look up how a for loop works, or take a look at this small example:

int i; // declare this variable before any statement in your code
for(i = 0; i < 5; i++)
{
  /* The code in this code block will run five times */
}

And because you're so kind I've also Googled up a tutorial about Control structures (if, while, do-while, for): http://www.cplusplus.com/doc/tutorial/control/ :)

Member Avatar for iamthwee

>for(int i = 0; i < 5; i++)

That code where 'i' is declared within the parenthesis
...
'for' loop initial declaration used outside C99 mode JFYI

>for(int i = 0; i < 5; i++)

That code where 'i' is declared with the parenthesis
...
'for' loop initial declaration used outside C99 mode JFYI

I didn't notice I was in the C forum, but according to C99 this was valid, however I edited my post to conform to the C89 standard :)

C89:

int i; // declare this variable before any statement in your code
for(i = 0; i < 5; i++)
{
  /* The code in this code block will run five times */
}

C99:

for(int i = 0; i < 5; i++)
{
  /* The code in this code block will run five times */
}
Member Avatar for iamthwee

If you want to write portable code (works on any system) it's best to use the c89 switch (and also turn up the warning level (-Wall).

commented: yes +11

there are a mistakes i dont know how to solve it .. i am trying :(

#include<stdio.h>
#include<conio.h>
 
void main()
{
 float total,average, s;
 int i;
 int a;
 clrscr();
  printf("Enter the numbers : ");
 scanf("%d",&a);
 for ( i=0;i<a ; i++)
 {
  total=total+a;
  s=s+1;
  }
   average =total/s;
 printf("The sum of numbers are %f and the average is %f",total,average);
}
commented: Read previous posts. -2
commented: will this turn you to green? +21

> void main()
Mentioned before, so I'll mention it again.
main returns an int, not void

> total=total+a;
Is the initial value of total zero?

commented: Yes, that was also getting on my nerves :) +10

there is more :(

You mean "more" like the scanf which was inside the loop, and now isn't.

bahr_alhalak,

This is your 43rd post and I think you have been quite familiar with
the standard c/c++. Its pity. You are not serious and ignoring the community. Senior members are always right with their valuable comments & suggestions. Please be gentle.

i admire Senior members and their comments & suggestions.

i am just trying to discuse with them .
i want answer for my problem

commented: Begging won't help! -2
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.