cmd prompt disappears

I'm using Dev-C++ on XP. The last three programs went fine, but now the cmd prompt disappears before I can read the output. I'm using system("PAUSE"); but it no longer pauses after I input the ISBN number.

Actually, if I input a ridiculously long ISBN number it will pause.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
using namespace std;

class asst3
{
   public:
      bool chkisbn(string);
};

bool chkisbn( string isbnStr)
{
   int lastInt, sum;
   bool isISBN = false;
   
   for( int i = 0; i < isbnStr.length(); i++ )
   {
      if( !isdigit(isbnStr[i]) )
      {
         isbnStr.erase(isbnStr[isbnStr.length() - 1]);
      } 
   }
   
   lastInt = isbnStr[isbnStr.length()-1];
   isbnStr.erase(isbnStr[isbnStr.length() - 1]);
   
   for( int i = 1; i < isbnStr.length(); i++ )
   {
      sum += i * isbnStr[i];
   }
   
   if( sum % (isbnStr.length() + 2) == lastInt )
   {
      isISBN = true;
   }
   else if ( sum % (isbnStr.length() + 2 ) == 10 && lastInt == 'X' )
   {
      isISBN = true;
   }
   else
      isISBN = false;
      
   return isISBN;
}

int main( int argc, char* argv[] )
{
   
   string inputStr;
   bool chk_isbn = false;
   
   printf("Enter 10 or 13 digit ISBN: ");
   cin >> inputStr;
   
   chkisbn(inputStr);
   
   if( chk_isbn )
   {
      cout << inputStr + " Good!";
   }
   else
   {
      cout << inputStr + " Bad!";
   }

   system("PAUSE");
   return 0;
}
That's because your program crashes. You're using string::erase the wrong way. see http://www.cplusplus.com/reference/string/string/erase/
The one you need is string& erase ( size_t pos = 0, size_t n = npos );
turn isbnStr.erase(isbnStr[i]); to isbnStr.erase(i, 1);

By the way, note that operator[] returns a char and not he integer value . That is, '0' == 48. Are you sure you want to do maths with them?

You never initialize your sum variable. It will most likely be a lot of garbage.
One problem I see is when you call erase on a string. The version your calling (1 parameter) is the pos to start deleting. I think you want to pass in i. Could be corrupting the stack, that's why it's not pausing.

1
2
3
4
5
6
7
8
  for( int i = 0; i < isbnStr.length(); i++ )
   {
      if( !isdigit(isbnStr[i]) )
      {
         //isbnStr.erase(isbnStr[isbnStr.length() - 1]);
         isbnStr.erase( i );
      } 
   }
Oh, thanks for the help! That did it perfectly.
Refrain from using system() commands. Stick with cin.get();
Topic archived. No new replies allowed.