CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Oct 2012
    Posts
    14

    College Lesson Trouble With Count/Sentinel Control Loops

    Hi,

    This is my first post here and I am in need with some help(might need a lot) with a lesson my professor has given me to help me understand loops.

    Here is an imgur link to my current homework. http://i.imgur.com/9Mcja.jpg

    As you can see on there I have them listed on which ones are supposed to be sentinel and which ones are supposed to be count control loops.

    This is currently what I have:

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	int count = 0;
    	int sum = 0;
    	int num;
    
    	ofstream fileout;
    	ifstream filein;
    
    
    	filein.open("C:\\Users\\commiedic\\Desktop\\New folder\\datafile2.txt");
    	fileout.open("C:\\Users\\commiedic\\Desktop\\New folder\\answers.txt");
    
    	sum=0;
    	filein>>num;
    	filein.get(inchar);
    	while(filein)
    	{ sum=sum+num;}
    
    
    }
    This class has me stumped currently and I am having a hard time breezing through the class at my instructors speed. So I don't want to get too far behind.

    Right now this program is giving me an error that opens CMD and gives me infinite lines of "0's".

  2. #2
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    can't find the edit button, but I meant to say this is what I have so far. I am stuck on (A) trying to write to the ANSWER.txt file.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  4. #4
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Sorry it has been so long for me to reply. rough week...

    Here is my updated code

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	int count = 0;
    	int sum = 0;
    	int num;
    
    	ofstream fileout;
    	ifstream filein;
    
    
    	filein.open("E:\\*****\\datafile2.txt");
    	fileout.open("E:\\*****\\answers.txt");
    
    	sum=0;
    	filein>>num;
    	while(filein)
    	{ 
    
    		filein>>num;
    		fileout<<num;
    		
    	}
    
    
    }
    So this will write to my answers.txt file, but for some reason it skips the first integer "55" and starts at the second integer "67"

    What the DataFile2 looks like:
    Code:
    55 67 458 23 81 33
    782 375 528
    405 324 950 46
    14 864 551 38 167 518 630
    What my answers.txt file looks like:
    Code:
    67458238133782375528405324950461486455138167518630630
    Last edited by commiedic; October 30th, 2012 at 11:46 AM.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Of course it does. Just look at your code.
    Code:
    	filein>>num;
    	while(filein)
    	{ 
    
    		filein>>num;
    		fileout<<num;
    		
    	}
    You should reverse the order of your statements in the while loop.

  6. #6
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by GCDEF View Post
    You should reverse the order of your statements in the while loop.
    Awesome thanks! For some reason I am also having trouble keeping the spaces and end line codes.

    I got it with:

    fileout<<num<<" ";
    Last edited by commiedic; October 29th, 2012 at 12:11 PM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by commiedic View Post
    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.

  8. #8
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Ok I see what you are saying. Here is my new updated code. I will be moving on to the averages now.
    Last edited by commiedic; October 30th, 2012 at 12:01 PM.

  9. #9
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Ok, for the averages of the numbers here is what I am getting in my output file.

    Code:
    55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630 630
    For some reason it isn't giving me A) the average and B) it is repeating the last number twice.

    Here is my code including the averages:

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	int num, fnum, lnum, sum;
    	float average, total, counter;
    	char inchar;
    	ofstream fileout;
    	ifstream filein;
    
    
    	filein.open("E:\\*****\\datafile2.txt");
    	fileout.open("E:\\*****\\answers.txt");
    
    	while(filein)
    	{	
    		filein>>num;
    		fileout<<num<<" "; 
    		
    
    	}
    
    	total=0;
    	counter=0;
    
    	while(filein)
    	{
    		filein>>num;
    		total=total+num;
    		counter++;
    		average = total/counter;
    		fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
    	}
    		
    }

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

    You didn't do what I told you to regarding the order of statements.
    Last edited by GCDEF; October 30th, 2012 at 12:20 PM.

  11. #11
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by GCDEF View Post
    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.

    So with this new build I am getting the average after every number:
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	int num, fnum, lnum, sum;
    	float average, total, counter;
    	char inchar;
    	ofstream fileout;
    	ifstream filein;
    
    
    	filein.open("E:\\*****\\datafile2.txt");
    	fileout.open("E:\\*****\\answers.txt");
    
    
    	total=0;
    	counter=0;
    	while(filein)
    	{	
    		fileout<<num<<" "<<endl; 
    		total=total+num;
    		counter++;
    		average = total/counter;
    		fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
    		
    
    	}
    
    		
    }
    Output is:
    Code:
    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 ___

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  13. #13
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by GCDEF View Post
    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.

  14. #14
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    This is the only way I could get it to work. Although it is still repeating the last number twice, but it is giving me the average.

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	int num, fnum, lnum, sum;
    	float average, total, counter;
    	char inchar;
    	ofstream fileout;
    	ifstream filein;
    
    
    	filein.open("E:\\Resource Files\\datafile2.txt");
    	fileout.open("E:\\Resource Files\\answers.txt");
    
    
    	total=0;
    	counter=0;
    	while(filein)
    	{	
    		filein>>num;
    		fileout<<num<<" "; 
    		total=total+num;
    		counter++;
    	}
    
    	average = total/counter;
    	fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
    
    		
    }
    Code:
    55 
    67 
    458 
    23 
    81 
    33 
    782 
    375 
    528 
    405 
    324 
    950 
    46 
    14 
    864 
    551 
    38 
    167 
    518 
    630 
    630 
    
    The Average from the input file is: 359.000
    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.

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured