why its jumping out of program?

I just start learning C++.

When I execute my code it's jumping out of the program without 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();

}

Answers


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.

}

Need Your Help

How do I unpack various form of integers in a byte buffer in golang?

buffer go unpack

I need to extract various fields in a byte buffer. I came up with this solution:

C# Random codes - Is most of it simply wrong?

c# windows-phone-7 random

I had a lot of issues with randomizing lists. I am talking about a list of 200 elements, where I want to shuffle the list. Don't get me wrong, I read a lot of examples, and on first glance there are

NSInvocation for Dummies?

objective-c cocoa undo-redo

How exactly does NSInvocation work? Is there a good introduction?