When I start to debug the following code in Visual C++ and it runs in debug mode, it gives me this error: "Unhandled exception at 0x1027d51c(msvcr100d.dll) in st.exe:0xC0000005: Access violation reading location 0x00000009." The weird part is that the program starts to run, but shows me the error a little into it.
Here is the code:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <time.h>
using namespace std;


int main () {

  int a, b, c, d, e;
  int junk;
   srand(time(NULL));

  char *scrpt[][2] = {
    "What color is the sky?", "Blue",
    "What is the shape of the moon?", "Circle",
    "What is the coloer of an apple?", "Red",
    "Which is longer, an inch or a meter?", "Meter",
    "Do these questions make sense?", "Nope"
  };
  a = rand() % 10 + 1;
    if (a == 2) a = 1; 
    if (a == 4) a = 3; 
    if (a == 6) a = 5; 
    if (a == 8) a = 7; 
    if (a == 10) a = 9; 
  
  b = rand() % 10 + 1;
    do { b = rand() % 10 + 1; 
      } while(a == b);
    if (b == 2) b = 1; 
    if (b == 4) b = 3; 
    if (b == 6) b = 5; 
    if (b == 8) b = 7; 
    if (b == 10) b = 9; 

  c = rand() % 10 + 1;
    do { c = rand() % 10 + 1; 
      } while((a == c) || (b == c));
    if (c == 2) c = 1; 
    if (c == 4) c = 3; 
    if (c == 6) c = 5; 
    if (c == 8) c = 7; 
    if (c == 10) c = 9; 

  d = rand() % 10 + 1;
    do { d = rand() % 10 + 1; 
      } while((a == d) || (b == d) || (c == d));
    if (d == 2) d = 1; 
    if (d == 4) d = 3; 
    if (d == 6) d = 5; 
    if (d == 8) d = 7; 
    if (d == 10) d = 9; 

  e = rand() % 10 + 1;
    do { e = rand() % 10 + 1; 
      } while((a == e) || (b == e) || (c == e) || (d == e));
    if (e == 2) e = 1; 
    if (e == 4) e = 3; 
    if (e == 6) e = 5; 
    if (e == 8) e = 7; 
    if (e == 10) e = 9; 

  cout << scrpt[a][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cin >> junk;
  cout << "\n";
  cout << scrpt[a][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[b][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cin >> junk;
  cout << "\n";
  cout << scrpt[b][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[c][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cin >> junk;
  cout << "\n";
  cout << scrpt[c][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[d][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cin >> junk;
  cout << "\n";
  cout << scrpt[d][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[e][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cin >> junk;
  cout << "\n";
  cout << scrpt[e][1];
  cout << "\n";
  cout << "\n";
  
  cout << "\n";
  cout << "Well done!\n";
  cin >> junk;

  return 0;
}

Also the code failed debug.

Recommended Answers

All 11 Replies

most likely the problem is line 66. The value of variable a may exceed the number of rows in the array script. Count the rows in that array -- there are only 5. But the value of a can be as much as 9. Most likely the same problem with all those other variables.

commented: An obvious truth that has yet to be recognised by the OP +20

shouldnt his char array be defined as something like char script[2][50] or do i have it backwards?

shouldnt his char array be defined as something like char script[2][50] or do i have it backwards?

No. The declaration is ok. What he declared is an 2d array of pointers.

ah okay. thanks AD

most likely the problem is line 66. The value of variable a may exceed the number of rows in the array script. Count the rows in that array -- there are only 5. But the value of a can be as much as 9. Most likely the same problem with all those other variables.

hhhmmmm.. tried that. I'm pretty sure you were right to change that, but the same error pops up. This time it didn't get to the part where is say passed/failed. Instead the program ran a little and then an error window popped up.
Also, when the error popped up, a new window/tab was created in Visual C++ called ".asm". I've never seen that before. In the .asm window, there was a lot of text that was very difficult to understand. In fact, I understood none of it. I might try running the code on bloodshed to see what happens.

Post your latest code.
Mostly when noobs describe they've fixed something and it doesn't work, they haven't actually fixed it at all.

I've modified the code to this:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
using namespace std;


int main () {

  int junk;

  char *scrpt[5][2] = {
    "What color is the sky?", "Blue",
    "What is the shape of the moon?", "Circle",
    "What is the coloer of an apple?", "Red",
    "Which is longer, an inch or a meter?", "Meter",
    "Do these questions make sense?", "Nope"
  };



  cout << scrpt[0][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cout << "\n";
  cout << scrpt[0][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[1][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cout << "\n";
  cout << scrpt[1][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[2][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cout << "\n";
  cout << scrpt[2][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[3][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cout << "\n";
  cout << scrpt[3][1];
  cout << "\n";
  cout << "\n";

  cout << scrpt[4][0];
  cout << "\n";
  cout << "Press ENTER if you are ready to reveal c/v.\n";
  cout << "\n";
  cout << scrpt[4][1];
  cout << "\n";
  cout << "\n";
  
  cout << "\n";
  cout << "Well done!\n";
  cin >> junk;

  return 0;
}

The program runs fine in debug mode, but notice what the text says in the output of visual C++ 2010:
'st.exe': Loaded 'C:\Documents and Settings\Chris\My Documents\Visual Studio 2010\Projects\st\Debug\st.exe', Symbols loaded.
'st.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file
'st.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Cannot find or open the PDB file
'st.exe': Loaded 'C:\WINDOWS\system32\msvcp100d.dll', Symbols loaded.
'st.exe': Loaded 'C:\WINDOWS\system32\msvcr100d.dll', Symbols loaded.
'st.exe': Loaded 'C:\WINDOWS\system32\shimeng.dll', Cannot find or open the PDB file
'st.exe': Unloaded 'C:\WINDOWS\system32\shimeng.dll'
The thread 'Win32 Thread' (0x15f4) has exited with code -1073741510 (0xc000013a).
The program '[5612] st.exe: Native' has exited with code -1073741510 (0xc000013a).

It's never says if it failed.

what settings are you using to write this program? what kind of project is it?

I get those similar warnings too -- just ignore them.

I know when I used to have MSVC++ 6.0 it had a special installer to install a bunch of old school NT dll's. maybe they stooped doing that so those errors come up.

Found on several pages after a brief search...
"The most common "C" error code is "0xC000013A: The application terminated as a result of a CTRL+C"

> cin >> junk;
Your "junk" is an int, which means you have to enter something like "0\n" to succeed.
Just pressing return endlessly won't get you past it.
Use say cin.get(); if you just want to wait for a character.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.