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.
19 messages · Page 1 of 1 (latest)
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.
You need to read a number before you check its value.
Also you need to initialize sum with 0.
more explain plz
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.
okaaaay it works thank you bro ❤️
but if i want to add the average with the sum how caan i do it
@quartz pollen Has your question been resolved? If so, run !solved :)
!solved
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 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.
Divide by the number of numbers you added. You'll need to add a separate variable for the counter that counts 1 up every time a variable is added and divide the sum by the counter at the end.
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 ?
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.
okay bro thank you ❤️
@quartz pollen Has your question been resolved? If so, run !solved :)
!solved