What am I doing wrong?

This is a discussion on What am I doing wrong? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp Code: if (count ==1) count = 1; else if (count == 2) count = 2 ; ...

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by matsp View Post
    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

    Tried to get rid of it but the program would just stop and not continue... so it works the way it is, thats the way Im going to leave it for now. LOL

    This is how the professor requested it. There is 8 parts to the program and he wants it laid out in the above fashion. It's only an introductory course to C programming. I'm enjoying it alot except for the fact that I make stupid mistakes all the time. I will try to fix this puppy up when I get home this evening and see what happens. <sigh>

  2. #17
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    This code
    Code:
    inFile >> num;
    
    	while (inFile && under)
    	{	
    		if (sum > 1000)
    			under = false;
    		
    		else 
    			sum = sum + num;
    			
    	}
    Should be:
    Code:
    while(infile >> num)
    {
         if(sum > 1000)
         {
              under = false;
              break;
         }
         sum += num;
    }
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #18
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Thumbs down

    Quote Originally Posted by Neo1 View Post
    This code
    Code:
    inFile >> num;
    
    	while (inFile && under)
    	{	
    		if (sum > 1000)
    			under = false;
    		
    		else 
    			sum = sum + num;
    			
    	}
    Should be:
    Code:
    while(infile >> num)
    {
         if(sum > 1000)
         {
              under = false;
              break;
         }
         sum += num;
    }
    Not allowed to use breaks.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Why not? It's essentially the same code, only one variable less.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by dispatch4599 View Post
    I am soooo confused... Ok, Im going to post the whole program..... maybe this will clear things up..
    "Clear things up"?!?! It's been clear for us since your first post.

    The only thing posting your code again makes clear is that you don't listen to a word anyone says. You've been told half a dozen times what you need to do to fix the bugs, and yet still no change to your code. You may as well be a brick wall.

    Not allowed to use break huh - What moron is teaching you?
    Fine, I forbid you to use if-statements either!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Angry

    Quote Originally Posted by iMalc View Post
    "Clear things up"?!?! It's been clear for us since your first post.

    The only thing posting your code again makes clear is that you don't listen to a word anyone says. You've been told half a dozen times what you need to do to fix the bugs, and yet still no change to your code. You may as well be a brick wall.

    Not allowed to use break huh - What moron is teaching you?
    Fine, I forbid you to use if-statements either!

    DUDE relax. I can't change it when I'm at work. When I get home this evening it will be done.
    I also apologize that my professor has rules for us. I'm not a brick wall, I do listen and each time I have asked for help on this board I have made the necessary changes to my code to make the program run. There is no need to jump on me because I am unable to do so today. I apologize for taking your time as well. To those with the suggestions, thank you and I will try them out when I get home.

  7. #22
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by Elysia View Post
    Why not? It's essentially the same code, only one variable less.
    Probably because it's not been taught yet to use it, although I don't know for sure. Vart helped me the other night and he saw the <break> in the program and said that I needed to do it again. Cannot use break whatsoever.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,775
    You can incorporate it in your loop, e.g.,
    Code:
    while (sum <= 1000 && (inFile >> num))
    That way you do not need the break or the flag.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by laserlight View Post
    You can incorporate it in your loop, e.g.,
    Code:
    while (sum <= 1000 && (inFile >> num))
    That way you do not need the break or the flag.
    LOL. I just looked at the assignment sheet and it says to use a flag controlled loop structure.. So much for that... lol. I can't wait till we get more freedom with our coding... <sigh>

  10. #25
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Ok here is what worked and it is in the professor guidelines..

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

  11. #26
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Depending on the values in the file, what you've got may end up adding the last value read from the file to the sum over and over. For example, if your file only had the values 500 and 200, then instead of a sum of 700 (500 + 200) being printed, you'd have 900 (500 + 200 + 200).
    "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

  12. #27
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Talking

    Quote Originally Posted by hk_mp5kpdw View Post
    Depending on the values in the file, what you've got may end up adding the last value read from the file to the sum over and over. For example, if your file only had the values 500 and 200, then instead of a sum of 700 (500 + 200) being printed, you'd have 900 (500 + 200 + 200).
    When I checked my output it equaled the amount I got from using the calculator. Hell, at this point, I'm just happy to have it do that..

  13. #28
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by dispatch4599 View Post
    When I checked my output it equaled the amount I got from using the calculator. Hell, at this point, I'm just happy to have it do that..
    Make file containing only one value - 200 and see what is the output
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> When I checked my output it equaled the amount I got from using the calculator.

    But did you try that specific case? The point is that if you get to the end of the file and are still under 1000 then you'll have problems.

  15. #30
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by dispatch4599 View Post
    Code:
    sum = 0;
    under = true;
    
    
    		while (under)
    		{
    			inFile >> num;
    			
    				if ((num + sum) > 1000)
    				    under = false;
    
    				else 
    				    sum = num + sum;					
    			
    		}
    
            outFile << "The sum of integers which does not exceed 1000 is " << sum << "."   <<endl;
    			
    
    	inFile.close();
    	inFile.clear();
    return 0;
    Code:
    sum = 0;
    under = true;
    
    
    		while (under && inFile >> num)
    		{
    			
    				if ((num + sum) > 1000)
    				    under = false;
    
    				else 
    				    sum = num + sum;					
    			
    		}
    
            outFile << "The sum of integers which does not exceed 1000 is " << sum << "."   <<endl;
    			
    
    	inFile.close();
    	inFile.clear();
    return 0;
    This should help, the loop will now stop when the end of the file has been reached, and it'll also stop if under == false...
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Page 2 of 3 FirstFirst 123 LastLast
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, 09: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, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21