strings, arrays, length program

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

Join Date: Jan 2008
Posts: 8
Reputation: curls is an unknown quantity at this point 
Solved Threads: 0
curls curls is offline Offline
Newbie Poster

strings, arrays, length program

 
0
  #1
Jan 19th, 2008
Hey. I'm having some trouble writing a program. I just begun C++ and don't know the language all that well. I've gotten a small start to the program and could really use some pointers.

Here are the program descriptions:
Read list of strings and store strings in array of strings.
Max. of 10 strings
Let n be the actual number of strings read
Use length function to find the length of each string. Store lengths in int array.
Find longest string and average length of all strings.

The output should be displayed in columns. Print the columns with a fixed field width=longest-word-length + 3. Avg. should be printed as a float with two decimal places.

Ok. So, that is what we know.
I know that I will need to define an array. I will have to use a for-loop. I will have to use the .length() statement. I need iomanipulators, setw() and setprecision(). I have all of this figured out and have begun writing the program some, and it's kind of broken -- so far this is what I have......
  1. #include <iostream>
  2. #include <iomanip> // For setw() and setprecision()
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_SIZE = 10;
  8.  
  9. int main ()
  10. {
  11. //Variable Declarations
  12. int n; // Number of strings to be read
  13. int i;
  14. string a[MAX_SIZE];
  15. float avgLength; // Average length of the strings
  16. string longestWord; // Longest word given
  17. int lengthofLongest; // Length of the longest word given
  18.  
  19.  
  20. for (i=0; i < 10; i++)
  21. {
  22. cout << "Enter the number of strings:" <<
  23. cin >> n >> endl;
  24. cout << "Enter a string:" <<
  25. }
From here I don't really know where to go. I'm not sure I made correct variable declarations. I don't know where I should put in the array and make it come into play. I know it goes into the for-loop. Then, I'm not sure where I should use the .length() statement.

Could someone please help me out just a little bit on the next step of solving this program and getting it to run correctly. Thank you!
Last edited by Narue; Jan 19th, 2008 at 6:50 pm. Reason: Added code tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strings, arrays, length program

 
0
  #2
Jan 19th, 2008
line 23: endl is only for cout, not cin, so you can delete that part of that line.

move lines 22 and 23 up outside the loop (between lines 19 and 20. Then the loop beginning on line 20 will go from 0 to n.

between lines 24 and 25 call getline() to get the string which may or may not contain spaces something like this: getline(cin, a[n]);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: curls is an unknown quantity at this point 
Solved Threads: 0
curls curls is offline Offline
Newbie Poster

Re: strings, arrays, length program

 
0
  #3
Jan 19th, 2008
Ok. I had already figured out to pull lines 22-23 out of the loop. What I am not sure of is what to put into the for-loop. Where and how do I use my array in this? I know within the for-loop I should prompt to read the strings into the array. Not sure how to do this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strings, arrays, length program

 
0
  #4
Jan 19th, 2008
>>I know within the for-loop I should prompt to read the strings into the array. Not sure how to do this
Read my post again -- I already gave you a hint about how to do that.

Suggestion: don't attempt to write the entire program all in one sitting. do just a little at a time and it will be easier to write. Write some, compile and test. Only after that works the way you want it do you contintinue with the next part of the assignment.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: curls is an unknown quantity at this point 
Solved Threads: 0
curls curls is offline Offline
Newbie Poster

Re: strings, arrays, length program

 
0
  #5
Jan 19th, 2008
Alright... so I fixed it with Ancient Dragon's suggestions and fixed any errors I had and it runs for what I have so far.....
  1. 1. #include <iostream>
  2. 2. #include <iomanip> // For setw() and setprecision()
  3. 3. #include <string>
  4. 4.
  5. 5. using namespace std;
  6. 6.
  7. 7. // Define
  8. 8. const int MAX_SIZE = 10; // Size of Array
  9. 9.
  10. 10. int main()
  11. 11. {
  12. 12.
  13. 13. // Variable Declarations
  14. 14. string A[MAX_SIZE];
  15. 15. int n; // number of strings read
  16. 16. int i;
  17. 17. float averageLength; // average length of strings
  18. 18. string longestWord;
  19. 19. int lengthofLongest;
  20. 20.
  21. 21.
  22. 22. cout << "Enter the number of strings:"<<
  23. 23. cin << ??? << n;
  24. 24.
  25. 25.
  26. 26.
  27. 27. // Loop
  28. 28. for (n=0; n < 10; n++);
  29. 29. {
  30. 30. cout << "Enter a string:";
  31. 31. getline(cin, A[n]);
  32. 32. }

That's what I've got so far. So, I think I have to for-loop right based on your hint. But, I don't know where to go from there. I know after the for-loop I need to make a calculation for averageLength and to use s.length() function to find length of the string. Not sure how to incorporate these, though. I'm really trying my best at it.
I'll give it a break for the night and get back on it for tomorrow. Thanks for the help so far!
Last edited by Ancient Dragon; Jan 19th, 2008 at 10:39 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strings, arrays, length program

 
0
  #6
Jan 19th, 2008
curis: it isn't necessary, or even desireable, for you to manually add the line numbers to the code you post. We have standard [code=c++] /* Your code goes here */ [/code] tags that will add the line numbers.


line 23: delete those question marks. Its obvious that you didn't compile that because those question marks would have cause error messages. And use >> operators, not << operator. Just one simple rule to remember: << is for cout and >> is for cin. So that line should read: cin >> n;
line 28: wrong, wrong wrong. You can't use variable n as a loop counter because it contains the number of strings you want to enter. for(int i = 0; i < n; ++i)
Last edited by Ancient Dragon; Jan 19th, 2008 at 10:44 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: curls is an unknown quantity at this point 
Solved Threads: 0
curls curls is offline Offline
Newbie Poster

Re: strings, arrays, length program

 
0
  #7
Jan 20th, 2008
Thanks. I realize now that I wrote the wrong operators for cin. And thanks for the help on the for-loop, I see what you're saying.
And, I didn't run those question marks when I compiled earlier... I just added them in when I posted because I don't know what to put there.
Anyway, thanks for the help so far, I'll see if I can figure some more out from here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: curls is an unknown quantity at this point 
Solved Threads: 0
curls curls is offline Offline
Newbie Poster

Re: strings, arrays, length program

 
0
  #8
Jan 20th, 2008
Okay. So, I figured out a little more. I've got my loop working partially. When it asks me to enter a number of strings, and say I enter 3, it still only allows me to enter just one word. How do I fix that, so that I can enter however many I specify? -- I'm guessing it's something to do with the for-loop and my array....? But, not really sure.
Also, I've got the table set up -- where all of the strings should go to, but I'm not sure how to use the .length() function to count the length of each word. Do I need to specify and identifier to use .length() function?

  1.  
  2. #include <iostream>
  3. #include <iomanip> // For setw() and setprecision()
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. // Define
  9. const int MAX_SIZE = 10; // Size of Array
  10.  
  11. int main()
  12. {
  13.  
  14. // Variable Declarations
  15. string A[MAX_SIZE];
  16. int n; // number of strings read
  17. int i;
  18. float averageLength; // average length of strings
  19. int lengthofLongest; // length of longest word
  20. string longestWord; // actual longest word
  21.  
  22.  
  23. cout << "Enter the number of strings:";
  24. cin >> n;
  25.  
  26.  
  27.  
  28. // Loop
  29. for (i=0; i < n; i++);
  30. {
  31. cout << "Enter a string:";
  32. getline(cin, A[n]);
  33. cin >> A[n];
  34.  
  35. }
  36.  
  37. // Table
  38. cout << setw(15) << "Words"
  39. << setw(15) << "Length" << endl;
  40. cout << fixed << setw(15) << A[n] << endl;
  41. cout << setw(15);
  42.  
  43. }
Last edited by curls; Jan 20th, 2008 at 3:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strings, arrays, length program

 
0
  #9
Jan 20th, 2008
1) delete line 33, it is destroying the value you enter from line 32.

2) line 32: you should be using i counter, not N. getline(cin, A[i]);
3) line 40: you need another loop around that line to display all the lines, similar to the loop you put on line 29. And don't forget to change A[n] to use the loop counter
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: curls is an unknown quantity at this point 
Solved Threads: 0
curls curls is offline Offline
Newbie Poster

Re: strings, arrays, length program

 
0
  #10
Jan 20th, 2008
1) Ok. I deleted line 32, but by doing that the program does not allow me to actually enter a string when it asks for one and just outputs everything else in the code.

2) I changed A[n], to A[i]

3) So I need another for-loop at line 40.

Still I need to know how to use to .length() function and where it comes into play...?

Thanks a lot for your help, by the way. I really do appreciate it. I'm doing my best to figure things out and not ask too many questions.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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