开发者

why its jumping out of program?

I just start learning C++.

When I execute my code it's jumping out of the program witho开发者_如何学Cut any error. Why?

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

  char s1[20],s2[10];
  cout<<" enter a number : ";
  cin.get(s1,19);
  cout<<" enter a number : ";
  cin.get(s2,9);

  cout<<s1<<"/n"<<s2;

  getch();

}


The method get() reads upto the '\n' character but does not extract it.

So if you type: 122345<enter>
This line:

cin.get(s1,19);

Will read 12345, but the '\n' (created by hitting <enter>) is left on the input stream. Thus the next line to read:

cin.get(s2,9);

Will read nothing as it sees the '\n' and stops. But it does not extract the '\n' either. So the input stream still has the '\n' there. So this line:

getch();

Just reads the '\n' character from the input stream. Which then allows it to finish processing and exit the program normally.

OK. That is what is happening. But there is more to this. You should not be using get() to read formatted input. Use the operator >> to read formatted data into the correct type.

int main()
{
    int   x;
    std::cin >> x; // Reads a number into x
                   // error if the input stream does not contain a number.
}

Because the std::cin is a buffered stream the data is not sent to the program until you push <enter> and the stream is flushed. Thus it is often useful to read the text (from user input) a line at a time then parse that line independently. This allows you to check the last user input for errors (on a line by line bases and reject it if there are errors).

int main()
{
    bool inputGood = false;
    do
    {
        std::string  line;
        std::getline(std::cin, line);   // Read a user line (throws away the '\n')

        std::stringstream data(line);
        int  x;
        data >> x;                   // Reads an integer from your line.
                                     // If the input is not a number then data is set
                                     // into error mode (note the std::cin as in example
                                     // one above).
        inputGood = data.good();
    }
    while(!inputGood);   // Force user to do input again if there was an error.

}

If you want to get advanced then you can also look at the boost libs. They provide some nice code in general and as a C++ program you should know the contents of boost. But we can re-write the above as:

int main()
{
    bool inputGood = false;
    do
    {
        try
        {
           std::string  line;
           std::getline(std::cin, line);   // Read a user line (throws away the '\n')

           int  x    = boost::lexical_cast<int>(line);
           inputGood = true;               // If we get here then lexical_cast worked.
        }
        catch(...) { /* Throw away the lexical_cast exception. Thus forcing a loop */ }
    }
    while(!inputGood);   // Force user to do input again if there was an error.

}


You need to use cin.ignore(); to ignore the newline character from stream before getting next input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜