If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: College Lesson Trouble With Count/Sentinel Control Loops

You have an input statement after you open your file, but you don't have one in the loop. All it does is keep adding num to sum over and over again. You need to add an input statement to your loop so that it actually reads a new line of the file each time it loops.

Re: College Lesson Trouble With Count/Sentinel Control Loops

Originally Posted by commiedic

Awesome thanks! For some reason I am also having trouble keeping the spaces and end line codes.

I got it with:

fileout<<num<<" ";

I'm not sure you got my point. You read from the file twice before you output, so the first input was thrown away. By swapping the lines inside your while statement, you output the first input, and you terminated the loop when the file reached the end, without trying to output the failed input.

Re: College Lesson Trouble With Count/Sentinel Control Loops

Why do you have two loops?
At the end of the first loop, you're at the end of the file, so the second one won't execute.
You don't compute the average in a while loop. To compute an average, you sum the numbers and divide the sum by the count of the numbers.

Re: College Lesson Trouble With Count/Sentinel Control Loops

Originally Posted by GCDEF

Why do you have two loops?
At the end of the first loop, you're at the end of the file, so the second one won't execute.
You don't compute the average in a while loop. To compute an average, you sum the numbers and divide the sum by the count of the numbers.

You didn't do what I told you to regarding the order of statements.

Ok, then I will try to build it into one loop. Hopefully I got what you were saying this time. I think I understand that I was being redundant.

55
The Average from the input file is: 55.000
67
The Average from the input file is: 61.000
458
The Average from the input file is: 193.333
23
The Average from the input file is: 150.750
81
The Average from the input file is: 136.800
33
The Average from the input file is: 119.500
782
The Average from the input file is: 214.143
375
The Average from the input file is: 234.250
528
The Average from the input file is: 266.889
405
The Average from the input file is: 280.700
324
The Average from the input file is: 284.636
950
The Average from the input file is: 340.083
46
The Average from the input file is: 317.462
14
The Average from the input file is: 295.786
864
The Average from the input file is: 333.667
551
The Average from the input file is: 347.250
38
The Average from the input file is: 329.059
167
The Average from the input file is: 320.056
518
The Average from the input file is: 330.474
630
The Average from the input file is: 345.450
630
The Average from the input file is: 359.000

I would like for it to read one line with all the numbers and then one line saying the average of all the numbers is ___

Re: College Lesson Trouble With Count/Sentinel Control Loops

Did you see where I said "You don't compute the average in a while loop. To compute an average, you sum the numbers and divide the sum by the count of the numbers."?

You need to think a little bit about what you're doing. You get an average by getting the sum over the numbers, then dividing by the count of the numbers. That's not what you're doing. You need to get the sum and the count in the loop, but the average after the loop.

Re: College Lesson Trouble With Count/Sentinel Control Loops

Originally Posted by GCDEF

Did you see where I said "You don't compute the average in a while loop. To compute an average, you sum the numbers and divide the sum by the count of the numbers."?

You need to think a little bit about what you're doing. You get an average by getting the sum over the numbers, then dividing by the count of the numbers. That's not what you're doing. You need to get the sum and the count in the loop, but the average after the loop.

Ok I think I see what you are saying now. I will revise my code after my next class. Thank you for being so helpful.

Now is there anyway I can get the third step of this assignment in the same program? I am having an issue figuring out how to get the average of the first twelve without having a separate program to do it.

Re: College Lesson Trouble With Count/Sentinel Control Loops

I hadn't read the original assignment about performing the tasks separately, but he shows you what you need to do. You will need multiple loops closing and reopening the file between them.

I've answered your question about repeating the last number several times now. You were on the right track earlier when you read the file before your loop, but you need to put the output before the input in the loop.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.