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......

#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:" <<
  }

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!

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]);

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?

>>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.

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. #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!

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)

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.

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?

#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);

}

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

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

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.

>>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.

Alright... think I got it, now. Seems to run correctly. See what you think or if you might have any suggestions. Thanks.

#include <iostream>
#include <iomanip>  // For setw() and setprecision()
#include <string>	// For using strings within program

using namespace std;

// Define 
const int MAX_SIZE = 10;	// Size of Arrays

int main()
{

	// Variable Declarations
	string A[MAX_SIZE];				// Array of strings
	int I[MAX_SIZE];				// Array of integers
	int n = 0;						// Number of strings read
	int i = 0;						// For loops, element
	int lengthofAll = 0;			// Length of all strings input by user
	float averageLength = 0.0;		// Average length of strings
	string longestWord;				// Longest word input by user
	int lengthofLongest = 0;		// Length of longest string input by user
	int colWidth = 0;				// Width of columns (lengthofLongest + 3)

	
	// Output - prompt user to enter number of strings
	cout << "Enter the number of strings (1-10):";
		cin >> n;

	// Verify that 1<= n <= 10
		if ( (n<1) | (n>10) ) 
		{
			cout << "Enter a new number (1-10):" << endl;
			cin >> n;
			return -1;
		}

	
	// Loop - prompt user to input strings
	for (i=0; i < n; i++)
	{
		cout << "Enter strings:" << endl;
		getline(cin, A[i]);			// use getline function to read whole line of strings
		cin >> A[i];
		I[i] = A[i].length();		// use length function to calculate length
		lengthofAll = lengthofAll + I[i];
		
		if (lengthofLongest < I[i])	//Calculations
		{
			lengthofLongest = I[i];
			longestWord = A[i];
			colWidth = lengthofLongest + 3;
		}
	}



	// Table Headings 
	cout << endl;
	cout << setw(colWidth) << "Words" << setw(colWidth) << "Length" << endl;
	

	// Table Body Setup
	for (i=0; i<n; i++)
	{
		cout << fixed << setw(colWidth) << A[i] << setw(colWidth) << I[i] << endl;
	}

	// Data Calculated from user's input
	cout << endl;
	cout << "Strings Processed:" << n << endl;
	cout << "Longest String:" << longestWord << endl;
	cout << "Length of Longest:" << lengthofLongest << endl;
	averageLength = (float) lengthofAll / n;	// Calculation for averageLength
	cout << "Average Length:" << setprecision(2) << averageLength << endl;

	// End program and return control to the operating system
	return 0;

}
This article has been dead for over six months. Start a new discussion instead.