Accelerated C++ chapter 3 help needed.

Started by
6 comments, last by steffy81 11 years ago

Hello, I am working out of the Accelerated C++ book. I have been learning C++ for a couple years now (teaching myself as I have time). I have run into a small issue in chapter 3 of this book and am not sure what to do about it. I have tried to run the example programs from the chapter and they will not run. I believe the problem is at the section:

while(cin >> x)

{

++count;

sum += x;

}

I have commented out code from here on, and the program runs fine. When I add this code in, the program allows me to enter the data, but then immediately exits without finishing. The output window in VSC++ 2010 says the exited with code 0.

I was wondering what would be causing the program to exit without finishing? Anyone have any ideas or suggestions of how to get this to run completely?

Here is the full code I was using. (I deleted the code because I thought I might be infringing on the copyright). I added the last bit before return 0 to keep the console window open until I saw the finished program.

Edit to issue: I have continued to work with the program. I have not figured out what I am doing wrong, but I can redefine what is happening. It seems the program is continuing to run, but I can no longer use cin. I have tried placing cin.clear() after my while loop as amrazek mentioned, and it has not corrected the problem. I will continue to look into the issue.

Advertisement

It's a bit subtle. http://www.parashift.com/c++-faq/istream-and-while.html

In a nutshell: your while loop continues until the user puts the stream into an error state, after which all attempts to extract data from it will fail until the error state is cleared. After the loop, reset the stream's state with istream's clear() function (cin.clear() -- as cin is just an istream under the hood)

As an aside, you can still break things in the same way by providing invalid input for the first two cins. For instance, typing anything that can't be cast to a double for the midterm or final grades will also put the stream into an error state and skip past the loop and your final cin. It might be wise to check the stream's state after those to see if the user tried to input something invalid.

revising...

The output window in VSC++ 2010 says the exited with code 0.

You're using Microsoft Visual C++ 2010...

I added the last bit before return 0 to keep the console window open until I saw the finished program.

...so there's absolutely no need for you to do that -- simply use Ctrl+F5:

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787/

http://stackoverflow.com/questions/2639891/visual-studio-2010-exiting-after-program-ends

...so there's absolutely no need for you to do that -- simply use Ctrl+F5:

Matt-D I tried this and it did not seem to work.

I did however find another nifty little bit of code as I was looking around the internet, that will get me the same result. This will keep my window open since I am not able to use cin again after placing it in the while loop mentioned above. Figured I would share it.


void keep_window_open()
{
    // clear buffer
    fflush(stdin);
    printf("Please enter a character to exit\n");
    char ch;
    ch = getchar();
    return;
}

One last update for the night on my progress. I placed:

cin.clear();

fflush(stdin);

after the while(cin >> x) loop and I could use cin again. I had to use both as one or the other would not work alone.

However, I am reading in many places that using the fflush(stdin) is a bad idea. So I guess this is just a temporary fix and not a good one at that.

That's because whatever invalid input cin tried to convert into x's type is still in the buffer. What's wrong with simply ignoring the remainder of the invalid input with cin.ignore()?

I wanted to thank you guys for the help. It is much appreciated.

This topic is closed to new replies.

Advertisement