944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 528
  • C++ RSS
Oct 29th, 2009
0

Help needed with dynamic arrays.

Expand Post »
Hello..
Can someone help me with this tutorial.
only have a background of how to creat a dynamic array in 4 steps but not other than that. And the question seems complicated more than only that. So kindly please someone help me and wxplain it to me.

---------------------
write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all string of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Don’t worry about proper names, if there first letter are changed to lowercase, that is acceptable. Treat a line break as if it was a blank, in the sense that line break and any number are blanks are compress to a single blank. Assume that the sentence ends with a period and contains no other period. For example the input

the Answer to life, the universe, and everything
IS 42.

Should produce

The answer to life, the universe, and everything is 42.


Note:
Following functions from might help:
1. str.insert(pos, str2)
2. str.erase(pos, length)

---------------
****Note that I haven't learnt the insert and erase functions yet.
I've no idea what will they do.
Similar Threads
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
fadia is offline Offline
89 posts
since Apr 2009
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
Post down your current code. This forum has a rule that we cannot give homework help until you show your effort.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
Ok here is what i've done and not even sure whether these steps are right or not.


C++ Syntax (Toggle Plain Text)
  1. # include <iostream>
  2. # include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. tepedef string* sentence;
  8.  
  9. sentence a;
  10.  
  11. a= new string[100];
  12.  
  13. cout<<"Please enter one sentence.";
  14.  
  15. getline(cin,a);
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. delete [] a;
  24.  
  25. return 0;
  26. }
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
fadia is offline Offline
89 posts
since Apr 2009
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
Oh it's typedef up there ! a typo!

but as to this code i'm getting a compiler error that says>>
1>------ Build started: Project: 102 hw, Configuration: Debug Win32 ------
1>Compiling...
1>fofa.cpp
1>c:\users\fofa\documents\visual studio 2005\projects\102 hw\fofa.cpp(15) : error C2664: 'std::getline' : cannot convert parameter 2 from 'sentence' to 'std::basic_string<_Elem,_Traits,_Ax> &'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>Build log was saved at "file://c:\Users\fofa\Documents\Visual Studio 2005\Projects\102 hw\Debug\BuildLog.htm"
1>102 hw - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
fadia is offline Offline
89 posts
since Apr 2009
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
I made some code for this but it is missing a check for the length of input and a newline character remover ( I don't know how you are going to enter a newline character through user input ).

I made 4 functions for this where 3 of them are used in 1 (the main function). See comments.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. //makes uppercase to lowercase
  7. void makeLower(string &in, int index)
  8. {
  9. if( in[index] >= 'A' && in[index] <= 'Z' )
  10. in[index] += 'a' - 'A';
  11. }
  12. //makes lowercase to uppercase
  13. void makeUpper(string &in, int index)
  14. {
  15. if( in[index] >= 'a' && in[index] <= 'z' )
  16. in[index] += 'A' - 'a';
  17. }
  18.  
  19. //makes 2 or more spaces into 1
  20. void checkSpaces(string &in)
  21. {
  22. for( int i = 0; i < in.length(); i++ )
  23. if( in[i] == ' ' && in[i+1] == ' ' && i != in.length()-1)
  24. {
  25. in.erase(i,1);
  26. i--;
  27. }
  28. }
  29.  
  30. //checks the string and does the above functions
  31. void check(string &in)
  32. {
  33. checkSpaces(in);
  34. bool peroid = true;
  35. for( int i = 0; i < in.length(); i++ )
  36. {
  37. if( (in[i] >= 'a' && in[i] <= 'z') || (in[i] >= 'A' && in[i] <= 'Z') )
  38. {
  39. if( peroid )
  40. {
  41. makeUpper(in, i);
  42. peroid = false;
  43. }
  44. else
  45. makeLower(in, i);
  46. }
  47. if( in[i] == '.' )
  48. peroid = true;
  49. }
  50. }
  51.  
  52. int main()
  53. {
  54. /*
  55. you can use a char array for getting user input so
  56. it limits it to 100 characters and then convert that
  57. to a string for checking
  58. */
  59. string in;
  60. getline(cin, in);
  61. check(in);
  62. cout << in << endl;
  63.  
  64. system("PAUSE"); //don't think you need this for VC++ but I need it for dev-C++
  65. return 0;
  66. }

I hope this helps
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
thanks for this informative posting. its so useful for me.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
wich is offline Offline
3 posts
since Oct 2009
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
lol ! Ok you kind of solved everything for me.. Thanks very much for that. But i really would like you to explain it to me.. How did you do it step by step if you don't mind.
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
fadia is offline Offline
89 posts
since Apr 2009
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
Oh also is this a dynamic array??
Cause you didn't include the basic steps that i've studies for dynamic array. It must be another method. If so, how can i do it to be a dynamic array?
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
fadia is offline Offline
89 posts
since Apr 2009
Oct 29th, 2009
0
Re: Help needed with dynamic arrays.
Oh I guess this can't be solved using dynamic arrays after i've revised it.
But can someone explain the coding. I want to know how please!
Reputation Points: 7
Solved Threads: 0
Junior Poster in Training
fadia is offline Offline
89 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Viewing .exe File Source / Editing .exe File
Next Thread in C++ Forum Timeline: Soccer game c++ PLEASE HELP!!!!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC