c++ programming

Nov 2, 2013 at 1:46am
cout <<Word: " << word << endl;
where word is a string: string word;

so my program is outputting:
Word: nature
Word: food
Word: cookie

I need it to output:
Word: nature, food, cookie


please help!!!!
Last edited on Nov 2, 2013 at 1:47am
Nov 2, 2013 at 2:00am
Not exactly clear what you want. I assume that you get different words (say from a file or array of strings) using a for loop.
1
2
3
4
5
6
7
8
string word;
cout<<"Word:";
for (int i=0;i<3;i++)
{
   Somehow get the word
   cout<<word;
}
cout<<endl;
Nov 2, 2013 at 2:02am
yes that's correct I get different words from a file using a for loop. Now I want it to print in the same line separated by commas.
Last edited on Nov 2, 2013 at 2:05am
Nov 2, 2013 at 2:23am
1
2
3
4
5
6
7
8
9
string word;
string before("Word: ");
for (int i=0;i<3;i++)
{
   word=getwordfromfile(i);
   cout<<before<<word;
   before=", ";
}
cout<<endl;

Nov 2, 2013 at 2:49am
thank you so much, could you please help me with something else. say I have a word repeated many times.

Word: cookie
Word: cookie
Word: cookie


how do I make it repeat one time?
Last edited on Nov 2, 2013 at 3:12am
Nov 2, 2013 at 2:54am
1
2
3
4
5
6
7
8
9
10
//get words

std::cout << "Word: ";

for( int i = 0; i < AMOUNT_OF_WORDS; ++i )
{
    if( i > 0 )
        std::cout << ", ";
    std::cout << word;
}
Nov 2, 2013 at 3:10am
this didn't work for me :(
Nov 2, 2013 at 3:19am
Define didn't work? you were supposed to replace word with your array of words and amount_of_words with the number of words in your array.
Nov 2, 2013 at 1:15pm
no, it didn't work, if I find repeated words I want to enter them into a hashtable.
Nov 2, 2013 at 5:23pm
can you please tell me how it is not working? Show me the code? 'It didn't work" is probably one of the most vague things to say. Also According to the example you provided that should work. Are you talking about not repeating words? You can put all your items into a list because lists won't contain multiples it will remove all the duplicates.

http://www.cplusplus.com/reference/list/list/?kw=list
Topic archived. No new replies allowed.