| | |
strings, arrays, length program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
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......
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!
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......
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> // For setw() and setprecision() #include <string> using namespace std; const int MAX_SIZE = 10; int main () { //Variable Declarations int n; // Number of strings to be read int i; string a[MAX_SIZE]; float avgLength; // Average length of the strings string longestWord; // Longest word given int lengthofLongest; // Length of the longest word given for (i=0; i < 10; i++) { cout << "Enter the number of strings:" << cin >> n >> endl; cout << "Enter a string:" << }
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.
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:
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.
>>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.
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.
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
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.....
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!
C++ Syntax (Toggle Plain Text)
1. #include <iostream> 2. #include <iomanip> // For setw() and setprecision() 3. #include <string> 4. 5. using namespace std; 6. 7. // Define 8. const int MAX_SIZE = 10; // Size of Array 9. 10. int main() 11. { 12. 13. // Variable Declarations 14. string A[MAX_SIZE]; 15. int n; // number of strings read 16. int i; 17. float averageLength; // average length of strings 18. string longestWord; 19. int lengthofLongest; 20. 21. 22. cout << "Enter the number of strings:"<< 23. cin << ??? << n; 24. 25. 26. 27. // Loop 28. for (n=0; n < 10; n++); 29. { 30. cout << "Enter a string:"; 31. getline(cin, A[n]); 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
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:
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.
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.
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
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?
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?
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> // For setw() and setprecision() #include <string> using namespace std; // Define const int MAX_SIZE = 10; // Size of Array int main() { // Variable Declarations string A[MAX_SIZE]; int n; // number of strings read int i; float averageLength; // average length of strings int lengthofLongest; // length of longest word string longestWord; // actual longest word cout << "Enter the number of strings:"; cin >> n; // Loop for (i=0; i < n; i++); { cout << "Enter a string:"; getline(cin, A[n]); cin >> A[n]; } // Table cout << setw(15) << "Words" << setw(15) << "Length" << endl; cout << fixed << setw(15) << A[n] << endl; cout << setw(15); }
Last edited by curls; Jan 20th, 2008 at 3:41 pm.
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.
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
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.
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- QBASIC and Visual Basic (Legacy and Other Languages)
- Need help with strings in C programming (C)
- problem with phonebook program (C)
- palindrome program (Legacy and Other Languages)
- Is there a way to tokenize an array of strings (C++)
- How to improve program with string compairing? (C++)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- how will i repeat my whole game program? (Java)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help on a School Project
- Next Thread: arrays, addition, and fibonacci
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






