Hey I was wondering if I could get some help. I just started to learn about pointers. And I seem to get it, i just cant seem to get why this code wont work. It does nothing useful I just wanted to see if I could create two function and make a third one using pointer so I could call both in this third function. This is what I came up with:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<Windows.h>
usingnamespace std;
int color(int number) //For changing color of the console
{
HANDLE color;
color = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(color,number);
return number;
}
string shout(string sentence) //For saying a word
{
cout << sentence;
return 0;
}
void all(string &refrence,int &number, string (*pointer)(string),int (*voidpointer)(int)) //To call both the color and shout function in one function
{
int typecolor;
typecolor = (*voidpointer)(number);
string phrase;
phrase = (*pointer)(refrence);
}
int _tmain(int argc, _TCHAR* argv[])
{
string word;
cout <<"Enter word > ";
getline(cin,word);
int colorneed = 5;
all(word,colorneed,shout,color);
return 0;
}
My code does it jobs it says the word that the user entered and give it in the color purple. But then it crashes and gives me this error:
Debug Assertion Failed!
Program: ........
File: c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring
Line: 930
Expression: invalid null pointer
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
If I take out the string part and just keep the color function my all function can call upon it no problem. But adding the string part cause the error shown above to show. Is there something I must know about string pointers to make this work? Any help is appreciated.