hi all pretty new to programming here

the idea of the code is for the function to take in an string and an int and to pad the string with space on the left with the int being the number of space to put in.

am i doing something wrong?

#include <iostream>
#include <string>
using namespace std;

string padBlanks(string, int);

int main()
{
string string1;
int padN;

cout<< "Please enter a string";
cin>> string1;
cout<< "Please enter a number";
cin >> padN;

padBlanks(string1, padN);

cin.ignore();
cin.ignore();
}

string padBlanks(string string1, int padN)
{
string blank(padN,' ');

cout<< blank <<string1;
return 0;
}

i keep getting the error Expression invalid null pointer

"The program '[3180] test.exe: Native' has exited with code 3 (0x3)."

thanks for the help!

StuXYZ commented: Well done a good first post +5

Recommended Answers

All 7 Replies

First off, well done for using code tags for your first post.

Second, your error is that you are returning 0 but actually need to return a string.
Try as the last two lines of padBlanks, this:

blank+=string1;
return blank;

You need to put the return 0; at the end of your main() function. The padBlanks() function will return the string.

if (padN > 0)
    string1.insert(0,padN,' ');

;)

The problem is that when your function is returning a string, it first makes a copy of the string to be returned (in your case, it is NULL). For making copy, it has to take the length of the original string. Of course, strlen(NULL) fails.
You can make your string return a reference as
const string& PadBlanks(string string1, int padN)
Then the copy is no longer made. However, the variable pad should be globally accessible.

hey thanks for the help all. got it solved =)

i just found a bug with the coding. it doesn't accept a string with space in it such as test test. the spacing throws an error
Microsoft C++ exception: std::bad_alloc at memory location 0x0012f50c.

any advice?

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string padBlanks(string, int);//function header
void removeLetter(string& inStr, char);

int main()
{
string string1;
int padN;
string inStr;
char alpha;

cout<< "Please enter a string"<<endl;
cin>> string1;
cout<< "Please enter a number"<<endl;
cin >> padN;

padBlanks(string1, padN);

cout<< "Please enter a sentence"<<endl;
cin>> inStr;
cout<< "Please enter a char to be removed"<<endl;
cin >> alpha;

cout<<"This is the string before function call "<<inStr <<endl;
removeLetter(inStr, alpha);

cin.ignore();
cin.ignore();
}

string padBlanks(string string1, int padN)//function body
{

	string blank(padN,' ');

cout<< blank <<string1<<endl;
return blank;
}

void removeLetter(string& inStr, char alpha ) 
{  

			//replace(inStr.begin(),inStr.end(),alpha," ");
//	remove(inStr.begin(), inStr.end(),alpha);
int i;
for ( i = 0; i < inStr.length(); i++)
if (inStr.at(i) == alpha)
{
	inStr.erase(i,1);
 	i--;
}
	cout<<"This is the string after function call "<< inStr <<endl;
}

>i just found a bug with the coding.
Alas, you did not found a bug in this unpleasant, untidy code ;)
Next time use code tag properly:
[code=c++] codes...

[/code]

Well, cin >> string1 gets a single word only, not a line (see operator>> for std::string specification). For example, after the string1<Enter> the string1 value is "the" and the next input statement gets "string1". However the next input cin >> padN wants an integer but "string1" is not an integer and the cin input stream is in failed state now. No value for padN variable, it has undefined value after failed input. Probably, the program catch an exception when was trying to padd a huge or negative number of blanks...

getline(cin,string1); // get a whole line
cout<< "Please enter a number" << endl; // What's a number?
if (cin >> padN) { // OK, test padBlanks
    ....
} else { // bad input
    cout << "*** It's not a number" << endl;
    cin.clear(); // clear cin state
    cin.ignore(1000,'\n'); // skip bad input
}
... go on ...

Of course, the padBlanks function presented is a forged surrogate only. Have you ever seen my previous post? There is true padBlanks function body in the snippet.

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.