Help needed with dynamic arrays.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 63
Reputation: fadia is an unknown quantity at this point 
Solved Threads: 0
fadia fadia is offline Offline
Junior Poster in Training

Help needed with dynamic arrays.

 
0
  #1
Oct 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster
 
0
  #2
Oct 29th, 2009
Post down your current code. This forum has a rule that we cannot give homework help until you show your effort.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 63
Reputation: fadia is an unknown quantity at this point 
Solved Threads: 0
fadia fadia is offline Offline
Junior Poster in Training
 
0
  #3
Oct 29th, 2009
Ok here is what i've done and not even sure whether these steps are right or not.


  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 63
Reputation: fadia is an unknown quantity at this point 
Solved Threads: 0
fadia fadia is offline Offline
Junior Poster in Training
 
0
  #4
Oct 29th, 2009
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 ==========
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 29
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
0
  #5
Oct 29th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: wich is an unknown quantity at this point 
Solved Threads: 0
wich wich is offline Offline
Newbie Poster
 
0
  #6
Oct 29th, 2009
thanks for this informative posting. its so useful for me.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 63
Reputation: fadia is an unknown quantity at this point 
Solved Threads: 0
fadia fadia is offline Offline
Junior Poster in Training
 
0
  #7
Oct 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 63
Reputation: fadia is an unknown quantity at this point 
Solved Threads: 0
fadia fadia is offline Offline
Junior Poster in Training
 
0
  #8
Oct 29th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 63
Reputation: fadia is an unknown quantity at this point 
Solved Threads: 0
fadia fadia is offline Offline
Junior Poster in Training
 
0
  #9
Oct 29th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC