strings, arrays, length program

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

Join Date: Aug 2005
Posts: 15,406
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: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strings, arrays, length program

 
0
  #11
Jan 20th, 2008
>>Ok. I deleted line 32, but by doing that the program does not allow me to actually enter a string
Yes it does -- that is what the getline() function does. getline() accepts all that characters you type up to the <Enter> key (newline character). You can alter that behavor, but for now just use the default.

>>Still I need to know how to use to .length() function and where it comes into play
Don't worry about that just yet. Get this part of the program working first. After you get it to compile AND WORK correctly, then go back and re-read the program requirements that you originally posted

Use length function to find the length of each string. Store lengths in int array.
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
  #12
Jan 20th, 2008
Ok. I'll work on that. Thanks!
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
  #13
Jan 25th, 2008
Alright... think I got it, now. Seems to run correctly. See what you think or if you might have any suggestions. Thanks.

  1.  
  2. #include <iostream>
  3. #include <iomanip> // For setw() and setprecision()
  4. #include <string> // For using strings within program
  5.  
  6. using namespace std;
  7.  
  8. // Define
  9. const int MAX_SIZE = 10; // Size of Arrays
  10.  
  11. int main()
  12. {
  13.  
  14. // Variable Declarations
  15. string A[MAX_SIZE]; // Array of strings
  16. int I[MAX_SIZE]; // Array of integers
  17. int n = 0; // Number of strings read
  18. int i = 0; // For loops, element
  19. int lengthofAll = 0; // Length of all strings input by user
  20. float averageLength = 0.0; // Average length of strings
  21. string longestWord; // Longest word input by user
  22. int lengthofLongest = 0; // Length of longest string input by user
  23. int colWidth = 0; // Width of columns (lengthofLongest + 3)
  24.  
  25.  
  26. // Output - prompt user to enter number of strings
  27. cout << "Enter the number of strings (1-10):";
  28. cin >> n;
  29.  
  30. // Verify that 1<= n <= 10
  31. if ( (n<1) | (n>10) )
  32. {
  33. cout << "Enter a new number (1-10):" << endl;
  34. cin >> n;
  35. return -1;
  36. }
  37.  
  38.  
  39. // Loop - prompt user to input strings
  40. for (i=0; i < n; i++)
  41. {
  42. cout << "Enter strings:" << endl;
  43. getline(cin, A[i]); // use getline function to read whole line of strings
  44. cin >> A[i];
  45. I[i] = A[i].length(); // use length function to calculate length
  46. lengthofAll = lengthofAll + I[i];
  47.  
  48. if (lengthofLongest < I[i]) //Calculations
  49. {
  50. lengthofLongest = I[i];
  51. longestWord = A[i];
  52. colWidth = lengthofLongest + 3;
  53. }
  54. }
  55.  
  56.  
  57.  
  58. // Table Headings
  59. cout << endl;
  60. cout << setw(colWidth) << "Words" << setw(colWidth) << "Length" << endl;
  61.  
  62.  
  63. // Table Body Setup
  64. for (i=0; i<n; i++)
  65. {
  66. cout << fixed << setw(colWidth) << A[i] << setw(colWidth) << I[i] << endl;
  67. }
  68.  
  69. // Data Calculated from user's input
  70. cout << endl;
  71. cout << "Strings Processed:" << n << endl;
  72. cout << "Longest String:" << longestWord << endl;
  73. cout << "Length of Longest:" << lengthofLongest << endl;
  74. averageLength = (float) lengthofAll / n; // Calculation for averageLength
  75. cout << "Average Length:" << setprecision(2) << averageLength << endl;
  76.  
  77. // End program and return control to the operating system
  78. return 0;
  79.  
  80. }
Last edited by curls; Jan 25th, 2008 at 2:43 pm.
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