#Loop

19 messages · Page 1 of 1 (latest)

real waspBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

distant osprey
#

You need to read a number before you check its value.

#

Also you need to initialize sum with 0.

quartz pollen
distant osprey
#

float num; leaves num uninitialized. Reading from it is actually not allowed and does chaotic stuff.

#

You need to put a value into in first.

quartz pollen
#

okaaaay it works thank you bro ❤️
but if i want to add the average with the sum how caan i do it

real waspBOT
#

@quartz pollen Has your question been resolved? If so, run !solved :)

quartz pollen
#

!solved

real waspBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

#

@quartz pollen

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

distant osprey
quartz pollen
#
int main()
{
float num=1 ,sum=0,count=0,avg;
while(num!=0)
{
    cout<<"enter num\n";
    cin>>num;
    sum+=num;
    count++;
}
avg=sum/count;

cout<<"sum="<<sum<<"\n";
cout<<"avg="<<avg;

like that ?

distant osprey
#

Looks right

#

You don't need avg, you can do cout<<"avg="<< sum/count; directly.

#

Your code doesn't handle incorrect input, if someone presses just enter without entering a number or entering "Lol" your program will endlessly print "enter num". This might not matter if it's for an assignment that tells you not to care about that.

quartz pollen
#

okay bro thank you ❤️

real waspBOT
#

@quartz pollen Has your question been resolved? If so, run !solved :)

quartz pollen
#

!solved