C++ strings

I am having some trouble with the below code. I can't think of anyway that would allow me to make the "word" appear the same amount of times as the number you input in and the "sep" not being at the end of the output. Also, you can't change the code outside of the "Enter your code" area. Any advice or hints would be more then appreciated, since this is driving me crazy right now.


#include <iostream>
#include <string>


using namespace std;

int main()
{
//Example: "And" "X" "2" = AndXAnd
// "A" "B" "5" = ABABABABA

cout << "Enter a word, then a separator, and integer :";
string word, sep, count;
cin >> word >> sep >> count;

string str = "";

//Enter code below




//Enter code above

cout << "The word " << word << " and " <<
sep << " becomes: " << str << endl;

return 0;

}
It'd be better to use an int for count instead of a string.
When getting an input of strings, use getline() instead.

Otherwise, you could use a for() loop -
1
2
3
4
5
6
for (int i; i < count; i++)
{
    print word
    if i is smaller than count - 1
        print sep
}


Sep is always printed 1 less than word, if you think that way.
I only know C ,so I give you some C code ,but you can understand that!
no function


//Example: "And" "X" "2" = AndXAnd
// "A" "B" "5" = ABABABABA
#include <stdio.h>
#include <string.h>
int main()
{
char word[256];
char sep;
int count,i;
printf("word is:\n");
gets(word);
printf("sep is:\n");
scanf("%c",&sep);
printf("count is:\n");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("%s%c",word,sep);
}
printf("\b \n");
return 0;
}
Topic archived. No new replies allowed.