|
-
August 15th, 2011, 11:36 PM
#1
Assertion Error: Invalid Null Pointer
Hello everyone, I am learning c++ migrating from Visual basic. I have created a sample program in which a random number is generated and a user must guess what the number is. Unfortunately I am being bogged down by an assertion error, and I cannot figure out what is wrong with my code. Here is my code, and the error occurs at line 33. Any help would be much appreciated!
Code:
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
char thing = NULL;
vector<int> randNum(4);
vector<int> guesses(4);
cout << "Enter any number \n";
int awesome;
cin >> awesome;
srand(awesome);
cout << "Guess the four correct digits \n";
int bulls = 1;
int cows = 1;
string input = 0;
while (bulls < guesses.size()) {
bulls = 0;
cows = 0;
cout << "Enter Four Digits";
cin >> input;
for(int i = 0; i< randNum.size(); i++) {
randNum[i] = rand() % 10;
guesses[i] = atoi(input.substr(0, 1).c_str());
//5 = 4359
}
for (int ii = 0; ii < randNum.size(); ii++) {
for(int j = 0; j < guesses.size(); j++) {
if(guesses[j] == randNum[ii] && j == ii){
bulls += 1;
}
else if(guesses[j] == randNum[ii]){
cows += 1;
}
} //end for (int j)
} //end for(int ii)
if (bulls < guesses.size()) {
cout << " You have guessed" << bulls << " bulls and " << cows << " cows";
}
else
{
"Congratulations! The number you have guessed is correct!";
} //end if(bulls < 4)
} // end while bulls < 4
cin >> thing;
//keeps the console window open
return 0;
}
Last edited by cilu; August 16th, 2011 at 12:34 AM.
-
August 15th, 2011, 11:43 PM
#2
Re: Assertion Error: Invalid Null Pointer
I'm sorry I forgot to state what line 33 is. It is this line of code:
while (bulls < guesses.size()) {
-
August 16th, 2011, 12:28 AM
#3
Re: Assertion Error: Invalid Null Pointer
looks like you need to allocate memory for the vector array ...
try something like this and see
Code:
vector<int> guesses(4) = new vector<int>(4);
-
August 16th, 2011, 12:38 AM
#4
Re: Assertion Error: Invalid Null Pointer
@vcdebugger, why are you suggesting something like that? That is not even valid C++ code. You allocate memory on the heap, but don't use a pointer. He's code was already correct. Mind what you suggest.
@Dan3444, did you try debugging step by step? Frankly, I can't figure what could be wrong with that while condition to assert. Maybe you can show us the assertion text.
-
August 16th, 2011, 06:38 AM
#5
Re: Assertion Error: Invalid Null Pointer
@@Cilu: I did set breakpoints in the code. The point where it errors at is while (bulls < guesses.size()) {
I can't figure it out for the life of me either... The error text reads: debug assertion Failed!
Program: c:\users\dan\desktop\c++\dan5\debug\dan5.exe file f:\dd\vctools\crt_bld\self_x86\crt\src\xstring line: 1-94
Expression: invalid null pointer. For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
-
August 16th, 2011, 07:18 AM
#6
Re: Assertion Error: Invalid Null Pointer
It came from this line.
string input = 0;
I'm not really sure why you're trying to assign zero to a string. Leave off the assignment and you should be okay.
-
August 16th, 2011, 11:58 AM
#7
Re: Assertion Error: Invalid Null Pointer
 Originally Posted by Dan3444
@@Cilu: I did set breakpoints in the code. The point where it errors at is while (bulls < guesses.size()) {
GCDEF identified the probable cause:
This line of code does a lot of damage. The reason why this is bad is because there is no constructor of std::string that takes an integer. So what happens is that the compiler attempts to match up the integer with a compatible constructor for std::string, and unlucky for you, the constructor that it does match up best is this one:
Code:
string(const char*);
So how is this bad? The 0 is then converted to a 0-pointer to char, which is a NULL pointer. Then the std::string class is now attempting to read starting from address 0, causing all sorts of bad things to happen.
It's one of those things you must be aware of with std::string -- you can construct one with an integer. But constructing a std::string with an integer is not doing what you think it's doing, instead all havoc is occurring.
Regards,
Paul McKenzie
-
August 16th, 2011, 07:16 PM
#8
Re: Assertion Error: Invalid Null Pointer
Thanks everyone! I can't believe I made such a n00b mistake. Unfortunately Visual studio is a lot different with c++ than with Visual Basic; The only thing that seems to work is text highlighting; I suppose it's time for a new IDE.
-
August 16th, 2011, 07:52 PM
#9
Re: Assertion Error: Invalid Null Pointer
 Originally Posted by Dan3444
Thanks everyone! I can't believe I made such a n00b mistake. Unfortunately Visual studio is a lot different with c++ than with Visual Basic; The only thing that seems to work is text highlighting; I suppose it's time for a new IDE.
Not really sure what you mean by that, but the debugger works, which is how I found your problem.
-
August 17th, 2011, 07:14 AM
#10
Re: Assertion Error: Invalid Null Pointer
 Originally Posted by cilu
@vcdebugger, why are you suggesting something like that? That is not even valid C++ code. You allocate memory on the heap, but don't use a pointer. He's code was already correct. Mind what you suggest.
.
sorry, Cilu. I suggested that by seeing the line where it crashed.. thinking memory would not have been allocated.
Its long time i coded in C++ and still want to be in touch with it and learn something. Thats why I am here.with this.. I learnt ( or got aware ? :P) something. thanks.
-
August 17th, 2011, 04:14 PM
#11
Re: Assertion Error: Invalid Null Pointer
 Originally Posted by Dan3444
Unfortunately Visual studio is a lot different with c++ than with Visual Basic; The only thing that seems to work is text highlighting; I suppose it's time for a new IDE.
The thing is that in C/C++ you're allowed to do all kinds of stupid things without getting your fingers slapped by neither the compiler nor the IDE. I.e in Visual Basic you always have a baby sitter around, in C/C++ you're home alone... 
MSVC has one of the best debuggers around so don't throw it out. You will (and should) be using the debugger a lot while learning.
-
August 17th, 2011, 06:00 PM
#12
Re: Assertion Error: Invalid Null Pointer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|