• 0

Abnormal Termination Error in C++


Question

Every time I try to run this I get an abnormal termination error. Why? heh... I'm still new. This is assignment 2 in the intro to C++ class that assumes no programming knowledge, but only teaches you syntax (thankfully I'm also taking Intro to VB, where the teacher insist on teaching us how to program before we even touch VB).

#include <iostream> // for cin, cout

#include <string>

using namespace std;

int main()

{

string gregorian;

cout << "Enter a date as dd mm yyyy: ";

cin >> gregorian;

cout << endl << endl;

cout << gregorian.substr( 0, 2 ) <<endl;

cout << gregorian.substr( 4, 6 ) <<endl;

cout << gregorian.substr( 7, 10 ) <<endl;

return 0;

}

Thanks,

~D

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

string.h is a C header file. Try this:

#include &lt;cstring&gt;
#include &lt;iostream&gt;

using namespace std;

int main(void)
{
    string gregorian;

    cout &lt;&lt; "Enter a date as dd mm yyyy: ";
    cin &gt;&gt; gregorian;
    cout &lt;&lt; endl &lt;&lt; endl;
    cout &lt;&lt; gregorian.substr( 0, 2 ) &lt;&lt; endl;
    cout &lt;&lt; gregorian.substr( 4, 6 ) &lt;&lt; endl;
    cout &lt;&lt; gregorian.substr( 7, 10 ) &lt;&lt; endl;

    return 0;
}

Link to comment
Share on other sites

  • 0

When your cin is streaming to gregorian, it's going to only get dd. It returns at the first sign of whitespace. Use getline function or the getline method of cin to get the entire line.

substr is defined in <string> as they are methods of the string class. No need to use <cstring>.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.