Thread: Whats wrong with my code?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    6

    Talking Whats wrong with my code?

    Hey, first off, this is my first time here, hopefully I will be able to give back to the community as my knowledge and programming skills are increased.

    I started out in VB6 (stop laughing!) in my high school, and was gunna take AP Java, but it was canceled for the year when only 11 people signed up. Anyway, since then I have been (trying) to teach myself c++, and have just started getting a little more serious about it.

    Anyway, I was wondering if someone could tell me what is wrong with this code:

    Code:
    int sum = 0, value;
        //std::cout << "Please enter the numbers to be added" << std::endl;
        //read till end of file, calculating a running total of all values read
        while (std::cin >> value)
              sum += value; 
        std::cout << "Sum is: " << sum << std::endl;
        return 0;
    I believe its triggering an infinite wgile loop, but I dont know why. Any information/tips would be appreciated. Thanks!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    while (std::cin >> value) // While cin hasn't failed, loop
    There is nothing here that would make you exit the loop unless you explicitly failed the stream. You'll have to have some condition to make it exit the loop, it's up to you what that should be.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    Wouldnt it fail at the end of the file? It ends (and gives me the sum) when I press ctrl +z (the manual way to tell it end of file) But it seems as though that event isnt triggering.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Where is the end of file in a cin statement unless you manually input it?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    when it reaches the last value?

    24 5 6 9 = cin

    then after 9 would be end of file right?

    Maybe I am wrong, I am going off of a book (but its got good reviews... c++ primer)

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    After 9 you might stop typing stuff, but it is still waiting for user input.
    Your while loop gets user input, runs it and waits for user input again, goes again and waits again. Just because you stop typing in does not mean it stops waiting for user input. In keeps waiting and waiting until it gets input or you forcefully stop the program.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    So is there any way to have it stop when it gets to the end of That input?

  8. #8
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Many ways.
    If you want the user to enter 4 things exactly, every run through the loop increment a counter and do it until that counter gets to four.

    Code:
    #include <iostream>
    
    
    int main()
    {
    
    	int counter = 1;
    	int sum = 0;
    	int value = 0;
    
    while (counter < 5)
    {
    	std::cin >> value;
    	sum += value; 
    	counter++;
    }
    
    
        std::cout << "Sum is: " << sum << std::endl;
    
    
    	return 0;
    
    }
    Or how about go until the user enters 0?
    Code:
    #include <iostream>
    
    
    int main()
    {
    
    	int counter = 1;
    	int sum = 0;
    	int value = 1; //Make sure it does not start at 0, but it can be 0 with a do-while loop
    
    while (value !=0)
    {
    	std::cin >> value;
    	sum += value; 
    
    }
    
    
        std::cout << "Sum is: " << sum << std::endl;
    
    
    	return 0;
    
    }
    Or you can do when it is anything less than 0, if it is exactly 99999 stop. The possibilities are endless.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    alright, Ill just have a -1 be the value (seeing as its made for adding any way)

    Thanks for the help

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    By default, operator>> treats a space and a newline the same. So when you hit enter sending your input to the function, it is adding a newline to the stream. That has basically the same effect as if you had put a space in the stream. That is why the loop isn't broken when the user hits enter.

    If you want to get only the values input before the user hits enter, you would want to use getline to read the line into a string, then put that into a stringstream and replace cin in your current code with the stringstream variable. That is probably a bit complicated at the moment for you, so I'd suggest using the -1 idea.

    You could also have the user type in a letter, which will make the input fail and break the loop. That will work well for your simple program, but the problem is that if you want to use cin again later in the program, you have to remember to clear the fail state and ignore the bad character(s).

    BTW, Enahs' example code has a minor problem if you try to use it with -1. It fails to check th einput before adding it to the sum, so if the user enters the sentinel (-1 in this case), that value will be added to the sum when it isn't meant to be. It only doesn't matter if the sentinel is 0 because it won't change the sum any.

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by thewierdone2000
    Wouldnt it fail at the end of the file? It ends (and gives me the sum) when I press ctrl +z (the manual way to tell it end of file) But it seems as though that event isnt triggering.
    There is a bug in MS Windows that prevents detection of the end-of-file character correctly. Try pressing Ctrl+Z and then ENTER. That's usually what I have to do. Here's a link to Bjarne Stroustrup's example for working around this bug:

    http://www.research.att.com/~bs/bs_f...simple-program

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    ah! Thanks!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM