| | |
program with padding a string.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 25
Reputation:
Solved Threads: 0
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?
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!
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?
C++ Syntax (Toggle Plain Text)
#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!
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
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:
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:
c++ Syntax (Toggle Plain Text)
blank+=string1; return blank;
experience is the most expensive way to learn anything
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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.
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.
•
•
Join Date: Feb 2009
Posts: 25
Reputation:
Solved Threads: 0
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?
Microsoft C++ exception: std::bad_alloc at memory location 0x0012f50c.
any advice?
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by halfnode; Feb 21st, 2009 at 10:21 pm.
>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,
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.
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... c++ Syntax (Toggle Plain Text)
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 ...
![]() |
Similar Threads
- java assignment problem- need pro helps. (Java)
- problem in padding a string (C++)
- AES Encryption/Decryption Padding is invalid error (C#)
- hi i would like to know how to update files and search in datatype. (C++)
- Removing duplicates from string array. (C++)
- text program help needed (C)
- Please check my code below (Java)
- How this code works??? (C)
- Open In New Window Php (PHP)
- html table to .csv (HTML and CSS)
Other Threads in the C++ Forum
- Previous Thread: having problem with an if statement...
- Next Thread: this pointer
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






