Thread: What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    What am I doing wrong?

    Here is just the while loop. The object is to add the integers in the file until the sum < = 1000.

    Code:
    sum = 0;
    under = true;
    
    	while (inFile && under)
    
    	{	
    		if (sum > 1000)
    			under = false;
    		
    		else 
    			sum = sum + num;
    	}
    	
    	outFile << "The sum of integers which does not exceed 1000 is " << sum << "." <<endl;
    			
    
    	inFile.close();
    	inFile.clear();

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You never actually read anything from inFile.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by brewbuck View Post
    You never actually read anything from inFile.
    Code:
    inFile.open ("K:\\DATFILE1.TXT");
    	
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	
    sum = 0;
    under = true;
    inFile >> num;
    
    	while (inFile && under)
    
    	{	
    		if (sum > 1000)
    			under = false;
    		
    		else 
    			sum = sum + num;
    			
    	}
    	
    	outFile << "The sum of integers which does not exceed 1000 is " << sum << "." <<endl;
    			
    
    	inFile.close();
    	inFile.clear();
    
    
    return 0;
    }
    My result is still over 1000.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're adding the same number to sum over and over again. You probably want something like this:
    Code:
    while(cin >> num) {
        sum += num;
        if(sum > 1000) {
            // ...
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Unhappy

    Quote Originally Posted by dwks View Post
    You're adding the same number to sum over and over again. You probably want something like this:
    Code:
    while(cin >> num) {
        sum += num;
        if(sum > 1000) {
            // ...
        }
    }
    Actually, I'm not adding the same number to sum over and over. It pulls in the next number from the file. I know that the answer is going to be something I could kick myself for as that has been the case with the prior 4 parts of the program. I just can't figure it out.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It pulls in the next number from the file.
    Umm . . . are you sure? Why don't you try code similar to what I used and see what happens?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by dwks View Post
    Umm . . . are you sure? Why don't you try code similar to what I used and see what happens?
    I'm absolutely sure. This is part 7 of the program with me using the infile for all of the solutions.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by dispatch4599 View Post
    I'm absolutely sure.
    Then you need to pause working on this project and re-read how file processing works in C++, because you're absolutely wrong.

    Confidence is good, but it helps to be right.

    Edit: To illustrate my point:

    Code:
    	while (inFile && under)
    
    	{	
    		if (sum > 1000)
    			under = false;
    		
    		else 
    			sum = sum + num;
    			
    	}
    Show us where you're reading from the file.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In other words: code like
    Code:
    while(inFile)
    checks to see if the file stream is in an error state, and continues executing if it isn't. (If it's "good".) Only with inFile>>num, or inFile.get(), or some such, do you actually read data from the file.

    At the risk of repeating myself, you should probably try something like this.
    Code:
    while(cin >> num) {
        sum += num;
        if(sum > 1000) {
            // ...
        }
    }
    The expression in the while loop does two things at once: it reads a number into the variable num, and then it checks to see if the stream is good. If it is, it continues. In effect, you get all of the numbers in the file one by one, just waiting to be processed.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Irrespective of the fact that based on the code you've provided you are *NOT* reading a new number from the file on each loop iteration...
    Code:
    while (inFile >> num)
    {
       // If sum here is 999, and num is 2,
       // what's going to happen to sum?
       if (sum > 1000)
          under = false;
       else 
          sum = sum + num;
    }

  11. #11
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by rags_to_riches View Post
    Code:
    while (inFile >> num)
    {
       // If sum here is 999, and num is 2,
       // what's going to happen to sum?
       if (sum > 1000)
          under = false;
       else 
          sum = sum + num;
    }

    Err if sum is 999 before the IF, "sum > 1000" will validate to false, and it will execute the code in the else statement, making sum 1001. In the next iteration of the loop the if will validate to true and under will be set to false. The loop will keep running until all numbers in the file have been read. Was this not what you were expecting?

    Edit: In your previous code, it looks like you want the loop to break when under is false, you simply do this by adding a "break;" to your if statement, then the loop will exit as soon as sum validates to more than 1000. Remember to put brackets around the if.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    • While you can successfully read a value from the file...
      • Test if the sum plus the value from file is > 1000
        • If true, then exit loop without adding value to sum
        • If not true, then add value to the sum and keep going
    • Display the sum


    Your code in post #3 is basically:
    • Read a single value from the file
    • Keep adding that single value to sum until sum is more than 1000
    • Display the over 1000 sum
    Last edited by hk_mp5kpdw; 03-06-2008 at 08:26 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    I am soooo confused... Ok, Im going to post the whole program..... maybe this will clear things up..

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    int main()
    {
    
    //Declare and open files
    	ifstream inFile;
    	ofstream outFile;
    
    	float num;
    	float average;
    	float sum = 0;
    	int count = 0;
    	float highest = 0;
    	float lowest = 1000;
    	float x;
    	float closest = 200;
    	bool under;
    	char inChar;
    	
    	
    	outFile.open ("K:\\ANSWERS.TXT");
    	
    
    //*****************************************************************************************
    	inFile.open ("K:\\DATFILE1.TXT");
    	
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	outFile<<"The file contains the following data:  "<<endl;
    	inFile >> num;
    	while (inFile)
    	{
    		outFile << num <<" ";
    		inFile >> num;
    	}
    
    	inFile.close();
    	inFile.clear();
    
    //******************************************************************************************
    
    	inFile.open ("K:\\DATFILE1.TXT");
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	
    	inFile >> num;	
    	
    		while (inFile)
    		{
    			sum = sum + num;
    			count++;
    			inFile >> num;
    		}	
    
    	average = sum / count;
    
    	outFile << fixed << setprecision(3)
    			<<"The average of all the integers is "<< average << "."<<endl;
    	
    	inFile.close();
    	inFile.clear();
    //******************************************************************************************
    
    	inFile.open ("K:\\DATFILE1.TXT");
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	count = 0;
    	average = 0;
    	sum = 0;
    	
    	while ( count < 12)     
        {
    		inFile >> num;
    		sum = sum + num;    
    		count++;
    	   
    	}
    	average = sum / count;
    
    	outFile << fixed << setprecision(3)
    			<<"The average of the first 12 integers is "<< average << "."<<endl;
    
    	
    	inFile.close();
    	inFile.clear();
    //******************************************************************************************
    
    	inFile.open ("K:\\DATFILE1.TXT");
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	count = 0;
    	sum = 0;
    
    	inFile >> num;
    	
    
    	while (inFile)
    	{
    		count++;
    		if (count ==1)
    			count = 1;
    
    		else if (count == 2)
    			count = 2;	
    
    		else if (count == 3)
    			sum = sum + num;
    
    		else if (count > 3)
    			count = 1;
    		
    		inFile >> num;	
    	}
    		
    	outFile << fixed << setprecision(0)
    			<<"The sum of every third number is " << sum << "."<< endl;
    	
    	inFile.close();
    	inFile.clear();
    
    //******************************************************************************************
    
    	inFile.open ("K:\\DATFILE1.TXT");
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	
    	inFile >> num;
    	
    		while (inFile)
    		{
    			inFile >> num;
    
    			if (num < lowest)
    					lowest = num;
    			
    			if (num > highest)
    				highest = num;
    
    		}
    
    	outFile << "The range of numbers is from "<< lowest << " to " << highest << "." 
    			<<endl;	
    
    	inFile.close();
    	inFile.clear();
    
    //*****************************************************************************************
    	inFile.open ("K:\\DATFILE1.TXT");
    	
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	
    	inFile >> num;
    	
    		while (inFile)
    		{
    			inFile >> num;
    			x = abs(num - 200);
    
    			if (x < closest)
    					closest = num;			
    		}
    
    	outFile << "The closest number in the file to 200 is " << closest << "."<< endl;
    
    	inFile.close();
    	inFile.clear();
    //*****************************************************************************************
    
    //*****************************************************************************************
    inFile.open ("K:\\DATFILE1.TXT");
    	
    	if ( !inFile )
    	{
    		cout<< "**Can't open input file**" <<endl;
    		return 1;
    	}
    	
    sum = 0;
    under = true;
    inFile >> num;
    
    	while (inFile && under)
    	{	
    		if (sum > 1000)
    			under = false;
    		
    		else 
    			sum = sum + num;
    			
    	}
    	
    	outFile << "The sum of integers which does not exceed 1000 is " << sum << "." <<endl;
    			
    
    	inFile.close();
    	inFile.clear();
    
    
    return 0;

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    and yes I see that I forgot to read from file...

    I think that I need to take a break from this stuff at night... ughhhhhh

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		if (count ==1)
    			count = 1;
    
    		else if (count == 2)
    			count = 2;	
    
    		else if (count == 3)
    			sum = sum + num;
    
    		else if (count > 3)
    			count = 1;
    The marked sections don't do anything useful. Perhaps you can do away with the whole if-statement for count == 1 and count == 2?

    And yes, your code does not read the number in for your last section of code. Compare it to your other funcitonality.

    Overall, there is quite a bit of repetition in this code. It could be written much shorter by some use of functions. And you don't really need to close/re-open the input file, you could just set the current input to the beginning (using "seekg(0)").

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM