c++ programming

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

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
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;
}
this didn't work for me :(
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.
no, it didn't work, if I find repeated words I want to enter them into a hashtable.
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.