Here is my code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream os; // Initialize stringstream "os"
string mValue = "month"; // Initialize mValue "month"
int iValue = 1; // Initialize iValue "1"
for(int iValue = 1; iValue < 13; ++iValue) // Iteration 1: 1 < 12 so execute the following:
{
os << mValue << "" << iValue; // Glue mValue and iValue together
cout << os.str() << endl; // Print glued mValue and iValue
}
return 0;
}
This results in the following output:
month1
month2month2
month3month3month3
month4month4month4month4
month5month5month5month5month5
month6month6month6month6month6month6
month7month7month7month7month7month7month7
month8month8month8month8month8month8month8month8
month9month9month9month9month9month9month9month9month9
month10month10month10month10month10month10month10month10month10
month11month11month11month11month11month11month11month11month11month11
month12month12month12month12month12month12month12month12month12month12month12
The desired output is:
month1
month2
month3
month4
month5
month6
month7
month8
month9
month10
month11
month12
Being a noob at coding, I understand why this is happening but I don't know how to fix it. I tried to place cout outside of the for loop but that results in
month1month2month3month4month5month6month7month8month9month10month11month12
I'm out of ideas and I hope you can tell me how to get this right.
Aucun commentaire:
Enregistrer un commentaire