program with padding a string.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 25
Reputation: halfnode is an unknown quantity at this point 
Solved Threads: 0
halfnode halfnode is offline Offline
Light Poster

program with padding a string.

 
1
  #1
Feb 18th, 2009
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?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string padBlanks(string, int);
  6.  
  7. int main()
  8. {
  9. string string1;
  10. int padN;
  11.  
  12. cout<< "Please enter a string";
  13. cin>> string1;
  14. cout<< "Please enter a number";
  15. cin >> padN;
  16.  
  17. padBlanks(string1, padN);
  18.  
  19. cin.ignore();
  20. cin.ignore();
  21. }
  22.  
  23. string padBlanks(string string1, int padN)
  24. {
  25. string blank(padN,' ');
  26.  
  27. cout<< blank <<string1;
  28. return 0;
  29. }

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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: program with padding a string.

 
0
  #2
Feb 18th, 2009
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:
  1. blank+=string1;
  2. return blank;
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: program with padding a string.

 
0
  #3
Feb 18th, 2009
You need to put the return 0; at the end of your main() function. The padBlanks() function will return the string.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: program with padding a string.

 
0
  #4
Feb 18th, 2009
  1. if (padN > 0)
  2. string1.insert(0,padN,' ');
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: program with padding a string.

 
0
  #5
Feb 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 25
Reputation: halfnode is an unknown quantity at this point 
Solved Threads: 0
halfnode halfnode is offline Offline
Light Poster

Re: program with padding a string.

 
0
  #6
Feb 20th, 2009
hey thanks for the help all. got it solved =)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 25
Reputation: halfnode is an unknown quantity at this point 
Solved Threads: 0
halfnode halfnode is offline Offline
Light Poster

Re: program with padding a string.

 
0
  #7
Feb 21st, 2009
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?

  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. string padBlanks(string, int);//function header
  7. void removeLetter(string& inStr, char);
  8.  
  9. int main()
  10. {
  11. string string1;
  12. int padN;
  13. string inStr;
  14. char alpha;
  15.  
  16. cout<< "Please enter a string"<<endl;
  17. cin>> string1;
  18. cout<< "Please enter a number"<<endl;
  19. cin >> padN;
  20.  
  21. padBlanks(string1, padN);
  22.  
  23. cout<< "Please enter a sentence"<<endl;
  24. cin>> inStr;
  25. cout<< "Please enter a char to be removed"<<endl;
  26. cin >> alpha;
  27.  
  28. cout<<"This is the string before function call "<<inStr <<endl;
  29. removeLetter(inStr, alpha);
  30.  
  31. cin.ignore();
  32. cin.ignore();
  33. }
  34.  
  35. string padBlanks(string string1, int padN)//function body
  36. {
  37.  
  38. string blank(padN,' ');
  39.  
  40. cout<< blank <<string1<<endl;
  41. return blank;
  42. }
  43.  
  44. void removeLetter(string& inStr, char alpha )
  45. {
  46.  
  47. //replace(inStr.begin(),inStr.end(),alpha," ");
  48. // remove(inStr.begin(), inStr.end(),alpha);
  49. int i;
  50. for ( i = 0; i < inStr.length(); i++)
  51. if (inStr.at(i) == alpha)
  52. {
  53. inStr.erase(i,1);
  54. i--;
  55. }
  56. cout<<"This is the string after function call "<< inStr <<endl;
  57. }
Last edited by halfnode; Feb 21st, 2009 at 10:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: program with padding a string.

 
0
  #8
Feb 22nd, 2009
>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...

  1. getline(cin,string1); // get a whole line
  2. cout<< "Please enter a number" << endl; // What's a number?
  3. if (cin >> padN) { // OK, test padBlanks
  4. ....
  5. } else { // bad input
  6. cout << "*** It's not a number" << endl;
  7. cin.clear(); // clear cin state
  8. cin.ignore(1000,'\n'); // skip bad input
  9. }
  10. ... 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC