Strings, Arrays, and Random Values

I am getting a weird output in a program which results in extra spaces

Page 1 of 1

4 Replies - 973 Views - Last Post: 05 March 2009 - 11:02 AM Rate Topic: -----

#1 Mrav12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 09

Strings, Arrays, and Random Values

Posted 05 March 2009 - 08:25 AM

I have an assignment to ask the user for the input of as many words as they want to type in (as long as it's under 100), store these words in an array, and then output a sentence using random values and the words the user typed. It should contain \t in the coding for the output. The problem is, is that the spacing in the final output is not even and results more or less in what i want but with random spaces every once in a while. Thank you for any help you can provide. My code is below:

#include <iostream>
#include <stdlib.h>
#include <string>


using namespace std;

int main()
{
	string userInputWord;
	int loopCounter = 0;
	string totalOfWords[100];
	int sentenceLength;
   
	cout << "type some words, (stopCollecting to stop)" << endl;
	do
	{ 

		cin >> userInputWord;	
		if (userInputWord !="stopCollecting")
		{
			totalOfWords[loopCounter] = userInputWord;  
			loopCounter=loopCounter+1;
		}
	
	}
	while ((userInputWord != "stopCollecting") and (loopCounter < 100));
	
	do
	{
		cout << "Length of the sentence? : ";
		cin >> sentenceLength;
		for (int j=1; j<=sentenceLength; j++)
		{
			 cout << totalOfWords[(rand() % (loopCounter))] << "\t";
		}
		cout << endl;
	}
	while (sentenceLength>0);
	
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Strings, Arrays, and Random Values

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Strings, Arrays, and Random Values

Posted 05 March 2009 - 08:40 AM

View PostMrav12, on 5 Mar, 2009 - 02:25 PM, said:

I have an assignment to ask the user for the input of as many words as they want to type in (as long as it's under 100), store these words in an array, and then output a sentence using random values and the words the user typed. It should contain \t in the coding for the output. The problem is, is that the spacing in the final output is not even and results more or less in what i want but with random spaces every once in a while. Thank you for any help you can provide. My code is below:

I assume you are using \t the tab character to get you output into columns? an alternative is to use setw() to set the output field width, see
http://www.cplusplus...ators/setw.html

This post has been edited by horace: 05 March 2009 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 Mrav12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 09

Re: Strings, Arrays, and Random Values

Posted 05 March 2009 - 10:23 AM

I appreciate the tip but i have not learned that command yet and fear i would lost marks for not doing the assignment the way the prof intends. I appreciate the quick response, does anyone know what may be causing the extra spaces?
Was This Post Helpful? 0
  • +
  • -

#4 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Strings, Arrays, and Random Values

Posted 05 March 2009 - 10:44 AM

View PostMrav12, on 5 Mar, 2009 - 04:23 PM, said:

I appreciate the tip but i have not learned that command yet and fear i would lost marks for not doing the assignment the way the prof intends. I appreciate the quick response, does anyone know what may be causing the extra spaces?

\t the tab should just move to the next tab position - could you give us a sample of the your input and output?
Was This Post Helpful? 0
  • +
  • -

#5 Mrav12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 09

Re: Strings, Arrays, and Random Values

Posted 05 March 2009 - 11:02 AM

Here is some sample output :^:

type some words, (stopCollecting to stop)
hello
how
are
you
thanks
for
the
help
stopCollecting
Length of the sentence? : 5
help how how are are
Length of the sentence? : 6
hello hello the you for hello
Length of the sentence? : 8
for thanks are you help help how hello
Length of the sentence? : -5


See how randomly spaced some of them are? shouldn't they all be even? :blink:

wait, nevermind! I know what i did, sorry, i totally just wasted your guys' time! You can ignore my output lol, i had read something wrong in the question

This post has been edited by Mrav12: 05 March 2009 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1