#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;
}
Strings, Arrays, and Random ValuesI 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
#1
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:
Replies To: Strings, Arrays, and Random Values
#2
Re: Strings, Arrays, and Random Values
Posted 05 March 2009 - 08:40 AM
Mrav12, 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
#3
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?
#4
Re: Strings, Arrays, and Random Values
Posted 05 March 2009 - 10:44 AM
Mrav12, 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?
#5
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?
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
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?
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
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|